Identification of license plate numbers based on opencv: opencv license plate number
Introduction
A http://blog.csdn.net/maotoula/article/details/7680716 is found on csdn, which analyzes the recognition process of a license plate. As a result, we analyzed the process and used opencv to implement the code again.
Image preprocessing
First, get the following license plate photo:
Then we perform some preprocessing on it. Here we do a simple gray-scale binarization:
uchar* ptr = img_2.ptr(0); /********************************************************************/ for(int i = 0; i < width; i++){ for(int j=0;j
That is to say, using the previously mentioned algorithm, first convert an RGB channel image to a grayscale image, and then convert the image to a 2-value image using the set threshold value of 125. The result is as follows:
Character Segmentation
The second step is to separate the seven characters in the binary image. Our test photo has now turned to a white font, black background, and 4 white rivets up and down. So what we need to do is remove four rivets and separate the seven characters separately. 1. the method used is shown in the blog. First, scan the image horizontally, and then scan the image vertically. As shown in the slice, we can see that the rows in the image contain the least pixels. There is a significant difference between it and real numbers. Therefore, you can make a threshold value judgment during horizontal scanning. Starting from the beginning, until the number of pixels is more than 20, it indicates that the character actually starts. Then, when the pixel of a row is lower than 20, the character ends.
int real_width1, real_width2; int flag_width[width]; for(int i = 0; i < width; i++){ for(int j=0;j
In this way, the final obtained real_width1 and real_wid2represent the start and end positions of the real row of the character. Therefore, the ordinate values of the start and end of the character are determined.
2. Find the abscissa between the start and end of each character. Since each character is separated, we can directly Scan each column. When 255 pixels are found, it is the starting position of the first character, when a column does not contain 255 pixels, it indicates the end position of the first character and the position where the next character starts scanning until all the seven characters are scanned.
int real_address[7][2]; for(int j=0;j
In this way, the start and end positions of the Seven-character ordinate exist in the array real_address. Then, we will display them:
void wordshow(int number){ cv::Mat word; int word_width = real_width2 - real_width1; int word_height; uchar* ptr_word; char str[2]; sprintf(str, "%d", number); word_height = real_address[number][1] - real_address[number][0]; word = cv::Mat(word_width, word_height, CV_8UC1, 1); ptr_word = word.ptr(0); for(int i = real_width1; i < real_width2; i++){ for(int j=real_address[number][0]; j<real_address[number][1]; j++){ int tmp; s = cvGet2D(&pI_2,i,j); tmp = (int)s.val[0]; ptr_word[(i-real_width1) * word_height + (j - real_address[number][0])] = tmp; } } cv::namedWindow(str); cv::imshow(str,word);}
The displayed character segmentation result is as follows:
The code is downloaded as follows: http://download.csdn.net/detail/u011630458/8414317
Normalization
After that, you need to normalize the split 7-character image. Simply put, you need to set the size of the seven images to the same size. I set it here to use the cvResize function provided by opencv for 40X20.
.......... word2 = cv::Mat(40, 20, CV_8UC1, 1); IplImage pI_3 = word; IplImage pI_4 = word2; cvResize(&pI_3, &pI_4, 1);.........
In this way, copy the original word image to word2 and set it to 40x20. The purpose is to facilitate template matching later. The template image I used is 40x20.
Character Recognition
Template matching is used for character recognition. The SSIM function is used in the opencv official tutorial. This function returns the structure similarity index of the image. This is a floating point number between 0 and 1 (the closer it is to 1, the higher the degree of conformity ). In the code, the match_pic folder is stored in the template image to be matched. Then, seven characters are separated, and each one uses getMSSIM to match all template images one by one. The image with the highest matching similarity is selected as the corresponding character. The final result is as follows:
Code download location: http://download.csdn.net/detail/u011630458/8414375
Summary
As shown in, the recognition rate of this method is low. In addition, the character extraction method in the middle is too simple and cannot be used universally .. It can only be used as a basic exercise ..