Using OPENCV SVM to realize license plate region recognition

Source: Internet
Author: User
Tags array length sprintf svm

First, preface

This paper only demonstrates the training of the positive and negative samples of the license plate region using the SVM functions already defined in the opencv2.4.6, and then uses the well-trained SVM model to predict the test samples.

second, the use of positive and negative samples first, I will be a series of images preprocessing, segmentation and other steps, this part of the content can be read "in-depth understanding OPENCV use Computer Vision Project Analysis", This selects 100 positive samples (license plate area) and 70 negative samples (non-license plate area), the size of which is 144*33, respectively, is stored in the directory F:\SVM_data\posdata and F:\SVM_data\negdata, as follows:



The image file in the image above is named to facilitate reading data, press CTRL + A Select all, press F2 to quickly and uniformly modify the name, of course, a TXT file to save the path of these images to the time to read again is also possible, but this does not do so.

Take another 6 pictures as a test sample, which contains 3 license plate area and 3 non-license plate area, the test sample is not related to the previous training sample, for another selection. Store in Directory F:\SVM_data\testdata

Three, the code introduction
In fact, the idea is very simple, for 100 positive samples and 70 negative sample images, first each picture will be converted to 1-dimensional array, the array length is 4752 (also 144 by 33), will also be the image of all the pixel values as a feature, of course, you can also select the image of other eigenvalues, so that each picture can be 1 *4752 Matrix, all these matrices are stored in a 170*4752 training matrix, followed by the corresponding label matrix, which is 170*1 size, the first 100 numbers are 1.0, and the last 70 numbers are 1.0, that is, the positive and negative properties of the data in the training matrix are marked. Set up the SVM training parameters, use the Cvsvm::train () function for training, and then use the trained model to predict the test data. The code is as follows:

#include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/ml/ml.hpp" using namespace

Cv
	#include <iostream> int main () {int num = 1;

	Char path[90]; Mat Trainningmat;
	The number of rows is the sample number 170, each sample contains 144*33=4752 number, that is, 4752 rows of Mat inputimg;

	Mat p;
		Load 100 positive Samples first while (Num < 101) {sprintf (Path, "f:\\svm_data\\posdata\\0000 (%d). bmp", num);
		inputimg = Imread (path,-1); if (Inputimg.empty ()) {std::cerr<< "Cannot load a positive sample.
			";
		return-1;
		} p = Inputimg.reshape (a);
		P.convertto (P, CV_32FC1);
		Trainningmat.push_back (P);
	num++;

	num = 1;
		Load 70 negative samples while (num <) {sprintf (Path, "f:\\svm_data\\negdata\\0000 (%d). bmp", num);
		inputimg = Imread (path,-1); if (Inputimg.empty ()) {std::cerr<< "Cannot load a negative sample.
			";
		return-1;
		} p = Inputimg.reshape (a);
		P.convertto (P, CV_32FC1);
		Trainningmat.push_back (P);
	num++;   } Mat label (170,1,CV_32FC1); The label corresponding to the training data, of course, write the data in TXT can also be read out for (int i = 0; i<label.rows; + +) i) {if (I <) label.at<float> (i,0) = 1.0;
	else label.at<float> (i,0) =-1.0;
	} CVSVM classifier;
	Cvsvmparams Svm_params; Svm_params.kernel_type = Cvsvm::linear; Use linear partitioning Classifier.train (Trainningmat,label, Mat (), Mat (), svm_params); SVM training vector<mat> TestData;
	Define test Data num = 1;
		while (Num < 4) {sprintf (path, "f:\\svm_data\\testdata\\postest%d.bmp", num);
		inputimg = Imread (path,-1); if (Inputimg.empty ()) {std::cerr<< "Cannot load the positive test sample.
			";
		return-1;
		} p = Inputimg.reshape (a);
		P.convertto (P, CV_32FC1);
		Testdata.push_back (P);
	num++;
	num = 1;
		while (Num < 4) {sprintf (path, "f:\\svm_data\\testdata\\negtest%d.bmp", num);
		inputimg = Imread (path,-1); if (Inputimg.empty ()) {std::cerr<< "Cannot load a negative test sample.
			";
		return-1;
		} p = Inputimg.reshape (a);
		P.convertto (P, CV_32FC1);
		Testdata.push_back (P);
	num++; } for (int i = 0;i < Testdata.size (); ++i) {std::cout<< "testThe test results for sample "<<i+1<<" are: "<< (int) classifier.predict (testdata[i]) <<" \ n ";
} return 0; }

Iv. Analysis of results

The results of the program run as follows:

It can be seen from the results that the first 3 test samples are positive samples, the last 3 samples are negative samples, the trained model can correctly predict whether the test sample is a license plate area




















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.