如何将空心形状视为一个对象而不是两个物体?
一个空心的形状,以下面的情况为例,被检测为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:
Second Image (Result from my code):
Expected Result:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了解决您的问题,
flag
的选择很重要。我们正在处理一种在另一个轮廓内放置轮廓的情况。我们可以在查找轮廓cv2.findcontours()
时使用rever_ccomp
标志。并分析层次结构输出。Into
在以下二进制图像中,我们有一个对象,但是在找到轮廓时,我们最终以2个对象(含义2个不同的对象)。
-1
0
我们有兴趣仅找到每个不同对象的外界。在上图中,这仅是轮廓1。
代码:
结果:
观察<<<<代码>层次结构变量给出了一个数组,每行分配给每个轮廓:
根据文档,第四列指示亲子关系。
如果值为:
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 ofRETR_CCOMP
flag while finding contourscv2.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).
-1
0
We are interested in finding only the outer boundary of every distinct object. In the image above, that would be only Contour 1.
Code:
Result:
Details:
Observing the
hierarchy
variable gives an array, with each row assigned to each contour:According to the documentation, the 4th column indicates the parent-child relationship.
If the value is: