preface

Hello! No good wine, a little long holiday, ha ha ha, today normal more learning materials ~

License plate recognition has a wide range of applications in the highway, such as our common electronic toll collection (ETC) system and traffic illegal vehicles

Testing, among other things

Like residential or underground garage access control will also be used, basically all the need for vehicle identity detection will be used.

Today xiaobian will teach you to write a simple license plate detection system, I hope you like the little attention ~ MUA!

The body of the

1) Development tools

Python3.6, Pycharm, Opencv, etc.

PIP install -i https://pypi.douban.com/simple/ + module nameCopy the code

2) Principle introduction

Note that this is not license plate recognition, it is license plate detection.

Because the shape of the license plate is relatively simple, I realized it by referring to some simple traditional algorithms without using deep learning. Effect comparison

In general. It is not suitable for license plate detection in complex environment.

Direct tuning of OpenCV function interface, not from 0 to achieve, so the overall technical content is low.

3) The process is as follows

Step1:

Image pretreatment, including grayscale, Gaussian smoothing, median filtering, Sobel operator edge detection and so on.

Step2:

OpenCV is used to find the profile of the preprocessed image, and then judge whether the profile is the license plate profile according to some parameters.

Using the demonstration

Run the detect.py file in a CMD window.

Please specify the image path to be detected before using:

​​

4) Source code display

File: Charles import cv2 import numpy as np # GaussianBlur(img, (3, 3), 0, 0, cv2.border_default) # Median filter = cv2.medianblur (Gaussian, 5) # Sobel operator # gradient direction: X sobel = cv2.sobel (median, cv2.cv_8u, 1, 0, ksize=3) # ret, binary = cv2.threshold(sobel, 170, 255, median, cv2.cv_8u, 1, 0, ksize=3) # ret, binary = cv2.threshold(sobel, 170, 255, 0) Cv2. # THRESH_BINARY) kernel function element1 = cv2. GetStructuringElement (cv2 MORPH_RECT, (9, 1) element2 = cv2. GetStructuringElement (cv2 MORPH_RECT, (9, 7)) # expansion dilation = cv2. Dilate (binary, element2. Iterations =1) # erosion = cv2. Iterations (erodes, element1, iterations=1) # erosion = cv2. iterations=3) return dilation2 def GetRegion(img): Hierarchy = cv2.findContours(img, cv2.retr_tree, cv2.CHAIN_APPROX_SIMPLE) for contour in contours: area = cv2.contourArea(contour) if (area < 2000): continue eps = 1e-3 * cv2.arcLength(contour, True) approx = cv2.approxPolyDP(contour, eps, True) rect = cv2.minAreaRect(contour) box = cv2.boxPoints(rect) box = np.int0(box) height = abs(box[0][1] - box[2][1]) Width = abs(box[0][0] -box [2][0]) ratio =float(width)/float(height) if (ratio < 5 and ratio > 1.8): regions.append(box) return regions def detect(img): # gray = cv2.cvtcolor (img, cv2.COLOR_BGR2GRAY) prc = Process(gray) regions = GetRegion(prc) print('[INFO]:Detect %d license plates' % len(regions))  for box in regions: cv2.drawContours(img, [box], 0, (0, 255, 0), 2) cv2.imshow('Result', img) cv2.imwrite('result.jpg', img) cv2.waitKey(0) cv2.destroyAllWindows()Copy the code

5) Effect display

01:

Effect:

02: the original image

Effect:

conclusion

Well, that’s all for this article

License plate detection and license plate recognition is actually very mature technology. So what I do only represents the rookie level ~

Make a little progress every day, and you can stick to it with me

💖 free source code base —

Private letter xiaobian 06 or click on this line of blue font can be free ha!

Your support is my biggest motivation!! Mua welcomes you to read the previous article

😘 Recommended reading —

Project 0.1【Opencv actual combat 】 Sketch, to draw the feeling, such as drawing like this: draw what everything looks good (suggested to save)

Project 0.2 [Opencv actual combat] Waste film becomes treasure, how to bring out the effect of yanyu oil painting? Try this special effect. It’s awesome

Project 0.3 [Opencv actual combat] Beauty mode, open: beauty function is so powerful? (The mob dance. JPG)

Project 0.4 【Opencv actual combat 】 What is the ghost of “character animation”? Teach you to rise posture today – net friend ecstasy: I want

Don’t stop me from studying

🎁 Article summary —

Summary: Python article collection | (introduction to the actual combat, games, Turtle, cases, etc.)

(article summary and more your case waiting for you to learn ~ source code can find me free!)

🎄 Article summary —

The article summary | Python 1.0-2021 | has continuously updated, direct look at this article is enough

More content + source code in the article summary oh!! Welcome to read ~)