【问题标题】:arcLength function (without OpenCv)arcLength 函数(没有 OpenCv)
【发布时间】:2019-01-17 11:13:50
【问题描述】:

我正在努力避免 CV2 用于工作 porpuses。我正在使用skimage.measure.find_contours。但我在任何地方都找不到等效的 CV2 arcLength 函数。

如果没有 CV2 函数,我如何计算轮廓周长或曲线长度。

这是我的代码,以防万一:

from skimage import measure

hresh = get_threshold(image, 127)
contours = measure.find_contours(thresh, 0.8)

for contour in contours:
    approx = aproximate_curve(contour, 0.1 * my_unknown_arc_length_function(contour))  # I have no idea about arc_length_function implementation

在此先感谢,对我的英语感到抱歉。

【问题讨论】:

    标签: python scikit-image cv2


    【解决方案1】:

    一开始我也被困在那里,但后来我意识到自己做数学很容易。

    以防万一以后有人需要此代码。

    import numpy as np
    
    def perimeter(contour):
        assert contour.ndim == 2 and contour.shape[1] == 2, contour.shape
     
        shift_contour = np.roll(contour, 1, axis=0)
        dists = np.sqrt(np.power((contour - shift_contour), 2).sum(axis=1))
        return dists.sum()
    

    【讨论】:

      【解决方案2】:

      在 OpenCV 中,arcLength() 函数用于查找轮廓/形状对象的周长。可以找到here

      在 scikit 映像中,可以在名为 perimetermeasure 模块中找到相同的功能。

      用法:

      #--- importing the module ---
      from skimage.measure import perimeter
      
      print('Perimeter : {}'.format(perimeter(image)))
      

      确保图像是float 数据类型。包含轮廓/形状对象的像素的值必须为1.0,而其他像素的值为0.0

      关于这个功能的讨论很多on this link

      【讨论】:

      • 谢谢!但是,如何将该函数应用于单个轮廓而不是图像中的所有轮廓
      猜你喜欢
      • 2023-04-03
      • 2013-12-08
      • 2018-01-08
      • 1970-01-01
      • 1970-01-01
      • 2019-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多