【问题标题】:Extract contour area from image从图像中提取轮廓区域
【发布时间】:2018-08-01 09:00:11
【问题描述】:

我有一些关于轮廓图像分割的问题。例如我得到了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)

【问题讨论】:

  • 您能说明一下您要提取的区域吗?
  • @littlecat 我需要通过分离图像中的电缆来读取颜色。

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    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

    对于蟒蛇

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

    【讨论】:

    • 请在您的答案中发布更多链接,并解释其中的内容,以防这些链接将来不再有效。
    猜你喜欢
    • 2013-12-17
    • 2020-03-29
    • 2018-07-13
    • 2012-11-15
    • 2022-01-26
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多