arcLength 函数(没有 OpenCv)

arcLength function (without OpenCv)

我正在努力避免将 CV2 用于工作海豚。我在任何地方都使用 skimage.measure.find_contours. But I couldn't find the 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

提前致谢,抱歉我的英语不好。

在 OpenCV 中,arcLength() 函数用于计算 contour/shape 对象的周长。可以查到here

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

用法:

#--- importing the module ---
from skimage.measure import perimeter

print('Perimeter : {}'.format(perimeter(image)))

确保图像是 float 数据类型。包含 contour/shape 对象的像素必须具有值 1.0,而其他像素具有 0.0.

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

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

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

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