简体   繁体   English

如何使用屏蔽区域找到超像素的质心?

[英]How to find superpixels' centroid using masked region?

Newbie here!新手来了! I'm working with python plus opencv and skimage packages.我正在使用 python 加上 opencv 和 skimage 包。 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".但我无法理解如何将“FOR-LOOP-2”中的每个区域与“FOR-LOOP-1”中的正确区域联系起来。 How can I calculate each region centroid inside "FOR-LOOP-1"?如何计算“FOR-LOOP-1”内的每个区域质心?

All the desired values can be found using regionprops in for-loop-2:可以使用 for-loop-2 中的 regionprops 找到所有所需的值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM