标题相当混乱,因此我将在下面解释我的问题:
我想用OpenCV和Python检测车牌,下面是我的代码:
img = cv2.imread('C:/Users/ACER/Downloads/BikePlate3K/plate_train_2K/image_1703.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #Grayscale
kernel = np.ones((5,5),np.uint8)
# Dilation
dilation = cv2.dilate(gray,kernel,iterations = 1)
#Edge detection
edges = cv2.Canny(dilation, 120, 560, 1) #120 255
squareKern = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
light = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, squareKern)
light = cv2.threshold(light, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
thresh = cv2.bitwise_and(edges, edges, mask=light)
rectKern = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 3))
closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, rectKern)
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
x, y, w, h = cv2.boundingRect(c)
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.imshow("Dilation", dilation)
cv2.imshow("Edges", edges)
cv2.imshow("Image", img)
cv2.waitKey(0)
这是我画出每个检测到的轮廓后的结果
所以问题是,我怎么才能只画一个正确的矩形(限制车牌)
(我提出了一些解决方案,比如找到最大的轮廓,但并不总是返回正确的轮廓)
目前没有回答
相关问题 更多 >
编程相关推荐