【发布时间】:2017-12-14 18:47:17
【问题描述】:
我需要计算钢棒的数量,在Python中使用opencv为下图。
Image with the steel rods at the center 我最初将图像转换为灰度,然后使用 erode 函数和 canny 边缘函数来检测边缘,如下所示。 image after canny edge 后来找到轮廓并使用边界旋转矩形来计算杆数。我无法得到正确的计数。
kernel = np.ones((4,4),np.uint8)
erosion = cv2.erode(gray,kernel,iterations = 1)
cv2.imshow('Erode',erosion)
canny=cv2.Canny(erosion,100,200)
cv2.imshow('Canny',canny)
kernele=np.array([[0,1,1,0],[0,1,1,0],[0,1,1,0],[0,1,1,0]],np.uint8)
dilation = cv2.dilate(canny,kernele,iterations = 1)
cv2.imshow('dilate',dilation)
c,cnt,h=cv2.findContours(dilation,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
if cnt:
i=0
for cnt in contours:
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
if rect[1][0]>1 and rect[1][0]<15: #width of rod
i+=1
请建议修改什么或如何获得计数。 谢谢你
【问题讨论】: