felzenszwalb.py 3.16 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
    Runs Segmenter Felzenszwalb's method implemented in skimage.segmentation.
    
    Efficient graph-based image segmentation, Felzenszwalb, P.F. and Huttenlocher, D.P. International Journal of Computer Vision, 2004
    
    Name: felzenszwalb.py
    Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
"""

from skimage.segmentation import felzenszwalb
from collections import OrderedDict

from util.config import Config

from segmenter import Segmenter
from skimage_segmenter import SkimageSegmenter

class Felzenszwalb(Segmenter, SkimageSegmenter):
    
    def __init__(self, scale = 100.0, sigma = 1.0, min_size = 20, border_color = 'Yellow', border_outline = 'No'):
        super(self.__class__, self).__init__(border_color, border_outline)
        
        self.scale = Config("Scale", scale, float)
        self.sigma = Config("Sigma", sigma, float)
        self.min_size = Config("Min Size", min_size, int)
        
    
    def get_config(self):
        felzenszwalb_config = OrderedDict()
        
        felzenszwalb_config["scale"] = self.scale
        felzenszwalb_config["sigma"] = self.sigma
        felzenszwalb_config["min_size"] = self.min_size
        felzenszwalb_config["border_color"] = self.border_color
        felzenszwalb_config["border_outline"] = self.border_outline
        
        return felzenszwalb_config
        
    def set_config(self, configs):
        self.scale = Config.nvl_config(configs["scale"], self.scale)
        self.sigma = Config.nvl_config(configs["sigma"], self.sigma)
        self.min_size = Config.nvl_config(configs["min_size"], self.min_size)
        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):
        felzenszwalb_config = OrderedDict()
        
        felzenszwalb_config[self.scale.label] = self.scale.value
        felzenszwalb_config[self.sigma.label] = self.sigma.value
        felzenszwalb_config[self.min_size.label] = self.min_size.value
        felzenszwalb_config[self.border_color.label] = self.border_color.value
        felzenszwalb_config[self.border_outline.label] = self.border_outline.value
        
        summary = ''
        for config in felzenszwalb_config:
            summary += "%s: %s\n" % (config, str(felzenszwalb_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 = { "scale": self.scale.get_cast_val(), 
                   "sigma": self.sigma.get_cast_val(), 
                   "min_size": self.min_size.get_cast_val()
                }

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