OpenCV geometric shape recognition based on SVM

Source: Internet
Author: User
Tags svm

Many times, after Findcontours, to get a closed geometry, if known to be a circle, then think of ways to parameterize the expression of the circle, if it is known as a rectangle, the corresponding way to parameterize the expression of the rectangle. When we develop a simple linear or circular parametric expression algorithm, we will find out how to know whether it is a circle or a straight line, and become an urgent problem to be solved.

This paper uses SVM to realize geometric shape recognition. Currently only supports cirlce Rectangle triangle cross four species. Knowing the principles and ideas behind it and wanting to support more of the geometry's recognition, I believe, is not difficult.

Not much to say, the first part of the code.

1. Data to be trained

void GetData ()
{
	traindata.create (train_samples*classes, Samplesize.width*samplesize.height, CV_32FC1);
	Trainclasses.create (train_samples*classes, 1, CV_32FC1);
	Mat Src_image;
	Char file[255];
	int I, J;
	for (i = 0; i<classes; i++)
	{
		for (j = 0; j< train_samples; j + +)
		{
			sprintf (file, "Samples/s%d/%d.pn G ", I, j);
			Src_image = imread (file, 0);
			if (Src_image.empty ())
			{
				printf ("Error:cant load Image%s\n", file);
				Exit ( -1);
			}
			Mat image = Readimagesavecontour (src_image);
			Mat imagenewsize;
			Resize (image, Imagenewsize, samplesize, cv_inter_linear);
			Image.release ();
			Image = Imagenewsize.reshape (1, 1);
			Image.convertto (Traindata (range (I*train_samples + j, I*train_samples + j + 1), range (0, traindata.cols)), CV_32FC1); 
  trainclasses.at<float> (I*train_samples + j, 0) = i;
		}
	}
}
2. Affirm a SVM class and train

Cvsvmparams Svm_params;  The CVSVMPARAMS structure is used to define the basic parameters  
	Svm_params.svm_type = cvsvm::c_svc;     SVM type  
	svm_params.kernel_type = cvsvm::linear;//Do not map  
	svm_params.degree = 0;
	Svm_params.gamma = 1;
	SVM_PARAMS.COEF0 = 0;
	Svm_params. C = 1;
	svm_params.nu = 0;
	SVM_PARAMS.P = 0;
	Svm_params.term_crit = Cvtermcriteria (Cv_termcrit_iter, 0.01);

	CVSVM SVM;
	Svm.train (Traindata, Trainclasses, Mat (), Mat (), svm_params);

Well, that's the part of the code that's shown.

The download of the entire project and training sample is given below. Vs2013+opencv2.4.13 can be run directly. Download link

The effect is as shown in the figure



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.