Pynovisao - Adicionando segmentadores Felzenszwalb e Quickshift
Showing
... | ... | @@ -4,15 +4,13 @@ |
""" | ||
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 ) | ||
""" | ||
import cv2 | ||
import numpy as np | ||
from skimage.segmentation import slic | ||
from skimage.segmentation import mark_boundaries | ||
from skimage.util import img_as_float, img_as_ubyte | ||
from collections import OrderedDict | ||
from util.config import Config | ||
... | ... | @@ -20,20 +18,16 @@ from util.utils import TimeUtils |
from util.x11_colors import X11Colors | ||
from segmenter import Segmenter | ||
from skimage_segmenter import SkimageSegmenter | ||
class Slic(Segmenter): | ||
class Slic(Segmenter, SkimageSegmenter): | ||
def __init__(self, n_segments = 100, sigma = 5.0, compactness = 10.0, border_color = 'Yellow', border_outline = 'No'): | ||
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) | ||
self.border_color = Config("Border Color", border_color, 'color') | ||
self.border_outline = Config("Border Outline", border_outline, str) | ||
self._segments = None | ||
self._original_image = None | ||
def get_config(self): | ||
... | ... | @@ -73,92 +67,18 @@ class Slic(Segmenter): |
def get_segment(self, px = 0, py = 0, idx_segment = None): | ||
if self._segments is None: | ||
return None, 0, -1, 0 | ||
start_time = TimeUtils.get_time() | ||
if idx_segment is None: | ||
idx_segment = self._segments[py, px] | ||
mask_segment = np.zeros(self._original_image.shape[:2], dtype="uint8") | ||
mask_segment[self._segments == idx_segment] = 255 | ||
size_segment = mask_segment[self._segments == idx_segment].size | ||
segment = self._original_image.copy() | ||
segment = cv2.bitwise_and(segment, segment, mask=mask_segment) | ||
contours, _ = cv2.findContours(mask_segment,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)[-2:] | ||
m = -1 | ||
max_contour = None | ||
for cnt in contours: | ||
if (len(cnt) > m): | ||
m = len(cnt) | ||
max_contour = cnt | ||
x,y,w,h = cv2.boundingRect(max_contour) | ||
segment = segment[y:y+h, x:x+w] | ||
end_time = TimeUtils.get_time() | ||
return segment, size_segment, idx_segment, (end_time - start_time) | ||
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): | ||
if self._segments is None: | ||
return image, 0 | ||
start_time = TimeUtils.get_time() | ||
if idx_segment is None: | ||
idx_segment = self._segments[py, px] | ||
height, width, channels = self._original_image.shape | ||
mask_segment = np.zeros(self._original_image.shape[:2], dtype="uint8") | ||
mask_segment[self._segments == idx_segment] = 255 | ||
mask_inv = cv2.bitwise_not(mask_segment) | ||
class_color = np.zeros((height,width,3), np.uint8) | ||
class_color[:, :] = X11Colors.get_color(color) | ||
if clear == False: | ||
colored_image = cv2.addWeighted(self._original_image, 0.7, class_color, 0.3, 0) | ||
else: | ||
colored_image = self._original_image | ||
colored_image = cv2.bitwise_and(colored_image, colored_image, mask=mask_segment) | ||
new_image = cv2.bitwise_and(image, image, mask=mask_inv) | ||
mask_segment[:] = 255 | ||
new_image = cv2.bitwise_or(new_image, colored_image, mask=mask_segment) | ||
return self.paint_segment_skimage(image, color, px, py, idx_segment, border, clear) | ||
< |