vs2017+opencv4.0+python+车牌图像处理与定位
学习方法参考:https://gitchat.csdn.net/activity/59fe7550d343ff71a3c8620f?utm_source=so
https://blog.csdn.net/weixin_41695564/article/details/79712393
import cv2
rawImage = cv2.imread(“图片地址”)//读取图片
参考:https://blog.csdn.net/zhang_cherry/article/details/88951259
1)高斯模糊:去除部分的干扰,将图片平滑化,让识别更加准确。
PS:所谓"模糊",可以理解成每一个像素都取周边像素的平均值。
image = cv2.GaussianBlur(rawImage,(3,3),0)
2)灰度化:将彩色图像转化成为灰度图像。
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
3)Sobel算子:图像边缘检测
Sobel_x = cv2.Sobel(image, cv2.CV_16S, 1, 0)
absX = cv2.convertScaleAbs(Sobel_x)
image = absX
参考:https://blog.csdn.net/YanMY2012/article/details/8110316
4)二值化:将图像上的像素点的灰度值设置为0或255,也就是呈现的只有黑白两种颜色(函数的作用是将一幅灰度图二值化)
ret, image = cv2.threshold(image, 0, 255, cv2.THRESH_OTSU)
参考:https://blog.csdn.net/kwame211/article/details/86307946
5)闭操作:可以将目标区域连成一个整体,便于后续轮廓的提取
kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (17, 5))
PS:开运算(Opening Operation),其实就是先腐蚀后膨胀的过程;先膨胀后腐蚀的过程称为闭运算。
image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernelX)
参考:https://blog.csdn.net/poem_qianmo/article/details/24599073
6)膨胀腐蚀:通过膨胀连接相近的图像区域,通过腐蚀去除孤立细小的色块
kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 1))
kernelY = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 19))
image = cv2.dilate(image, kernelX)
image = cv2.erode(image, kernelX)
image = cv2.erode(image, kernelY)
image = cv2.dilate(image, kernelY)
参考:https://blog.csdn.net/poem_qianmo/article/details/23710721
7)中值滤波:去除图像或者其他信号中的噪声
image = cv2.medianBlur(image, 15)
8)查找轮廓:
tmp, contours, w1 = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
PS:但是该函数返回的三个参数具体表示的是什么呢?
网上是旧的代码版本,有三个返回值,新的j有两个。
旧:image,contours ,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
PS:删除第一个返回值image即可!!!!!
参考:https://blog.csdn.net/songjinxaing/article/details/79842474
https://blog.csdn.net/jjddss/article/details/73527990
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, 255, 255), 3)
cv2.imshow(‘image’, image)
cv2.waitKey(0)
cv2.destroyAllWindows()
参考:https://blog.csdn.net/wsp_1138886114/article/details/82945328