#!/usr/bin/python # -*- coding: utf-8 -*- # """ Runs Segmenter SLIC (Simple Linear Iterative Clustering) implemented in skimage.segmentation. 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. Name: slic.py Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com ) """ from skimage.segmentation import slic from collections import OrderedDict from util.config import Config from util.utils import TimeUtils from util.x11_colors import X11Colors from segmenter import Segmenter from skimage_segmenter import SkimageSegmenter class Slic(Segmenter, SkimageSegmenter): 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) self.n_segments = Config("Segments", n_segments, int) self.sigma = Config("Sigma", sigma, float) self.compactness = Config("Compactness", compactness, float) def get_config(self): 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 def set_config(self, configs): self.n_segments = Config.nvl_config(configs["n_segments"], self.n_segments) self.sigma = Config.nvl_config(configs["sigma"], self.sigma) self.compactness = Config.nvl_config(configs["compactness"], self.compactness) 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' def get_summary_config(self): 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 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 = { "n_segments": self.n_segments.get_cast_val(), "sigma": self.sigma.get_cast_val(), "compactness": self.compactness.get_cast_val() } return self.run_skimage(image, slic, **args)