0
votes

Newbie here! I'm working with python plus opencv and skimage packages. I've segmented an image in superpixels using:

segments = slic(image, n_segments=numSegments, sigma=1, convert2lab=True)

I can access every superpixel with:

#FOR-LOOP-1
for v in np.unique(segments):
    #create a mask to access one region at the time
    mask = np.ones(image.shape[:2])
    mask[segments == v] = 0

    #my function to calculate mean of A channel in LAB color space
    A = mean_achannel(img, mask) 

Now I'd like to get the coordinates associated with each superpixel's centroid, how can I do that? I tried using:

from skimage.measure import regionprops

#FOR-LOOP-2
regions = regionprops(segments)
for props in regions:
    cx, cy = props.centroid  # centroid coordinates

But I can't understand how to link each region in the "FOR-LOOP-2" with the right one in the "FOR-LOOP-1". How can I calculate each region centroid inside "FOR-LOOP-1"?

1
Can't you just use for-loop-2, and get "v" using the centroids coordinates? - Miki
Yeah but how can I do that? - Jimbo
Please at least show some effort... getting a value in a matrix given the coordinates it's really not that hard... - Miki
I solved this with this line in for-loop-1: centers = np.append(centers, [np.mean(np.nonzero(segments == v), axis=1)]) - Jimbo

1 Answers

0
votes

All the desired values can be found using regionprops in for-loop-2:

from skimage.measure import regionprops

#FOR-LOOP-2
regions = regionprops(segments,
                      intensity_image=img[..., 1])
for props in regions:
    cx, cy = props.centroid  # centroid coordinates
    v = props.label  # value of label
    mean_a = props.mean_intensity