问题描述
主要目的是获取桅杆(如图original image)和相机之间的距离。在 HoughLines 的帮助下,该代码可以很好地检测食品室电线(因为天空是背景)。但是说到桅杆检测,因为有很多物体可以作为背景场景的一部分,我想到了模糊原始图像,应用canny边缘检测,对桅杆的边缘做一些扩张,然后组合后他们,我得到了一个图像,其中桅杆的边缘是可见的以获得轮廓。但是发现轮廓是围绕整个图像边界而不是所需的桅杆(required image)绘制的。我尝试将每个步骤可视化,如 output 所示。如果我认为的方式不正确,有人可以帮助我构建逻辑吗? 以下是我的代码副本:
import cv2
import numpy as np
import imutils
from matplotlib import pyplot as plt
img = cv2.imread('images/Mast4.jpeg',0)
#image before blurring bkgd
edges1 = cv2.Canny(img,70,75)
gray = cv2.GaussianBlur(img,(15,25),0)
#get well-defined borders for the mast
kernel = np.ones((5,5),np.uint8)
img_dilation = cv2.dilate(gray,kernel,iterations=1)
#image after blurring bkgd
edges2 = cv2.Canny(img_dilation,75)
added = img + gray + edges2
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Original Image'),plt.xticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(edges1,cmap = 'gray')
plt.title('Normal edged Image'),plt.yticks([])
plt.figure()
plt.subplot(121),plt.imshow(gray,cmap = 'gray')
plt.title('Blurred Image'),plt.imshow(edges2,cmap = 'gray')
plt.title('Edged Image'),plt.imshow(added,cmap = 'gray')
plt.title('added Image'),plt.yticks([])
#draw contours/ bounding boxes
cnts = cv2.findContours(added,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts,key = cv2.contourArea)
marker = cv2.minAreaRect(c)
box = cv2.cv.BoxPoints(marker) if imutils.is_cv2() else cv2.boxPoints(marker)
box = np.int0(box)
cv2.drawContours(added,[box],-1,(0,255),2)
plt.subplot(122),plt.imshow(added)
plt.title('final Image'),plt.yticks([])
plt.show()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)