【问题标题】:Calculating perimeter of contours (OpenCV, Python)计算轮廓周长(OpenCV,Python)
【发布时间】:2021-12-10 12:41:19
【问题描述】:

我有这张图片,上面有很多青色、品红色和黄色的小打印机点。

分离颜色通道 (CMYK) 后,我在图像上应用了阈值。

这里是颜色通道青色。

现在我想找到一种方法来计算每个点的周长。 所以最后我想得到周长的平均值和标准差。

我已经找到了一种方法(在 stackoverflow 上的某个人的帮助下)计算点大小的均值和标准差:

def compute_mean_stddev(contours_of_images):
    for contours_of_image in contours_of_images:
        count = len(contours_of_image)

        sum_list = []
        for cntr in contours_of_image:
            area = cv2.contourArea(cntr) 
            sum_list.append(area) 
            
        average = np.mean(sum_list)
        standard_deviation = np.std(sum_list)    

现在对于该区域,有没有办法获得周长?

【问题讨论】:

  • 你想要什么面积和周长?你会训练神经网络吗?还是其他分类器?

标签: python opencv computer-vision


【解决方案1】:

很好的情况,根据OpenCV documentation,一旦你有了轮廓,你应该能够使用cv.arcLength() 方法计算你想要的东西。

也叫弧长。可以使用 cv.arcLength() 函数找到它。第二个参数指定 shape 是闭合轮廓(如果传递 True),还是只是曲线。

来自官方文档的示例:

    import numpy as np
    import cv2 as cv
    img = cv.imread('star.jpg',0)
    ret, thresh = cv.threshold(img,127,255,0)
    contours, hierarchy = cv.findContours(thresh, 1, 2)
    
    cnt = contours[0]
    area = cv.contourArea()  # Area of first contour
    perimeter = cv.arcLength(cnt, True)  # Perimeter of first contour 

所以在你的情况下你应该更新你的代码如下:

    def compute_mean_stddev(contours_of_images):
        for contours_of_image in contours_of_images:
            count = len(contours_of_image)

            sum_list = []
            for cntr in contours_of_image:
                area = cv2.contourArea(cntr)
                perimeter = cv.arcLength(cntr, True)  
            
            average = np.mean(sum_list)
            standard_deviation = np.std(sum_list) 

我希望这行得通!

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 2020-12-25
    • 2012-11-04
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多