pythonimage-processingimage-segmentationscikit-imageinterpretation

What exactly does skimage.segmentation.slic() represent?


I am working on copy move forgery detection and got stuck on one of the algorithms. I have an RGB image of 532x800 pixels.

When the following code is run:

import matplotlib.pyplot as plt
from skimage.segmentation import slic, mark_boundaries
from skimage.util import img_as_float
from skimage import io

img_rgb = img_as_float(io.imread(PATH))
segments = slic(image=img_rgb, n_segments=1000)
print(img_rgb.shape)
print(segments.shape)
img_rgb = mark_boundaries(image=img_rgb, label_img=segments)
plt.imshow(img_rgb)
plt.show()

it returns:

(532, 800, 3)
(532, 800)

and this image: output_image.

Since the input image and the variable 'segments' are of the same dimensions (except for the 3rd channels dimension),

  1. Why are they of same dimensions?
  2. The different values in the 'img_rgb' 3D matrix represent color intensities. What does the values in variable 'segments' represent? How can they be similarly interpreted?

Thank you for your inputs.

Edit: I'm aware it returns a numpy array. I'm interested in knowing what does it represent.


Solution

  • Found what I was looking for.

    Here's a code that I wrote

    import matplotlib.pyplot as plt
    from skimage.segmentation import slic, mark_boundaries
    from skimage.util import img_as_float
    from skimage import io
    from skimage.transform import resize
    import numpy as np
    
    img_rgb = img_as_float(io.imread("apple_32x32.png"))
    img_rgb = resize(image=img_rgb, output_shape=(32, 32, 3))
    
    segments = slic(image=img_rgb, n_segments=10)
    print(img_rgb.shape)
    print(segments.shape)
    img_rgb = mark_boundaries(image=img_rgb, label_img=segments)
    plt.imshow(img_rgb)
    plt.show()
    
    np.savetxt("segments.txt", segments, fmt='%i')
    

    It outputs this image: output_image.

    and this to the console:

    (32, 32, 3)
    (32, 32)
    

    and writes the variable 'segments' to the file 'segments.txt'. The content of the file is shown below:

    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1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 2 2 2 3 3 3 3 3 5 5 5
    4 4 4 4 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 5 5 5
    4 4 4 4 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 5 5 5
    4 4 4 4 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 5 5 5
    4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 5 5 5
    4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 5 5 5 5
    4 4 4 4 4 4 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 5 5 5 5 5
    4 4 4 4 4 4 4 3 3 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 5 5 5 5 5 5
    4 4 4 4 4 4 4 3 3 3 3 3 2 2 2 2 2 2 2 3 3 3 3 3 3 3 5 5 5 5 5 5
    4 4 4 4 4 4 4 3 3 3 3 3 3 2 2 2 2 2 2 3 3 3 3 3 3 3 5 5 5 5 5 5
    4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5
    4 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5
    4 4 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5 5
    4 4 4 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5 5 5
    

    As you can see the variable 'segments' has the same size as that of the input image. But every value of segments[i,j] represent which cluster the pixel [i,j] of the image belongs to.