slic.py 3.26 KB
Newer Older
1 2 3 4
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
5 6
    Runs Segmenter SLIC (Simple Linear Iterative Clustering) implemented in skimage.segmentation.
    
7 8
    Radhakrishna Achanta, Appu Shaji, Kevin Smith, Aurelien Lucchi, Pascal Fua, and Sabine Suesstrunk, SLIC Superpixels Compared to State-of-the-art Superpixel Methods, TPAMI, May 2012.
    
9 10
    Name: slic.py
    Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
11 12 13
"""

from skimage.segmentation import slic
14
from collections import OrderedDict
15

16 17 18
from util.config import Config
from util.utils import TimeUtils
from util.x11_colors import X11Colors
19

20
from segmenter import Segmenter
21
from skimage_segmenter import SkimageSegmenter
22

23
class Slic(Segmenter, SkimageSegmenter):
24

25 26 27
    def __init__(self, n_segments = 250, sigma = 5.0, compactness = 10.0, border_color = 'Yellow', border_outline = 'No'):
        super(self.__class__, self).__init__(border_color, border_outline)
        
28
        self.n_segments = Config("Segments", n_segments, int)
29 30
        self.sigma = Config("Sigma", sigma, float)
        self.compactness = Config("Compactness", compactness, float)
31
        
32 33
    
    def get_config(self):
34 35 36 37 38 39 40 41 42
        slic_config = OrderedDict()
        
        slic_config["n_segments"] = self.n_segments
        slic_config["sigma"] = self.sigma
        slic_config["compactness"] = self.compactness
        slic_config["border_color"] = self.border_color
        slic_config["border_outline"] = self.border_outline
        
        return slic_config
43 44
        
    def set_config(self, configs):
45
        self.n_segments = Config.nvl_config(configs["n_segments"], self.n_segments)
46 47
        self.sigma = Config.nvl_config(configs["sigma"], self.sigma)
        self.compactness = Config.nvl_config(configs["compactness"], self.compactness)
48 49 50 51
        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.border_outline.value = self.border_outline.value if self.border_outline.value == 'Yes' else 'No'
52 53

    def get_summary_config(self):
54 55 56 57 58 59 60 61 62 63 64 65 66
        slic_config = OrderedDict()
        
        slic_config[self.n_segments.label] = self.n_segments.value
        slic_config[self.sigma.label] = self.sigma.value
        slic_config[self.compactness.label] = self.compactness.value
        slic_config[self.border_color.label] = self.border_color.value
        slic_config[self.border_outline.label] = self.border_outline.value
        
        summary = ''
        for config in slic_config:
            summary += "%s: %s\n" % (config, str(slic_config[config]))
        
        return summary
67 68
    

69
    def get_segment(self, px = 0, py = 0, idx_segment = None):
70 71
        return self.get_segment_skimage(px, py, idx_segment)
    
72
    def paint_segment(self, image, color, px = 0, py = 0, idx_segment = None, border = True, clear = False):
73
        return self.paint_segment_skimage(image, color, px, py, idx_segment, border, clear)
74 75


76 77
    def run(self, image):
        
78 79 80 81
        args = { "n_segments": self.n_segments.get_cast_val(), 
                   "sigma": self.sigma.get_cast_val(), 
                   "compactness": self.compactness.get_cast_val()
                }
82

83
        return self.run_skimage(image, slic, **args)
84