如何继续处理车牌裁剪?

2024-04-18 12:24:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我想预处理专利牌,然后输入OCR

在我这一部分,我必须做一些一般性的事情,因为我只处理一个图像,但是以后它们会更多,并且在不同的角度

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

工作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()

我的输出,我认为这是不好的:

enter image description here

这是图像:

enter image description here

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

enter image description here

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

Description


Tags: 图像cropimportimgthresholdaspltcv2
1条回答
网友
1楼 · 发布于 2024-04-18 12:24:32

我用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)

倾斜车牌:

enter image description here

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

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

相关问题 更多 >