Specific steps
1, the acquisition of the color license plate image conversion into a grayscale image
2, the gray image uses the Gaussian smoothing processing, then carries on the direct filtering to it
3, using Sobel operator to detect the edge of the image
4, the two value of the image for corrosion, expansion, open operation, closed operation of the morphological combination transformation
5, the morphological transformation of the image after the contour search, according to the license plate length and width ratio to extract license plate
<!--more--
Code implementation
Grayscale of images
Gray = Cv2.cvtcolor (img, Cv2. Color_bgr2gray)
Gaussian smoothing, median filter processing
Gaussian = Cv2. Gaussianblur (Gray, (3, 3), 0, 0, Cv2. Border_default) median = Cv2.medianblur (Gaussian, 5)
Sobel Edge Detection
Sobel = Cv2. Sobel (median, cv2. cv_8u, 1, 0, ksize = 3)
Binary Value
RET, binary = cv2.threshold (Sobel, 255, Cv2. Thresh_binary)
The morphological combination transformation of two-valued image for corrosion, expansion, open operation and closed operation
# Expansion and corrosion operation of the kernel function element1 = cv2.getstructuringelement (cv2. Morph_rect, (9, 1)) Element2 = Cv2.getstructuringelement (cv2. Morph_rect, (8, 6)) # Swell once, let contour highlight dilation = cv2.dilate (binary, element2, iterations = 1) # corrode Once, remove detail erosion = Cv2.erode (Dilat Ion, element1, iterations = 1) # Swell again, let the contour clear some dilation2 = cv2.dilate (erosion, element2,iterations = 3)
The image of morphological transformation is searched, and the license plate is extracted according to the aspect ratio of the license plate.
1. Find the license plate area
def findplatenumberregion (IMG): Region = [] # find contour contours,hierarchy = Cv2.findcontours (img, Cv2. Retr_tree, Cv2. Chain_approx_simple) # Filter Area small for I in range (len (contours)): cnt = contours[i] # Calculates the area of the contour areas = Cv2.contourarea (CNT) # The small area is filtered out if (areas <): continue # contour approximation, very small function epsilon = 0.001 * Cv2.arclength (cnt,true) approx = CV2.APPROXPOLYDP (cnt, epsilon, True) # finds the smallest rectangle, which may have a direction of rect = Cv2.minarearect (CNT) print "Rect is:" The print rect # box is a four point coordinate box = cv2.cv.BoxPoints (rect) box = np.int0 (Box) # Calculates high and wide height = ABS (box[0][1]-box[2][1]) width = ABS (box[0][0]-box[2][0]) # license plate Under normal conditions the height ratio is between 2.7-5 ratio =float (width)/float (height) if (ratio > 5 or ratio < 2): continue Region.append (Box) return region
2. Draw the license plate area with the green Line and cut the license plate
# Draw these found outlines with the Green Line for the box in the 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_so Rted_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]