License plate number identification based on opencv _ 1, opencv_1

Source: Internet
Author: User
Tags float range scalar scale image

License plate number identification based on opencv _ 1, opencv_1
Introduction

In accordance with the spirit of falling and falling, this chapter continues to detect and identify the license plate number. All steps are completed in three steps: license plate number positioning, license plate number character segmentation, and character recognition. This chapter is the first part: license plate number positioning.

Effect demonstration
Before getting started, let's take a look at the effect demonstration of the license plate number. Note: All images in this article are from the Internet.
                 
, Locate the license plate number, select the license plate number in a yellow box, and copy the license plate number as a new image.

Code and implementation principles
Gray-scale image/binarization
First, it is also a common operation to grayscale the image, and then from the pixel value of 255 side, with the cumulative pixel accounted for 5% of the total pixel as the binarization threshold value, then obtain the corresponding binarization image. The Code is as follows:
void pic_gray(Mat& mat1, Mat& mat2){IplImage pI = mat1;uchar* ptr;CvScalar s; int width = mat1.rows;int height = mat1.cols; mat2 = cv::Mat(width, height, CV_8UC1, 1);ptr = mat2.ptr(0);for(int i = 0; i < width; i++){for(int j=0; j

First, pic_gray converts the source image img to the grayscale image img_2. Then, the histogram_Calculate function is used to calculate the binarization threshold value threshold in proportion to 5%. Finally, pic_Thresholding and binarization are used. The effect is demonstrated as follows:
                                     

Image Segmentation
The license plate is a blue-white image, and the blue background is removed from the binary image img_2, leaving White words. According to the characteristics of the license plate, the blue and white colors in each line should be at least seven times. If it is converted to the img_2 binary image, it indicates that in the row where the license plate is located, the pixel value is changed at least seven times. Therefore, based on this feature, we can split the image where the license plate number is located. The purchase agent is as follows:
int** selection_Function_1(Mat& mat1, int* number){    int **a, i, j, flag, num = 0, enter_flag = 0;    int width = mat1.rows;    int height = mat1.cols;    uchar* ptr = mat1.ptr(0);     a = (int**)malloc(width * sizeof(int*));     for(i=0; i<width; i++){        flag = 0;        for(j=0; j< height-1; j++){            if(ptr[i*height + j] != ptr[i*height + j +1]){                flag += 1;              }        }        if((flag >= 7) && (enter_flag == 0)){            a[num] = (int* )malloc(2 * sizeof(int));            a[num][0] = i;            enter_flag = 1;        }else if((enter_flag != 0) && (flag < 7)){            if(i - a[num][0] < 8){                continue;               }            a[num][1] = i - 1;            num ++;            enter_flag = 0;        }    }    *number = num;    return a;}void pic_cutting(Mat& mat1, Mat& pic_cutting, int** selection, int number){    int real_height = mat1.cols;    IplImage pI_1 = mat1;    IplImage pI_2;    IplImage pI_3;    CvScalar s;     pic_cutting = cv::Mat(selection[number][1] - selection[number][0], real_height, CV_8UC3, 1);    pI_2 = pic_cutting;     for(int i = selection[number][0]; i < selection[number][1]; i++){        for(int j=0; j<real_height; j++){            s = cvGet2D(&pI_1, i, j);            cvSet2D(&pI_2, i-selection[number][0], j, s);        }    }} int main(int argc,char *argv[]){    ................    selection_1 = selection_Function_1(img_2, &selection_Number_1);    for(i=0; i< selection_Number_1; i++){        DE("selection_1[%d]:%d, %d\n", i, selection_1[i][0], selection_1[i][1]);        }     for(i=0; i<selection_Number_1; i++){        pic_cutting(img, img_3, selection_1, i);        sprintf(str, "%d", i);        namedWindow(str);        imshow(str, img_3);    }    waitKey(0);    return 0;}


First, use the selection_Function_1 function to save the start position and end position of the image row with at least seven consecutive hops in the binary image img_2 to the two-dimensional array selection_1, statistics on the number of times in img_2 images that meet the requirement for consecutive occurrence of at least seven hop-and-hop images are stored in selection_Number_1. Then, the function pic_cutting uses the img source image length as the length of the new image, starts with the row stored in selection_1, and ends as the width of the new image. Then, the image at the corresponding position is copied to img_3 from the source image. The display effect is as follows:
   
On the way to the display effect, we can see that the original image is divided into six images, and the license plate is in the fifth image.

Image Filtering
We know from the license plate number feature that the license plate background is blue. Here, we can detect the amount of blue in the split image and discard images that do not meet the requirements. Then, the filtered images are dimmed and binarization again. The Code is as follows:
Int choice_Color (Mat & mat1, int color_Start, int color_End) {int width = mat1.rows; int height = mat1.cols; uchar * ptr = mat1.ptr (0); IplImage pI_1; int flag [width]; int num, I, j, num_width = 0; CvScalar s; pI_1 = mat1; cvCvtColor (& pI_1, & pI_1, CV_BGR2HSV ); for (I = 0; I <width; I ++) {num = 0; for (j = 0; j 
Use choice_Color to convert img_3 of each split image to HSV in sequence. Then, based on H, the number of lines with more than 20 blue pixels in the image is detected, and the data is returned to color_num. Then, if color_num is greater than five rows, the image cannot be discarded. The demo is as follows:
This step filters and discards only the last image because there are many blue backgrounds in the image.

Related Article

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.