从图像中提取轮廓区域

时间:2018-08-01 09:00:12

标签: python python-3.x opencv image-processing opencv-contour

我对轮廓图像的分割有一些疑问。例如,我得到了cable image,并且可以使用下面的代码使用阈值和drawcontour函数对该图像进行轮廓处理。 Contoured imagethreshold image。我的问题是我要提取这根电缆并阅读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)

2 个答案:

答案 0 :(得分:0)

您可以使用cv2.contourArea(contours)的更多信息herehere

答案 1 :(得分:0)

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

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

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
      }

area =面积/ 2; 区域1 =区域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/

相关问题