Yifeng_Li
Yifeng_Li

Reputation: 133

What is the difference between opencv ximgproc.slic and skimage segmentation.slic?

I run the SLIC (Simple Linear Iterative Clustering) superpixels algorithm from opencv and skimage on the same picture with, but got different results, the skimage slic result is better, Shown in the picture below.First one is opencv SLIC, the second one is skimage SLIC. I got several questions hope someonc can help.

  1. Why opencv have the parameter 'region_size' while skimage is 'n_segments'?
  2. Is convert to LAB and a guassian blur necessary?
  3. Is there any trick to optimize the opecv SLIC result?

===================================

Opencv SLIC

Skimage SLIC

# Opencv
src = cv2.imread('pic.jpg') #read image
# gaussian blur
src = cv2.GaussianBlur(src,(5,5),0)
# Convert to LAB
src_lab = cv.cvtColor(src,cv.COLOR_BGR2LAB) # convert to LAB

# SLIC
cv_slic = ximg.createSuperpixelSLIC(src_lab,algorithm = ximg.SLICO, 
region_size = 32)
cv_slic.iterate()



# Skimage
src = io.imread('pic.jpg')
sk_slic = skimage.segmentation.slic(src,n_segments = 256, sigma = 5)

Image with superpixels centroid generated with the code below

# Measure properties of labeled image regions
regions = regionprops(labels)
# Scatter centroid of each superpixel
plt.scatter([x.centroid[1] for x in regions], [y.centroid[0] for y in regions],c = 'red')

but there is one superpixel less(top-left corner), and I found that

len(regions) is 64 while len(np.unique(labels)) is 65 , why?

enter image description here

Upvotes: 8

Views: 4984

Answers (1)

Juan
Juan

Reputation: 5768

I'm not sure why you think skimage slic is better (and I maintain skimage! 😂), but:

  • different parameterizations are common in mathematics and computer science. Whether you use region size or number of segments, you should get the same result. I expect the formula to convert between the two will be something like n_segments = image.size / region_size.
  • The original paper suggests that for natural images (meaning images of the real world like you showed, rather than e.g. images from a microscope or from astronomy), converting to Lab gives better results.
  • to me, based on your results, it looks like the gaussian blur used for scikit-image was higher than for openCV. So you could make the results more similar by playing with the sigma. I also think the compactness parameter is probably not identical between the two.

Upvotes: 7

Related Questions