从图像中提取轮廓区域



我得到了一些关于轮廓图像分割的问题。例如,我得到了有线电视图像,我可以用阈值和drawcontour函数绘制这张图像的轮廓,代码如下。轮廓图像,阈值图像。我的问题是,我想提取这根电缆并阅读rgb代码。任何建议都可能很棒!谢谢

gray_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
ret, thresh_img = cv2.threshold(gray_image, trs, 255, cv2.THRESH_BINARY)
im2, contours, hierarchy = cv2.findContours(thresh_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im2, contours, -1, red, cnt)
cv2.imshow(winName, im2)

您可以在此处和此处使用cv2.contourArea(contours)了解更多信息

您可以使用鞋带公式获得某个多边形"轮廓"内的区域

想法是通过求和/减去多边形边之间和轴上的面积来递增地计算面积,在通过多边形轮廓的一个完整循环之后,求和/减法的结果将是多边形内的面积

j = numPoints-1       
for (uint_fast8_t i=0; i<numPoints; i++)
{
area = area +  (contour[j][0]+contour[i][0]) * (contour[j][1]-contour[i][1]);
area1 = area1 +  (contour[j][0]*contour[i][1]) - (contour[j][1]*contour[i][0]); //different form for the formula
j = i;  //j is previous vertex to i
}

面积=面积/2;area1=面积1/2//区域标志取决于旋转方向

https://en.wikipedia.org/wiki/Shoelace_formula

https://www.mathopenref.com/coordpolygonarea.html

https://www.mathopenref.com/coordpolygonarea2.html

对于python

https://www.101computing.net/the-shoelace-algorithm/

最新更新