【问题标题】:How to find each SLIC superpixel's centroid in python?如何在 python 中找到每个 SLIC 超像素的质心?
【发布时间】:2018-10-03 10:20:56
【问题描述】:

这里是新手! 我正在使用 python plus opencv 和 skimage 包。我使用以下方法将图像分割成超像素:

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

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

【问题讨论】:

    标签: python opencv image-processing image-segmentation scikit-image


    【解决方案1】:

    试试看skimage.measure.regionprops

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

    【讨论】:

      【解决方案2】:

      您可以通过按类对每个坐标进行平均来轻松做到这一点,方法如下:

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

      干杯

      【讨论】:

        猜你喜欢
        • 2018-10-03
        • 2019-09-24
        • 2017-10-07
        • 2015-12-27
        • 2019-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多