基于Sobel算子的车牌识别
步骤如下
- 高斯模糊
- 图片灰度化
- Sobel算子
- 图像二值化
- 闭操作
- 膨胀腐蚀
- 中值滤波
- 查找轮廓
- 判断车牌区域
import cv2
# 读取图片
rawImage = cv2.imread("car1.jpg")
# 高斯模糊,将图片平滑化,去掉干扰的噪声
image = cv2.GaussianBlur(rawImage, (3, 3), 0)
# 图片灰度化
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# Sobel算子(X方向)
Sobel_x = cv2.Sobel(image, cv2.CV_16S, 1, 0, ksize=5)
# Sobel_y = cv2.Sobel(image, cv2.CV_16S, 0, 1, ksize=5)
absX = cv2.convertScaleAbs(Sobel_x) # 转回uint8
# absY = cv2.convertScaleAbs(Sobel_y)
# dst = cv2.addWeighted(absX, 0.8, absY, 0.2, 0)
image = absX
cv2.imshow('0', image)
# 二值化:图像的二值化,就是将图像上的像素点的灰度值设置为0或255,图像呈现出明显的只有黑和白
ret, image = cv2.threshold(image, 0, 255, cv2.THRESH_OTSU)
cv2.imshow('1', image)
# 闭操作:闭操作可以将目标区域连成一个整体,便于后续轮廓的提取。
kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (17, 5))
image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernelX)
cv2.imshow('2', image)
# 膨胀腐蚀(形态学处理)
kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 1))
kernelY = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 19))
image = cv2.dilate(image, kernelX, iterations=1)
image = cv2.erode(image, kernelX, iterations=2)
image = cv2.erode(image, kernelY, iterations=2)
image = cv2.dilate(image, kernelY, iterations=1)
cv2.imshow('3', image)
# 平滑处理,中值滤波
image = cv2.medianBlur(image, 15)
cv2.imshow('4', image)
# 查找轮廓
contours, hierarchy = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for item in contours:
rect = cv2.boundingRect(item)
x = rect[0]
y = rect[1]
weight = rect[2]
height = rect[3]
if weight > (height * 2):
# 裁剪区域图片
chepai = rawImage[y:y + height, x:x + weight]
cv2.imshow('chepai'+str(x), chepai)
# 绘制轮廓
image = cv2.drawContours(rawImage, contours, -1, (0, 0, 255), 3)
cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()