如何解决检测位于图像opencv侧面的对象
我正在尝试检测图像中的多个对象;但是,有些对象位于边缘,因此图像中并未显示所有轮廓。我们如何才能以某种方式检测“被裁剪”的对象?我们可以将轮廓包围在图像的边缘吗?
首先,我模糊了图像,应用了Canny检测器,进行了扩张,然后腐蚀了边缘。
这是我的代码:
img = cv2.imread('porosity1.png')
img1 = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(7,7),0)
med = np.median(blur)
lower = int(max(0,0.7*med))
upper = int(min(255,1.3*med))
edged = cv2.Canny(blur,lower,upper)
edged = cv2.dilate(edged,None,iterations=1)
edged = cv2.erode(edged,iterations=1)
这是我进行边缘检测的结果,对我来说很好。
但是当我要填充轮廓以检查检测器是否能够检测到所有物体(甚至是图像侧面的物体)时,我会得到:
gray,cnts,hierarchy = cv2.findContours(edged,mode = cv2.RETR_CCOMP,method = cv2.CHAIN_APPROX_NONE )
cnts1 = []
external_contours = np.zeros(gray.shape)
for i,cnt in enumerate(cnts):
#External contours
if cv2.contourArea(cnt)>100.0: #To exclude small contour areas
cv2.drawContours(external_contours,i,1,-1)
cnts1.append(cnt)
#Last column in each row in the hierarchy
plt.imshow(external_contours,cmap='gray')
我要检测的原因是我想找到对象的封闭区域。
解决方法
您可以对找到的轮廓使用cv2.convexHull
功能,然后将边缘上的轮廓封闭。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。