0

I try to use the implemented library slic from python on an RGB image. However, even if I set the multichannel to True, the output shape is (X, X), not (X, X, 3), and the saved image is simply a gray image with no color at all. The input image is here:

enter image description here

My code is as follows:

from skimage.segmentation import slic,mark_boundaries
from skimage import io
img = io.imread("luna.png", 0)
print("img shape: {0}".format(img.shape))

segments = slic(img, n_segments=10, compactness=10, start_label = 1, multichannel=True)
print("seg type:", type(segments))
print("seg shape: {0}".format(segments.shape))
print("seg max:", np.max(segments))
print("seg min:", np.min(segments))

seg_255 = (segments / np.max(segments)) * 255
seg_255 = seg_255.astype(np.uint8)
cv2.imwrite("luna_seg.png", seg_255)

The command line displays the following result:

img shape: (512, 512, 3)
seg type: <class 'numpy.ndarray'>
seg shape: (512, 512)
seg max: 34
seg min: 1

The output image is

enter image description here

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70

1 Answers1

0

If you ask for 10 segments, you will get back a single channel grey image with values 0..9 each value representing the segment number.

You can colour it however you like.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thanks, but your answer seems unrelated to my problem. I am confused why the output is not (X, X, 3), but (X, X). Wasn't the result supposed to be an RGB image too, if the input is an RGB image? I'd appreciate it so much if you could give me more help. – Fusberta Gaudreau Aug 23 '22 at 03:17
  • Please click `edit` under your question and add a representative input and corresponding output image. Also add output from `print(np.max(segments))` – Mark Setchell Aug 23 '22 at 07:08
  • You're so kind to give such detailed instructions for a new user of the stack overflow. I have edited it as described. – Fusberta Gaudreau Aug 23 '22 at 15:17
  • So do you understand now... each shade of grey represents a class or group of pixels in the image. They are like labels and you can assign any colour to each label. Example of assigning colours here... https://stackoverflow.com/a/62385424/2836621 – Mark Setchell Aug 23 '22 at 15:34
  • @MarkSetchell is correct. Each shade of gray represents a segment, and there are methods for coloring the segments. Take a look at the example in the link. Note the use of ```out1 = color.label2rgb(labels1, img, kind='avg', bg_label=0)```, which takes a grayscale image and colors it based on the average color of the original image within the segment https://scikit-image.org/docs/stable/auto_examples/segmentation/plot_rag_mean_color.html#sphx-glr-auto-examples-segmentation-plot-rag-mean-color-py. – Juancheeto Aug 24 '22 at 13:36