簡體   English   中英

從圖像中提取輪廓區域

[英]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)

您可以在此處此處使用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; 面積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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM