如何将空心形状视为一个对象而不是两个物体?

发布于 2025-01-26 22:55:31 字数 1433 浏览 2 评论 0原文

一个空心的形状,以下面的情况为例,被检测为2个对象,外三角(可以将其视为一个轮廓)以某种方式被检测为第二张图像中所示的2个对象(2个绿色轮廓(2个绿色轮廓)外部黑色三角形)。

第一个图像:

”“在此处输入图像描述”

第二张图像(我的代码结果):

​strong>

我的代码:

import os
import cv2
import numpy as np

file_name = os.path.join(os.path.dirname(__file__),'hollowtri.png')
assert os.path.exists(file_name)
a = cv2.imread(file_name)

#Convert image to gray image
imgGray = cv2.cvtColor(a,cv2.COLOR_BGR2GRAY)

#Doing threshold on the image
_,thresh = cv2.threshold(imgGray,100,255,cv2.THRESH_BINARY_INV)
#Finding objects
contours,_ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
#Print the result based on the contours found
num = 0
for contour in contours:
    cv2.drawContours(a,contours,num,(0,255,0),2)
    num+=1

cv2.imshow("Thresh", thresh)
cv2.imshow("Triangle", a)
cv2.waitKey(0)

我不想使用cv2.retr_external,因为我希望检测到内部的固体三角形,所以是否有解决此问题的解决方案?我希望将外三角仅视为1个对象。

注意:只有黑色的形状才是相关的对象。因此,结果应仅包含2个对象,但是在图像2中,外三角在绿色的内部和外部两次概述,我的目标是仅概述一次,因为一个空心三角形只是1个对象,而不是2个。

A shape that is hollow, take the below situation as example, is detected as 2 objects, the outer triangle(can be seen as just an outline) is somehow detected as 2 objects as shown in the second image (2 green outline in and outside of the outer black triangle).

First Image:

enter image description here

Second Image (Result from my code):

enter image description here

Expected Result:

enter image description here

My Code:

import os
import cv2
import numpy as np

file_name = os.path.join(os.path.dirname(__file__),'hollowtri.png')
assert os.path.exists(file_name)
a = cv2.imread(file_name)

#Convert image to gray image
imgGray = cv2.cvtColor(a,cv2.COLOR_BGR2GRAY)

#Doing threshold on the image
_,thresh = cv2.threshold(imgGray,100,255,cv2.THRESH_BINARY_INV)
#Finding objects
contours,_ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
#Print the result based on the contours found
num = 0
for contour in contours:
    cv2.drawContours(a,contours,num,(0,255,0),2)
    num+=1

cv2.imshow("Thresh", thresh)
cv2.imshow("Triangle", a)
cv2.waitKey(0)

I don't want to use cv2.RETR_EXTERNAL because I want the inner solid triangle to be detected, so is there a solution to this problem? I want the outer triangle to be detected as just 1 object.

Note: Only the shape colored in black are the object of concerned. So the result should contain only 2 objects, but in image 2, the outer triangle is outlined in green twice, inside and outside, my goal is to outline it just once, because a hollow triangle is just 1 object, not 2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昨迟人 2025-02-02 22:55:31

为了解决您的问题,flag的选择很重要。我们正在处理一种在另一个轮廓内放置轮廓的情况。我们可以在查找轮廓cv2.findcontours()时使用rever_ccomp标志。并分析层次结构输出。

Into

在以下二进制图像中,我们有一个对象,但是在找到轮廓时,我们最终以2个对象(含义2个不同的对象)。

  • 轮廓1(指向红色)是对象的外边界 - 这是 parent ,值-1
  • 轮廓2(指向绿色)是对象 - 这是 child 带有值0

我们有兴趣仅找到每个不同对象的外界。在上图中,这仅是轮廓1。

代码:

img = cv2.imread(r'C:\Users\524316\Desktop\Stack\tri.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th = cv2.threshold(gray,100,255,cv2.THRESH_BINARY_INV)[1]

# using cv2.RETR_CCOMP flag
contours,hierarchy = cv2.findContours(th,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)

# initializing an index list
contour_index_list = []

# draw every contour whose 4th column in hierarch array is -1
for index in range(0, len(contours)):
    if (hierarchy[:,index][0][3] == -1):
        contour_index_list.append(index)
        cv2.drawContours(img, [contours[index]], 0, (0, 255, 0), 3)

结果:

“在此处输入图像描述”

详细信息:

观察<<<<代码>层次结构变量给出了一个数组,每行分配给每个轮廓:

array([[[ 1, -1, -1, -1],
        [-1,  0,  2, -1],
        [-1, -1, -1,  1]]], dtype=int32)

根据文档,第四列指示亲子关系。
如果值为:

  • -1轮廓是父
  • 0,轮廓是一个孩子

To solve your problem the choice of flag is important. We are dealing with a situation where there is a contour placed within another contour. We can make use of RETR_CCOMP flag while finding contours cv2.findContours(). And analyze the hierarchy output.

Intro

In the following binary image we have one object, but while finding contours we end up with 2 (meaning 2 distinct objects).

  • Contour 1 (pointed in red) is the outer boundary of the object - this is the parent with value -1
  • Contour 2 (pointed in green) is the inner boundary of the object - this is the child with value 0

enter image description here

We are interested in finding only the outer boundary of every distinct object. In the image above, that would be only Contour 1.

Code:

img = cv2.imread(r'C:\Users\524316\Desktop\Stack\tri.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th = cv2.threshold(gray,100,255,cv2.THRESH_BINARY_INV)[1]

# using cv2.RETR_CCOMP flag
contours,hierarchy = cv2.findContours(th,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)

# initializing an index list
contour_index_list = []

# draw every contour whose 4th column in hierarch array is -1
for index in range(0, len(contours)):
    if (hierarchy[:,index][0][3] == -1):
        contour_index_list.append(index)
        cv2.drawContours(img, [contours[index]], 0, (0, 255, 0), 3)

Result:

enter image description here

Details:

Observing the hierarchy variable gives an array, with each row assigned to each contour:

array([[[ 1, -1, -1, -1],
        [-1,  0,  2, -1],
        [-1, -1, -1,  1]]], dtype=int32)

According to the documentation, the 4th column indicates the parent-child relationship.
If the value is:

  • -1 the contour is the parent
  • 0 the contour is a child
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文