簡體   English   中英

檢測 opencv 中的嵌套形狀

[英]Detecting Nested Shape in opencv

我需要檢測 opencv 中的嵌套形狀,但似乎即使使用層次結構我也可以做到


edges = cv2.Canny(img,50,200)
contours,hierarchy = cv2.findContours(edges, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

hierarchy = hierarchy[0] # get the actual inner list of hierarchy descriptions


for comp in contours:

currentCont = comp[0]
currentHie = hierarchy[1]

x,y,w,h = cv2.boundingRect(currentContour)
  if ( currentHierarchy[1] < 0 ) 
    these are the innermost child components
    cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),3)
    cv2.putText(img, "nes", (x, y), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0), 2)
    cv2.drawContours(img, [currentCont], -1, (0, 255, 0), 1)
    cv2.waitKey()
    cv2.imshow("image",img)

cv2.waitKey(0)
cv2.destroyAllWindows()

我將向您展示如何找到嵌套的最內部輪廓。
我認為有比您的樣本更復雜的結構,您必須對其進行分類。

  • 我建議不要使用cv2.Canny ,因為它會創建更多的層次結構。
    看看使用cv2.imshow("edges", edges)cv2.waitKey()
    您可能只是反轉極性以獲得黑色背景上的白色三角形。

     img = 255 - img
  • 有一個小錯誤(在循環內):
    您正在使用: currentHierarchy = hierarchy[1]
    而不是: currentHierarchy = component[1]

查找嵌套輪廓:

  • 從最內部的輪廓開始。
    最內部的輪廓是沒有“第一個孩子”的輪廓。
    檢查currentHierarchy[2] < 0是否。

  • 所有內部輪廓都將有一個父輪廓。
    原因是每個三角形應用兩個(嵌套)輪廓:

    • 三角形內邊緣的內輪廓。
    • 三角形外邊緣的外輪廓。

    我們需要獲取父輪廓,並檢查父輪廓是否有父輪廓。
    具有祖父母的內部三角形是嵌套三角形。

這是完整的代碼示例:

import numpy as np
import cv2

img = cv2.imread('triangles.png', cv2.IMREAD_GRAYSCALE)  # Read input image as grayscale,

# edges = cv2.Canny(img, 50, 200)

# Invert polarity of img, instead of using Canny - we need the contours to be white.
img = 255 - img

contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

hierarchy = hierarchy[0] # get the actual inner list of hierarchy descriptions

img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) # Convert image to BGR, for using colored text.

for component in zip(contours, hierarchy):
    currentContour = component[0]
    #currentHierarchy = hierarchy[1] # Why ?
    currentHierarchy = component[1]

    approx = cv2.approxPolyDP(currentContour, 0.01 * cv2.arcLength(currentContour, True), True)
    x = approx.ravel()[0]
    y = approx.ravel()[1] - 5

    # https://docs.opencv.org/master/d9/d8b/tutorial_py_contours_hierarchy.html
    # Hierarchy Representation in OpenCV
    # Each contour has its own information regarding what hierarchy it is, who is its child, who is its parent etc. 
    # OpenCV represents it as an array of four values : [Next, Previous, First_Child, Parent]

    #if (currentHierarchy[1] < 0) and len(approx) == 3:

    if (currentHierarchy[2] < 0) and len(approx) == 3:
        # These are the innermost child components

        parent_idx = currentHierarchy[3]  # Get the index of the parent contour
        parent_hier = hierarchy[parent_idx]  # Get the hierarchy of the parent.

        if parent_hier[3] >= 0:
            # Contour is nested only if the parent has a parent.
            cv2.putText(img, "Nested", (x, y), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 0, 0), 2)
            cv2.drawContours(img, [currentContour], -1, (0, 255, 0), 1)
            cv2.waitKey(1000)
            cv2.imshow("image", img)

cv2.waitKey()
cv2.destroyAllWindows()

結果:
在此處輸入圖像描述

暫無
暫無

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

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