8

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

Opencv SLIC

  • Skimage 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

Yifeng_Li
  • 133
  • 2
  • 6

1 Answers1

7

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.
Juan
  • 5,433
  • 21
  • 23
  • 1
    Thanks a lot for your advice. Now I got better results. However, there occurs another problem, specific in the newly updated quesion. – Yifeng_Li Jul 16 '19 at 13:54
  • 2
    Thanks again for you patience. I found the reason is tha zero is considered unlabeled so isn't counted by regionprops. Can use `regionprops(labels + 1)` to get the right result. – Yifeng_Li Jul 17 '19 at 00:48
  • I think the formula should be region_size = sqrt( image_size / n_segments). region_size seem to refer to the width/height of the region and thus should correspond to the step in skimage – CookieMaster Nov 25 '20 at 22:32