如何在 python 中找到每个 SLIC 超像素的质心?

How to find each SLIC superpixel's centroid in python?

这里是新手! 我正在使用 python 加上 opencv 和 skimage 包。我使用以下超像素分割图像:

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

现在我想获取与每个超像素的质心关联的坐标,我该怎么做?

尝试查看 skimage.measure.regionprops:

from skimage.measure import regionprops

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

您可以通过 class 对每个坐标进行平均来轻松完成此操作,方法如下:

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})')

干杯