这个
只是 对一张 车牌图片的识别
图片如下:
读取图片 主要使用 的sobel算子 图片的 一个前期 处理
# 转成灰度色彩空间
img = cv.cvtColor(img2, cv.COLOR_BGR2GRAY)
# sobel算子x方向 阈值较低,大部分的边缘和高频都保留下来了
sobel_imgx = cv.Sobel(img, cv.CV_64F, 1, 0, ksize=3)
# sobel算子y方向
sobel_imgy = cv.Sobel(img, cv.CV_64F, 0, 1, ksize=3)
sobel_imgx = cv.convertScaleAbs(sobel_imgx)
sobel_imgy = cv.convertScaleAbs(sobel_imgy)
sobel_img = cv.subtract(sobel_imgx, sobel_imgy)
# 模糊
sobel_img = cv.GaussianBlur(sobel_img,(3,3),0)
# 二值化
# 小于等于 255像素 设为0 其余设为255 白色
# re, sobel_img = cv.threshold(sobel_img, 225, 255, cv.THRESH_BINARY)
# 领域 二值化
b=25
c=10
sobel_img = cv.adaptiveThreshold(sobel_img, 225, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY_INV,b,c)
kernel = cv.getStructuringElement(cv.MORPH_RECT,(21,7))
closed = cv.morphologyEx(sobel_img,cv.MORPH_CLOSE,kernel)
closed = cv.erode(closed, None, iterations = 4) # 腐蚀
closed = cv.dilate(closed, None, iterations = 4) # 膨胀
以检测出 图片的 白色区域 的轮廓 必须经过 二值化 处理
mage,contours,hierarchy = cv.findContours(closed.copy(), cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
c = sorted(contours, key = cv.contourArea, reverse = True)[0]
rect = cv.minAreaRect(c)
box = np.int0(cv.boxPoints(rect))
再 将 车牌 画 框
cv.drawContours(img2, [box], -1, (255, 0, 0), 5)
cv.imshow("Image", img2)
# cv.imshow('img2',box)
cv.waitKey(0)
效果 图
这个 只是 一个 找出 车牌
对 不同 的 图片 需要 选择 最优 算子
这个 是一个 车牌 识别 项目中的一点点
还有 连接 数据库 网上 也有直接 调用 API 的 识别 出车牌 号
这些 都是 很久 之前的 代码