How to continue processing the license plate crop?

Viewed 872

I want to pre-process the patent plate and then enter an OCR.

In the part that I am, I have to do things in general, because I am working only with one image, but later they will be more and at different angles.

I am in the part where I insert filters, I wonder if the next part is to find the contours or straighten it (for this I am using hough transform).

Working 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()

The output I have, which I think this is bad:

enter image description here

And this is the image:

enter image description here

And this is the output when I rotate the image with HoughTransform:

enter image description here

The final result should be something like this (but remember that I will use the same preprocessing for other images):

Description

1 Answers

I wrote a script in python to find the angle in which the license plate is rotated with and then rotating in the reverse order to deskew the license plate.

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)

Deskewed License Plate:

enter image description here

You can apply some post-processing to completely remove the padded region, but the angle correction is the most important part for any detector.

Gist link: https://gist.github.com/zabir-nabil/dfb78f584947ebdb9f29d39c9737b5c6

Related