Using Python to locate and separate license plates

Source: Internet
Author: User
Step 1: convert the collected color license plate image to a grayscale or grayscale image after Gaussian smoothing, then perform direct filtering. 3. use the Sobel operator to perform Edge Detection. 4. conduct corrosion, expansion, and operation on the binarization image, closed operation morphological combination transformation 5. search for the contour of the transformed morphological image, and extract the license plate & amp; lt; based on the license plate's aspect ratio ;! More & amp; gt; code: grayscale graycv2.cvtColor (img, cv2.COLORBGR2GR...

1. convert the collected color license plate image into a grayscale image
2. after a grayscale image is processed using Gaussian smoothing, the image is then subjected to Central direct filtering.
3. use the Sobel operator to perform edge detection on the image
4. corrosion, expansion, opening, and closed morphological combination transformation of binarization images
5. search the contour of the image after morphological transformation, and extract the license plate based on the license plate's aspect ratio.

Code implementation of image grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Gaussian smoothing and median filtering
gaussian = cv2.GaussianBlur(gray, (3, 3), 0, 0, cv2.BORDER_DEFAULT)median = cv2.medianBlur(gaussian, 5)

Corrosion, expansion, open operation, and morphological combination transformation of binarization images
# Expansion and corrosion operation kernel function element1 = cv2.getStructuringElement (cv2.MORPH _ RECT, (9, 1) element2 = cv2.getStructuringElement (cv2.MORPH _ RECT, (8, 6) # expansion once, highlight the profile with dilation = cv2.dilate (binary, element2, iterations = 1) # corrode once, remove the details with erosion = cv2.erode (dilation, element1, iterations = 1) # expand again, make the profile more visible: dilation2 = cv2.dilate (erosion, element2, iterations = 3)

Search the contour of the transformed image based on the aspect ratio of the license plate.

1. find the license plate area

Def findPlateNumberRegion (img): region = [] # Find the contour S, hierarchy = cv2.findContours (img, cv2.RETR _ TREE, cv2.CHAIN _ APPROX_SIMPLE) # for I in range (len (contours) with a small area: cnt = S [I] # calculate the area of the contour area = cv2.20.area (cnt) # if (area <2000): continue # contour approximation. epsilon = 0.001 * cv2.arcLength (cnt, True) approx = cv2.approxPolyDP (cnt, epsilon, true) # Find the smallest rectangle, which may have direction rect = cv2.minAreaRect (cnt) print "rect is:" print rect # box is the coordinate box of the four points = cv2.cv. boxPoints (rect) box = np. int0 (box) # calculate height and width height = abs (box [0] [1]-box [2] [1]) width = abs (box [0] [0]-box [2] [0]) # Under normal circumstances, the length to height ratio of the license plate is between 2.7 and 5. ratio = float (width) /float (height) if (ratio> 5 or ratio <2): continue region. append (box) return region

2. use green lines to draw license plate areas and cut license plates

# Use a green line to draw the outlines of these images for box in region: cv2.drawContours (img, [box], 0, (0,255, 0), 2) ys = [box [0, 1], box [1, 1], box [2, 1], box [3, 1] xs = [box [0, 0], box [1, 0], box [2, 0], box [3, 0] ys_sorted_index = np. argsort (ys) xs_sorted_index = np. argsort (xs) x1 = box [xs_sorted_index [0], 0] x2 = box [xs_sorted_index [3], 0] y1 = box [ys_sorted_index [0], 1] y2 = box [ys_sorted_index [3], 1] img_org2 = img. copy () img_plate = img_org2 [y1: y2, x1: x2]

The above describes how to locate and split license plates in Python. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.