[英]Extract contour area from image
我有一些关于轮廓图像分割的问题。 例如,我得到了电缆图像,我可以使用阈值和 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)
您可以使用鞋带公式获得某些多边形“轮廓”内的区域
想法是通过求和/减去多边形边之间和轴上的区域来逐步计算面积,经过多边形轮廓的一个完整循环后,求和/减法的结果将是多边形内的区域
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; 面积1=面积1/2; //面积符号取决于旋转方向
https://en.wikipedia.org/wiki/Shoelace_formula
https://www.mathopenref.com/coordpolygonarea.html
https://www.mathopenref.com/coordpolygonarea2.html
蟒蛇
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.