2
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)

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

2

2 Answers

3
votes

Try looking at skimage.measure.regionprops:

from skimage.measure import regionprops

regions = regionprops(segments)
for props in regions:
    cx, cy = props.centroid  # centroid coordinates
0
votes

You can do it easily by averaging each one of the coordinates by the class, in the following manner:

import numpy as np
import cv2

slic = cv2.ximgproc.createSuperpixelSLIC(image, n_segments=numSegments, sigma=1, convert2lab=True))

num_slic = slic.getNumberOfSuperpixels()

for cls_lbl in range(num_slic):
    fst_cls = np.argwhere(lbls==cls_lbl)
    x, y = fst_cls[:, 0], fst_cls[:, 1]
    c = (x.mean(), y.mean())
    print(f'Class {cls_lbl} centroid coordinates: ({c[0]:.1f}, {c[1]:.1f})')

Cheers