Misha Semeniuk
Misha Semeniuk

Reputation: 3

Slic segmentation in skimage

I'm using skimage library to define graph nodes and edges, which will describe certain image. After applying algorithm and plotting segmented regions I have realized that one of regions was not labeled. My goal is label all regions and find out all neighbors for each of them, but I've stuck in attempts to answer this question. I would be really grateful for any helpful information.

import imageio
import numpy as np
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
from skimage.measure import regionprops
from skimage.segmentation import slic
from skimage.segmentation import mark_boundaries

def rgb2gray(rgb):
    return np.dot(rgb[..., :3], [0.2126, 0.7152, 0.0722])

image = imageio.imread(img_file_path)
segments_slic = slic(image, n_segments=250, compactness=100)
regions = regionprops(segments_slic, intensity_image=rgb2gray(image))
for props in regions:
    cy, cx = props.centroid
    plt.plot(cx, cy, 'ro')

plt.imshow(mark_boundaries(image, segments_slic))
plt.show()

Original image Labeled image

Upvotes: 0

Views: 1873

Answers (1)

Juan
Juan

Reputation: 5738

This is an unfortunate historical accident: SLIC returns segments starting from 0, but regionprops (and most other functions) treat 0 as the background. To fix your code, add 1 to the output of SLIC:

segments_slic = 1 + slic(image, n_segments=250, compactness=100)

Then you get the output you expect, with the top-left segment (formerly 0, now 1) properly detected:

slic-modified-output

Upvotes: 2

Related Questions