quickshift.py 3.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
    Runs Segmenter Quickshift implemented in skimage.segmentation.
    
    Quick shift and kernel methods for mode seeking, Vedaldi, A. and Soatto, S. European Conference on Computer Vision, 2008
    
    Name: quickshift.py
    Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""

from skimage.segmentation import quickshift
from collections import OrderedDict

from util.config import Config

from segmenter import Segmenter
from skimage_segmenter import SkimageSegmenter

class Quickshift(Segmenter, SkimageSegmenter):

    def __init__(self, ratio = 0.5, kernel_size = 2.0, max_dist = 10.0, border_color = 'Yellow', border_outline = 'No'):
        super(self.__class__, self).__init__(border_color, border_outline)
        
        self.ratio = Config("Ratio [0-1]", ratio, float)
        self.kernel_size = Config("Kernel Size", kernel_size, float)
        #self.sigma = Config("Sigma", sigma, float)
        self.max_dist = Config("Max Dist", max_dist, float)
        
    
    def get_config(self):
        quickshift_config = OrderedDict()
        
        quickshift_config["ratio"] = self.ratio
        quickshift_config["kernel_size"] = self.kernel_size
        #quickshift_config["sigma"] = self.sigma
        quickshift_config["max_dist"] = self.max_dist
        quickshift_config["border_color"] = self.border_color
        quickshift_config["border_outline"] = self.border_outline
        
        return quickshift_config
        
    def set_config(self, configs):
        self.ratio = Config.nvl_config(configs["ratio"], self.ratio)
        self.kernel_size = Config.nvl_config(configs["kernel_size"], self.kernel_size)
        #self.sigma = Config.nvl_config(configs["sigma"], self.sigma)
        self.max_dist = Config.nvl_config(configs["max_dist"], self.max_dist)
        self.border_color = Config.nvl_config(configs["border_color"], self.border_color)
        self.border_outline = Config.nvl_config(configs["border_outline"], self.border_outline)
        
        self.ratio.value = self.ratio.value if self.ratio.value <= 1 else 1
        self.border_outline.value = self.border_outline.value if self.border_outline.value == 'Yes' else 'No'

    def get_summary_config(self):
        quickshift_config = OrderedDict()
        
        quickshift_config[self.ratio.label] = self.ratio.value
        quickshift_config[self.kernel_size.label] = self.kernel_size.value
        #quickshift_config[self.sigma.label] = self.sigma.value
        quickshift_config[self.max_dist.label] = self.max_dist.value
        quickshift_config[self.border_color.label] = self.border_color.value
        quickshift_config[self.border_outline.label] = self.border_outline.value
        
        summary = ''
        for config in quickshift_config:
            summary += "%s: %s\n" % (config, str(quickshift_config[config]))
        
        return summary


    def get_segment(self, px = 0, py = 0, idx_segment = None):
        return self.get_segment_skimage(px, py, idx_segment)
    
    def paint_segment(self, image, color, px = 0, py = 0, idx_segment = None, border = True, clear = False):
        return self.paint_segment_skimage(image, color, px, py, idx_segment, border, clear)


    def run(self, image):
        
        args = { "ratio": self.ratio.get_cast_val(), 
                   "kernel_size": self.kernel_size.get_cast_val(), 
                   #"sigma": self.sigma.get_cast_val(), 
                   "max_dist": self.max_dist.get_cast_val()
                }

        return self.run_skimage(image, quickshift, **args)