gpt4 book ai didi

python - 计算等高线的周长(OpenCV、Python)

转载 作者:行者123 更新时间:2023-12-02 18:25:19 24 4
gpt4 key购买 nike

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

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

此处用于颜色 channel 青色。

enter image description here

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

我已经找到了一种方法(在 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)

现在对于面积,有没有办法得到周长?

最佳答案

根据OpenCV documentation,情况不错一旦你有了轮廓,你应该能够使用 cv.arcLength() 方法计算出你想要的内容。

It is also called arc length. It can be found out using cv.arcLength() function. Second argument specify whether shape is a closed contour (if passed True), or just a curve.

官方文档示例:

    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)

希望这能奏效!

关于python - 计算等高线的周长(OpenCV、Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70304679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com