美文网首页
[py055] 实战OpenCV车牌识别

[py055] 实战OpenCV车牌识别

作者: Andy计算机专业 | 来源:发表于2020-08-29 18:28 被阅读0次

    目录:

    1.读取图像
    2.图像预处理(降噪)
    3.定位到ROI(感兴趣区域)图像
    4.对ROI图像进行车牌号文字识别提取

    # 准备工作:导入相关库、定义需全局使用的函数、设置相关参数
    import cv2
    import imutils
    import numpy as np
    import pytesseract
    import  re
    from matplotlib import pyplot as plt
    # %matplotlib inline
    
    def imshow(img):
        img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        plt.imshow(img_rgb)
    

    1.读取图像

    img = cv2.imread('LicensePlate001.jpg',cv2.IMREAD_COLOR)
    img = cv2.resize(img, (600,400))
    imshow(img)
    

    2.图像预处理(降噪)

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
    # 使用双边滤波(模糊)从图像中删除不需要的细节。
    # gray = cv2.bilateralFilter(gray, 13, 15, 15)
    imshow(gray)
    

    3.定位到ROI(感兴趣区域)图像

    # 3-1.边缘检测
    edged = cv2.Canny(gray, 30, 200) 
    imshow(edged)
    
    # 3-2.在图像上寻找轮廓,并定位ROI
    contours = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    contours = imutils.grab_contours(contours)
    contours = sorted(contours, key = cv2.contourArea, reverse = True)[:10]
    ROI = None
    for c in contours:
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.018 * peri, True)
        if len(approx) == 4:
            ROI = approx
            cv2.drawContours(img, [ROI], -1, (0, 0, 255), 2)
            break
    imshow(img)
    
    # 3-3.从主图像裁剪出ROI(感兴趣区域)图像
    y0,y1 = np.min(ROI[:,:,1]),np.max(ROI[:,:,1])
    x0,x1 = np.min(ROI[:,:,0]),np.max(ROI[:,:,0])
    Cropped = gray[y0:y1,x0:x1]
    imshow(Cropped)
    

    4.对ROI图像进行车牌号文字识别提取

    text = pytesseract.image_to_string(Cropped)
    text = re.sub('[^a-zA-Z\d ]','',text)
    img2 = img.copy()
    cv2.putText(img2,text,(x0-50,y0),cv2.FONT_HERSHEY_SIMPLEX,1.2, (255, 255, 255), 2)
    imshow(img2)
    

    附1:Tesseract文字识别可参考https://www.jianshu.com/p/24352fead613

    最后

    [1].代码截止2020-08-29调试无误。
    [2].如需全部代码及相关文件,留言邮箱。
    [3].过程中有任何问题,欢迎交流!Q597966823

      让知识或技术实现其最大的价值,欢迎收藏自用、转载分享,转载请注明原文出处,谢谢!

    相关文章

      网友评论

          本文标题:[py055] 实战OpenCV车牌识别

          本文链接:https://www.haomeiwen.com/subject/fpejsktx.html