基于边缘提取的车牌识别
步骤
- 高斯模糊
- 边缘提取
- 膨胀腐蚀
- 中值滤波
- 查找轮廓
- 判断车牌区域
import cv2
rawImage = cv2.imread("car2.jpg")
image = cv2.GaussianBlur(rawImage, (3, 3), 0)
# 闭操作:闭操作可以将目标区域连成一个整体,便于后续轮廓的提取。(作用不明显)
# kernelX1 = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 2))
# image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernelX1)
# cv2.imshow('00', image)
image_Canny = cv2.Canny(image,50,100)
cv2.imshow('01', image_Canny)
kernalX = cv2.getStructuringElement(cv2.MORPH_RECT, (23, 1))
kernalY = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 19))
image_Canny = cv2.dilate(image_Canny, kernalX)
image_Canny = cv2.erode(image_Canny, kernalX)
image_Canny = cv2.dilate(image_Canny, kernalX, iterations=3)
image_Canny = cv2.erode(image_Canny, kernalX, iterations=3)
image_Canny = cv2.dilate(image_Canny, kernalY)
image_Canny = cv2.erode(image_Canny, kernalY, iterations=3)
cv2.imshow('02', image_Canny)
image_blur = cv2.medianBlur(image_Canny,15)
cv2.imshow('image_blur',image_blur)
contours0, hierarchy0 = cv2.findContours(image_blur, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for item in contours0:
rect = cv2.boundingRect(item)
x = rect[0]
y = rect[1]
weight = rect[2]
height = rect[3]
if weight > (height * 2.5):
chepai = rawImage[y:y + height, x:x + weight]
cv2.imshow('chepai'+str(x), chepai)
image = cv2.drawContours(rawImage, contours0, -1, (0, 0, 255), 3)
cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()