Code analysis of HyperLPR license plate recognition library

Keywords: Python

2021SC@SDUSC

Source code download address: https://gitee.com/zeusees/HyperLPR

See the first analysis for details of source code configuration

  The content of this article will continue to be analyzed according to the tasks assigned by the group, as follows:

1, bitwise_not(...)

          For all pictures in the interval [0,5], perform the following operations:

        if ptype>0 and ptype<5:
            # pass
            plate = cv2.bitwise_not(plate)

  bitwise_not is the "not" operation on binary data, that is, the binary "not" operation is performed on each pixel value of the image (gray image or color image), ~ 1 = 0, ~ 0 = 1

2, findContoursAndDrawBoundingBox(...)

           Execute functions on pictures:

image_rgb = fm.findContoursAndDrawBoundingBox(plate)

        Its source code is:

def findContoursAndDrawBoundingBox(image_rgb):
    line_upper  = [];
    line_lower = [];
    line_experiment = []
    grouped_rects = []
    gray_image = cv2.cvtColor(image_rgb,cv2.COLOR_BGR2GRAY)
    for k in np.linspace(-50, 0, 15):
        binary_niblack = cv2.adaptiveThreshold(gray_image,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,17,k)
        imagex, contours, hierarchy = cv2.findContours(binary_niblack.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
        for contour in contours:
            bdbox = cv2.boundingRect(contour)
            if (bdbox[3]/float(bdbox[2])>0.7 and bdbox[3]*bdbox[2]>100 and bdbox[3]*bdbox[2]<1200) or (bdbox[3]/float(bdbox[2])>3 and bdbox[3]*bdbox[2]<100):
                # cv2.rectangle(rgb,(bdbox[0],bdbox[1]),(bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]),(255,0,0),1)
                line_upper.append([bdbox[0],bdbox[1]])
                line_lower.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])

                line_experiment.append([bdbox[0],bdbox[1]])
                line_experiment.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
                # grouped_rects.append(bdbox)

    rgb = cv2.copyMakeBorder(image_rgb,30,30,0,0,cv2.BORDER_REPLICATE)
    leftyA, rightyA = fitLine_ransac(np.array(line_lower),3)
    rows,cols = rgb.shape[:2]
    leftyB, rightyB = fitLine_ransac(np.array(line_upper),-3)
    rows,cols = rgb.shape[:2]
    pts_map1  = np.float32([[cols - 1, rightyA], [0, leftyA],[cols - 1, rightyB], [0, leftyB]])
    pts_map2 = np.float32([[136,36],[0,36],[136,0],[0,0]])
    mat = cv2.getPerspectiveTransform(pts_map1,pts_map2)
    image = cv2.warpPerspective(rgb,mat,(136,36),flags=cv2.INTER_CUBIC)
    image,M = deskew.fastDeskew(image)
    return image

        This function is a conditional method, provided that the 6 characters of the license plate cannot be completely adhered. The first is to determine the upper and lower boundaries and extract the license plate area detected by extend.

         Then, multiple parameters are used to adaptively binarize this region for many times. The parameter of k of the adaptive threshold function in opencv changes from - 50 to 0. Do binarization 15 times.

         Then, the connected domain of each binarized image is analyzed to find the contour that meets the character aspect ratio.

         Then we will do line fitting for the following points. Before doing line fitting, we will introduce the random sampling consensus (RANSAC) algorithm.

         The data obtained in practical application often contains noise data, which will interfere with the construction of the model. We call such noise data points outliers, and those that play a positive role in the construction of the model are inliers. One thing RANSAC does is to randomly select some points and use these points to obtain a model (this is a little mysterious. If you are fitting a straight line, the so-called model is actually the slope) , and then use this model to test the remaining points. If the tested data points are within the allowable error range, the data points will be judged as inlier, otherwise it will be judged as outlier. If the number of inliers reaches a set threshold, it means that the selected data point sets have reached an acceptable level. Otherwise, continue all the steps after the previous random selection point sets, and no This process is repeated until the selected data point sets are found to be acceptable. At this time, the obtained model can be regarded as the optimal model construction of data points.

         When analyzing the connected domain, we only use the boundingbox that meets the character length width ratio as the judgment condition, which will bring some noise. As shown in the figure below, there are error points that meet the conditions in the lower corner. Therefore, RANSAC algorithm can help us eliminate these noise points.

         We use RANSAC algorithm to fit the points in the above figure.        

         In this way, the upper and lower boundaries are found. Next, we just need to cross this area.

Reference article: fine license plate location of HyperLPR license plate recognition algorithm

Author: Jack Yu Yu

Original link: https://blog.csdn.net/Relocy/article/details/78705566

Posted by glennn3 on Sun, 31 Oct 2021 06:03:21 -0700