【问题标题】:How to continue processing the license plate crop?如何继续处理车牌裁剪?
【发布时间】:2020-05-14 15:09:55
【问题描述】:

我想对专利牌进行预处理,然后输入 OCR。

在我的角色中,我必须做一般性的事情,因为我只处理一张图像,但以后它们会更多,角度不同。

我在插入过滤器的部分,我想知道下一部分是找到轮廓还是拉直它(为此我使用霍夫变换)。

工作on colab:

!pip install pytesseract
import cv2
import numpy as np
import matplotlib.pyplot as plt
import pytesseract
plt.style.use('dark_background')

crop_img = cv2.imread('/content/0.png')

#IMG2GRAY
gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
plt.imshow(gray)

#tresh
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
plt.imshow(thresh)

# Otsu's thresholding after Gaussian filtering
blur = cv2.GaussianBlur(thresh,(5,5),0)
th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

plt.imshow(th3)
plt.show()

我的输出,我认为这很糟糕:

这是图片:

这是我使用 HoughTransform 旋转图像时的输出:

最终结果应该是这样的(但请记住,我将对其他图像使用相同的预处理):

【问题讨论】:

    标签: python python-3.x opencv image-processing


    【解决方案1】:

    我在 python 中编写了一个脚本来查找车牌旋转的角度,然后以相反的顺序旋转以歪斜车牌。

    import numpy as np
    import math
    import cv2
    
    def rotate_image(image, angle):
        image_center = tuple(np.array(image.shape[1::-1]) / 2)
        rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
        result = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR)
        return result
    
    def compute_skew(src_img):
    
        if len(src_img.shape) == 3:
            h, w, _ = src_img.shape
        elif len(src_img.shape) == 2:
            h, w = src_img.shape
        else:
            print('upsupported image type')
    
        img = cv2.medianBlur(src_img, 3)
    
        edges = cv2.Canny(img,  threshold1 = 30,  threshold2 = 100, apertureSize = 3, L2gradient = True)
        lines = cv2.HoughLinesP(edges, 1, math.pi/180, 30, minLineLength=w / 4.0, maxLineGap=h/4.0)
        angle = 0.0
        nlines = lines.size
    
        #print(nlines)
        cnt = 0
        for x1, y1, x2, y2 in lines[0]:
            ang = np.arctan2(y2 - y1, x2 - x1)
            #print(ang)
            if math.fabs(ang) <= 30: # excluding extreme rotations
                angle += ang
                cnt += 1
    
        if cnt == 0:
            return 0.0
        return (angle / cnt)*180/math.pi
    
    def deskew(src_img):
        return rotate_image(src_img, compute_skew(src_img))
    
    
    if __name__ == '__main__':
        import cv2
        img = cv2.imread('test.png')
        corrected_img = deskew(img)
    

    车牌偏斜:

    您可以应用一些后处理来完全去除填充区域,但角度校正是任何探测器最重要的部分。

    要点链接:https://gist.github.com/zabir-nabil/dfb78f584947ebdb9f29d39c9737b5c6

    【讨论】:

    • 谢谢,非常有用。您知道下一步将如何将尽可能好的图像提供给 OCR 吗?
    • 简单的轮换是一个好的开始。您还可以找到板的四个角,将它们映射到一个您知道代表正确纵横比的矩形,然后计算为您提供该映射的仿射变换。然后你就可以真正处理歪斜和其他伪影了。
    猜你喜欢
    • 2020-06-25
    • 2021-09-26
    • 1970-01-01
    • 2020-05-22
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    相关资源
    最近更新 更多