Nad*_*Lnk 6 opencv image-processing javacv
如何识别另一个轮廓内的轮廓?我试图通过许多OpenCV教程,但我无法识别它.请一些专家提供简单的代码来解释一下吗?
这是我的输入文件

这个黑暗的部分是我需要识别的轮廓.

请善意与我分享您的经验.
这是 Python 代码中的一个简单方法。( But as I stated in my comment below, It is not an universal answer applicable everywhere, It is just to show finding contour inside a contour can be done. And if your images are different, then you have to come with some different constraints other than i mentioned below)
找到轮廓后你要做的是,检查每个轮廓的面积是否小于指定值(我给了10000,只是猜测),如果不是,则它是更大的轮廓,避免它。如果小于指定值,它旁边可能是我们的正方形或矩形。
所以我们找到它的宽度和高度,并检查长宽比是否接近 1。如果是,那么它就是我们的正方形。
import cv2
import numpy as np
img = cv2.imread('sofcnt.jpg')
gray = cv2.imread('sofcnt.jpg',0)
ret,thresh = cv2.threshold(gray,127,255,1)
cont,hier = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
for cnt in cont:
approx = cv2.approxPolyDP(cnt,0.02*cv2.arcLength(cnt,True),True)
if cv2.contourArea(cnt) < 10000:
x,y,w,h = cv2.boundingRect(cnt)
if w/float(h) < 2:
cv2.drawContours(img,[cnt],0,255,-1)
cv2.imshow('a',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
结果 :
