Hai*_*zai 2 python opencv computer-vision
我这里有这张图像,其中有很多青色、品红色和黄色的小打印机点。
分离颜色通道 (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)
Run Code Online (Sandbox Code Playgroud)
现在对于面积来说,有没有办法得到周长?
很好的例子,根据OpenCV 文档,一旦你有了轮廓,你应该能够使用cv.arcLength()
方法计算你想要的东西。
也称为弧长。可以使用 cv.arcLength() 函数找到。第二个参数指定形状是闭合轮廓(如果传递 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
Run Code Online (Sandbox Code Playgroud)
因此,在您的情况下,您应该按如下方式更新代码:
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)
Run Code Online (Sandbox Code Playgroud)
我希望这能起作用!