久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何區(qū)分 OpenCV 中的實(shí)心圓/輪廓和空心圓/輪廓

How to distinguish filled circle/contour and unfilled circle/contour in OpenCV?(如何區(qū)分 OpenCV 中的實(shí)心圓/輪廓和空心圓/輪廓?)
本文介紹了如何區(qū)分 OpenCV 中的實(shí)心圓/輪廓和空心圓/輪廓?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我無法區(qū)分以下兩個(gè)輪廓.cv2.contourArea 為兩者提供相同的值.Python中有什么函數(shù)可以區(qū)分它們嗎?

解決方案

為了區(qū)分填充輪廓和未填充輪廓,可以在查找輪廓時(shí)使用輪廓層次結(jié)構(gòu)

最外面的七個(gè)輪廓都是那些沒有父輪廓的輪廓,即那些在其 hierarchy 條目的第四個(gè)字段中具有 -1 值的輪廓.根"之一之下的每個(gè)子節(jié)點(diǎn).表示最外輪廓內(nèi)的輪廓.請(qǐng)注意等高線 13 和 14 如何位于圖中的等高線 12 下方.這兩個(gè)輪廓代表最里面的輪廓,可能是其中一個(gè)點(diǎn)中的噪聲或一些丟失的油漆.一旦我們了解了輪廓是如何排列成層次結(jié)構(gòu)的,我們就可以執(zhí)行更復(fù)雜的任務(wù),例如除了計(jì)算圖像中對(duì)象的數(shù)量之外,還計(jì)算形狀中輪廓的數(shù)量.


回到您的問題,我們可以使用層次結(jié)構(gòu)來區(qū)分內(nèi)輪廓和外輪廓,以確定輪廓是填充還是未填充.我們可以將填充輪廓定義為沒有子元素的輪廓,而將未填充輪廓定義為至少一個(gè)子元素.因此,使用此輸入圖像的屏幕截圖(刪除框):

結(jié)果

代碼

(dǎo)入 cv2將 numpy 導(dǎo)入為 np# 加載圖片,灰度,Otsu的閾值圖像 = cv2.imread('1.png')灰色 = cv2.cvtColor(圖像,cv2.COLOR_BGR2GRAY)thresh = cv2.threshold(灰色, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]# 使用輪廓層次過濾cnts,層次結(jié)構(gòu) = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)層次結(jié)構(gòu)=層次結(jié)構(gòu)[0]對(duì)于 zip 中的組件(cnts,層次結(jié)構(gòu)):currentContour = 組件[0]currentHierarchy = 組件[1]x,y,w,h = cv2.boundingRect(currentContour)# 有內(nèi)部輪廓,這意味著它是未填充的如果當(dāng)前層次結(jié)構(gòu) [3] >0:cv2.putText(圖像,'未填充',(x,y-10),cv2.FONT_HERSHEY_SIMPLEX,0.7,(36,255,12),2)# 沒有孩子,這意味著它已被填滿elif currentHierarchy[2] == -1:cv2.putText(image, 'Filled', (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (36,255,12), 2)cv2.imshow('圖像', 圖像)cv2.waitKey()

I am unable to differentiate the below two contours. cv2.contourArea is giving the same value for both. Is there any function to distinguish them in Python?

解決方案

To distinguish between a filled contour and unfilled contour, you can use contour hierarchy when finding contours with cv2.findContours(). Specifically, you can select the contour retrieval mode to optionally return an output vector containing information about the image topology. There are the four possible modes:

  • cv2.RETR_EXTERNAL - retrieves only the extreme outer contours (no hierarchy)
  • cv2.RETR_LIST - retrieves all of the contours without establishing any hierarchical relationships
  • cv2.RETR_CCOMP - retrieves all of the contours and organizes them into a two-level hierarchy. At the top level, there are external boundaries of the components. At the second level, there are boundaries of the holes. If there is another contour inside a hole of a connected component, it is still put at the top level
  • cv2.RETR_TREE - retrieves all of the contours and reconstructs a full hierarchy of nested contours

Understanding contour hierarchies

So with this information, we can use cv2.RETR_CCOMP or cv2.RETR_TREE to return a hierarchy list. Take for example this image:

When we use the cv2.RETR_TREE parameter, the contours are arranged in a hierarchy, with the outermost contours for each object at the top. Moving down the hierarchy, each new level of contours represents the next innermost contour for each object. In the image above, the contours in the image are colored to represent the hierarchical structure of the returned contours data. The outermost contours are red, and they are at the top of the hierarchy. The next innermost contours -- the dice pips, in this case -- are green.

We can get that information about the contour hierarchies via the hierarchy array from the cv2.findContours function call. Suppose we call the function like this:

(_, contours, hierarchy) = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

The third return value, saved in the hierarchy variable in this code, is a three-dimensional NumPy array, with one row, X columns, and a "depth" of 4. The X columns correspond to the number of contours found by the function. The cv2.RETR_TREE parameter causes the function to find the internal contours as well as the outermost contours for each object. Column zero corresponds to the first contour, column one the second, and so on.

Each of the columns has a four-element array of integers, representing indices of other contours, according to this scheme:

[next, previous, first child, parent]

The next index refers to the next contour in this contour's hierarchy level, while the previous index refers to the previous contour in this contour's hierarchy level. The first child index refers to the first contour that is contained inside this contour. The parent index refers to the contour containing this contour. In all cases, an value of -1 indicates that there is no next, previous, first child, or parent contour, as appropriate. For a more concrete example, here are some example hierarchy values. The values are in square brackets, and the indices of the contours precede each entry. If you printed out the hierarchy array you will get something like this

0:  [ 6 -1  1 -1]   18: [19 -1 -1 17]
1:  [ 2 -1 -1  0]   19: [20 18 -1 17]
2:  [ 3  1 -1  0]   20: [21 19 -1 17]
3:  [ 4  2 -1  0]   21: [22 20 -1 17]
4:  [ 5  3 -1  0]   22: [-1 21 -1 17]
5:  [-1  4 -1  0]   23: [27 17 24 -1]
6:  [11  0  7 -1]   24: [25 -1 -1 23]
7:  [ 8 -1 -1  6]   25: [26 24 -1 23]
8:  [ 9  7 -1  6]   26: [-1 25 -1 23]
9:  [10  8 -1  6]   27: [32 23 28 -1]
10: [-1  9 -1  6]   28: [29 -1 -1 27]
11: [17  6 12 -1]   29: [30 28 -1 27]
12: [15 -1 13 11]   30: [31 29 -1 27]
13: [14 -1 -1 12]   31: [-1 30 -1 27]
14: [-1 13 -1 12]   32: [-1 27 33 -1]
15: [16 12 -1 11]   33: [34 -1 -1 32]
16: [-1 15 -1 11]   34: [35 33 -1 32]
17: [23 11 18 -1]   35: [-1 34 -1 32]

The entry for the first contour is [6, -1, 1, -1]. This represents the first of the outermost contours; note that there is no particular order for the contours, e.g., they are not stored left to right by default. The entry tells us that the next dice outline is the contour with index six, that there is no previous contour in the list, that the first contour inside this one has index one, and that there is no parent for this contour (no contour containing this one). We can visualize the information in the hierarchy array as seven trees, one for each of the dice in the image.

The seven outermost contours are all those that have no parent, i.e., those with an value of -1 in the fourth field of their hierarchy entry. Each of the child nodes beneath one of the "roots" represents a contour inside the outermost contour. Note how contours 13 and 14 are beneath contour 12 in the diagram. These two contours represent the innermost contours, perhaps noise or some lost paint in one of the pips. Once we understand how contours are arranged into a hierarchy, we can perform more sophisticated tasks, such as counting the number of contours within a shape in addition to the number of objects in an image.


Going back to your question, we can use hierarchy to distinguish between inner and outer contours to determine if a contour is filled or unfilled. We can define a filled contour as a contour with no child whereas a unfilled contour as at least one child. So with this screenshot of your input image (removed the box):

Result

Code

import cv2
import numpy as np

# Load image, grayscale, Otsu's threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Filter using contour hierarchy
cnts, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
hierarchy = hierarchy[0]
for component in zip(cnts, hierarchy):
    currentContour = component[0]
    currentHierarchy = component[1]
    x,y,w,h = cv2.boundingRect(currentContour)
    # Has inner contours which means it is unfilled
    if currentHierarchy[3] > 0:
        cv2.putText(image, 'Unfilled', (x,y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (36,255,12), 2)
    # No child which means it is filled
    elif currentHierarchy[2] == -1:
        cv2.putText(image, 'Filled', (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (36,255,12), 2)

cv2.imshow('image', image)
cv2.waitKey()

這篇關(guān)于如何區(qū)分 OpenCV 中的實(shí)心圓/輪廓和空心圓/輪廓?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測(cè)和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測(cè)圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測(cè)圖像中矩形的中心和角度)
主站蜘蛛池模板: 在线观看黄色电影 | 国产午夜精品久久久 | 欧美a级成人淫片免费看 | 欧美成ee人免费视频 | 免费同性女女aaa免费网站 | 91在线资源| 伊人啪啪网| 精品成人一区二区 | 日本一区二区高清视频 | 欧美午夜精品久久久久免费视 | 人人爽人人爽人人片av | 国产色网站 | 在线视频国产一区 | 日韩久久久久 | 青青草网站在线观看 | 国产成人久久精品 | 亚洲免费在线视频 | 国产成人精品一区二区 | 91精品国产综合久久久久久蜜臀 | 国产亚洲一区二区精品 | 在线色网 | h视频在线免费 | 久久免费电影 | 欧美日韩久 | 国产精品久久av | 亚洲成人在线视频播放 | 日本久久久久久 | 日本视频在线播放 | 久久久久久久久久一区 | 亚洲精品视频播放 | 中文字幕第一页在线 | 午夜电影福利 | 日本a视频 | 视频在线一区二区 | 久久精品国产99国产精品 | 日本欧美大片 | 欧美久久久久久久 | 99这里只有精品视频 | av性色全交蜜桃成熟时 | 在线免费亚洲视频 | 欧美日韩在线成人 |