如何使用OpenCV和python指定图像中的轮廓?

2024-04-23 21:23:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Opencv和Python检测汽车牌照上的文字,但遇到了一个问题,canny Edge检测器工作得很好,也许这是基于这个问题,但在指定轮廓时,它不起作用,我看到许多其他人在同一个模型上工作,并且做得足够好。 这是密码

bfilter = cv2.bilateralFilter(gray, 17, 17, 17) # noise reduction
edged = cv2.Canny(bfilter, 50, 150) # edge detection
plt.imshow(cv2.cvtColor(edged, cv2.COLOR_BGR2RGB))

keypoints = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(keypoints)
contours = sorted(contours, key=cv2. contourArea, reverse=True)[:30]
location = None
screenCnt = None

for contours in contours:
    approx = cv2.approxPolyDP(contours, 0.0.1* perimeter, True)
    if len(approx)== 4:
        location = approx
        break

mask = np.zeros(gray.shape,np.uint8)
new_image = cv2.drawContours(mask, [location], 0, (0,255,0), 3)
new_image = cv2.bitwise_and(img,img,mask=mask)
plt.imshow(cv2.cvtColor(new_image, cv2.COLOR_BGR2RGB))

(x, y) = np.where(mask == 255)
(topx, topy) = (np.min(x), np.min(y))
(bottomx, bottomy) = (np.max(x), np.max(y))`
Cropped = gray[topx:bottomx+1, topy:bottomy+1]
plt.imshow(cv2.cvtColor(Croped_image, cv2.COLOR_BGR2RGB))

现在它显示的是黑色填充的图像,而不是交叉的图像。 帮我摆脱这个问题

enter image description here