Put RGB2Lab into slic as it seems to be essential.

This commit is contained in:
Andreas Mueller
2012-08-03 12:00:39 +01:00
parent cd1007a0bc
commit b6059b5672
2 changed files with 8 additions and 4 deletions
+1 -2
View File
@@ -8,10 +8,9 @@ import numpy as np
from skimage.data import lena
from skimage.segmentation import slic, visualize_boundaries
from skimage.util import img_as_float
from skimage.color import rgb2lab
img = img_as_float(lena()).copy("C")
segments = slic(rgb2lab(img), ratio=10.0, n_segments=1000)
segments = slic(img, ratio=10.0, n_segments=1000)
print("number of segments: %d" % len(np.unique(segments)))
+7 -2
View File
@@ -3,9 +3,10 @@ cimport numpy as np
from time import time
from scipy import ndimage
from ..util import img_as_float
from ..color import rgb2lab
def slic(image, n_segments=100, ratio=10., max_iter=10, sigma=1):
def slic(image, n_segments=100, ratio=10., max_iter=10, sigma=1, convert2lab=True):
"""Segments image using k-means clustering in Color-(x,y) space.
Parameters
@@ -19,6 +20,9 @@ def slic(image, n_segments=100, ratio=10., max_iter=10, sigma=1):
maximum number of iterations of k-means
sigma: float
Width of Gaussian smoothing kernel for preprocessing.
convert2lab: bool
Whether the input should be converted to Lab colorspace prior to segmentation.
For this purpose, the input is assumed to be RGB. Highly recommended.
Returns
-------
@@ -28,7 +32,6 @@ def slic(image, n_segments=100, ratio=10., max_iter=10, sigma=1):
Notes
-----
The image is smoothed using a Gaussian kernel prior to segmentation.
Best results are achieved if the image is given in Lab color space.
References
----------
@@ -41,6 +44,8 @@ def slic(image, n_segments=100, ratio=10., max_iter=10, sigma=1):
if image.shape[2] != 3:
ValueError("Only 3-channel 2d images are supported.")
image = ndimage.gaussian_filter(img_as_float(image), sigma)
if convert2lab:
image = rgb2lab(image)
# initialize on grid:
height, width = image.shape[:2]