基于OpenCV简单的车牌识别

OpenCV是计算机视觉中经典的专用库,其支持多语言、跨平台,功能强大。

OpenCV-Python为OpenCV提供了Python接口,使得使用者在Python中能够调用C/C++,在保证易读性和运行效率的前提下,实现所需的功能。

OpenCV-Python Tutorials是官方提供的文档,其内容全面、简单易懂,使得初学者能够快速上手使用。

OpenCV-Python Tutorials官方文档:OpenCV: OpenCV-Python Tutorials

学习网址OpenCV中文官方文档

参考上一篇博客下载opencv模块包才能开始使用

代码结构:

0c3a5131dec046f8b5b0060f40fc5f2b.png

 车牌识别代码



import cv2

img = cv2.imread("./img/cp.png")
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("gray", img_gray)
cv2.waitKey(0)

img_thre = img_gray
cv2.threshold(img_gray, 100, 255, cv2.THRESH_BINARY_INV, img_thre)
cv2.imshow("threshold", img_thre)
cv2.waitKey(0)

cv2.imwrite("thre_res.png", img_thre)

white = []
black = []
height = img_thre.shape[0]
width = img_thre.shape[1]
white_max = 0
black_max = 0

for i in range(width):
    s = 0
    t = 0
    for j in range(height):
        if img_thre[j][i] == 255:
            s += 1
        if img_thre[j][i] == 0:
            t += 1
    white_max = max(white_max, s)
    black_max = max(black_max, t)
    white.append(s)
    black.append(t)
    print(s)
    print(t)

arg = False
if black_max > white_max:
    arg = True


def find_end(start_):
    end_ = start_ + 1
    for m in range(start_ + 1, width - 1):
        if (black[m] if arg else white[m]) > (0.95 * black_max if arg
        else 0.95 * white_max):
            end_ = m
            break
    return end_


n = 1
start = 1
end = 2
while n < width - 2:
    n += 1
    if (white[n] if arg else black[n]) > (0.05 * white_max if arg
    else 0.05 * black_max):
        start = n
        end = find_end(start)
        n = end
        if end - start > 5:
            cj = img_thre[1:height, start:end]
            cv2.imshow("caijian", cj)
            cv2.waitKey(0)

查看效果:

4b94d5a98cc04d968930b7efa611f743.png

7cd1d893e8d146939df9b472472e0933.png

8d1f0a0d7e62413bb1b58f9d4111620c.png 

 34a15210926340b08b384ef693278c71.png

 d29a6136b92b4ee6bfe54b79dcbd9e62.png

 99ff8431958d489ba1b12b2275b9550f.png98aa249346314d2d83575f194adf173d.png

98aa249346314d2d83575f194adf173d.png

 6bee0e55fbdf4c17a4faed5af2878724.png

98aa249346314d2d83575f194adf173d.png

 可以全提取出来

代码精度不高可以自己研究不断完善

猜你喜欢

转载自blog.csdn.net/qq_46654604/article/details/127354251