SVM Learning _1 of OPENCV

Source: Internet
Author: User
Tags scalar svm svm tutorial

Overview
This is the SVM learning note for OPENCV, based on the modification and recording of the OPENCV official SVM tutorial. OpenCV's SVM tutorial is as follows: Original: http://docs.opencv.org/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.html# Introductiontosvms Chinese Translation version: http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/ml/introduction_to_svm/ Introduction_to_svm.html#introductiontosvms

Effect Demo
The code of this article works as follows:                                          

Code explanation
Specific Code
The specific code is as follows:
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv/cv.h> #include <stdio.h> #include &l T;opencv2/ml/ml.hpp> using namespace CV; int main () {//Data for visual representationint width =, height = 512; Mat image = Mat::zeros (height, width, cv_8uc3); float labels[3]; Mat Labelsmat (3, 1, CV_32FC1, labels), Labelsmat.rowrange (0,1). Setto (1); Labelsmat.rowrange. Setto (2); Labelsmat.rowrange (2,3). Setto (3); Float Trainingdata[3][2] = {{501, 10}, {255, 10}, {501, 255}}; Mat Trainingdatamat (3, 2, CV_32FC1, trainingdata); Set up SVM ' s parameterscvsvmparams params;params.svm_type = Cvsvm::c_svc;params.kernel_type = cvsvm::linear;params.t Erm_crit = Cvtermcriteria (Cv_termcrit_iter, 1e-6); Train the SVMCVSVM SVM; Svm.train (Trainingdatamat, Labelsmat, Mat (), Mat (), params); VEC3B Green (0,255,0), Blue (255,0,0), Red (0,0,255);//Show the decision regions given by the svmfor (int i = 0, i < image.rows; ++i) {for (int j = 0; j < Image.cols; ++j) { Mat Samplemat = (mat_<float> << i,j); Float response = svm.predict (Samplemat);  if (response = = 1) image.at<vec3b> (j, i) = Green;else if (response = = 2) image.at<vec3b> (j, i) = Blue;else if         (Response = = 3) image.at<vec3b> (j, i) = red;} }//Show the training dataint thickness = -1;int Linetype = 8;circle (Image, point (501, ten), 5, Scalar (0, 0, 0), Th Ickness, Linetype); Circle (image, point (255, ten), 5, Scalar (255, 0, 255), thickness, linetype), Circle (Image, point (501, 255), 5, Scalar (255, 255, 0), thickness, linetype); Imshow ("SVM simple Example", image); Show it to the Userwaitkey (0); }


Set up training samples
float labels[3]; Mat Labelsmat (3, 1, CV_32FC1, labels), Labelsmat.rowrange (0,1). Setto (1); Labelsmat.rowrange. Setto (2); Labelsmat.rowrange (2,3). Setto (3); Float Trainingdata[3][2] = {{501, 10}, {255, 10}, {501, 255}}; Mat Trainingdatamat (3, 2, CV_32FC1, trainingdata);

Here the Labelsmat is set to the classification label 1, 2, 3, corresponding to the training data, that is, the trainingdata in the three groups of data is divided into three classes.

Training Support Vector Machine
Use the function Svm.train to train.
Set up SVM ' s parameterscvsvmparams params;params.svm_type    = Cvsvm::c_svc;params.kernel_type = Cvsvm::linear; Params.term_crit   = Cvtermcriteria (Cv_termcrit_iter, 1e-6);//Train the SVMCVSVM SVM; Svm.train (Trainingdatamat, Labelsmat, Mat (), Mat (), params);

SVM Region Segmentation
Using CVSVM::p redict classifies the input samples by rebuilding the trained support vector machine.
VEC3B Green (0,255,0), Blue (255,0,0), Red (0,0,255);//Show The decision regions given by the svmfor (int i = 0; i < ima Ge.rows; ++i) {for (int j = 0; j < Image.cols; ++j) {Mat Samplemat = (mat_<float>) << i,j); Float response = SVM.PR Edict (Samplemat); if (response = = 1) image.at<vec3b> (j, i)  = Green;else if (response = = 2) image.at<vec3b> (j, i)  = Blue;e LSE if (response = = 3) image.at<vec3b> (j, i)  = red;}}
This is a large loop, traversing the entire Imgge image, in every loop: first create a samplemat, the corresponding I, J, in this loop as the initialization parameter of the Samplemat as the form of float. The Samplemat is then classified using Svm.predict. By response return data, determine whether the samplemat belong to the first, second or third class, and then respectively with red, green, blue in the corresponding position of the image, the classification is marked.

SVM Learning _1 of OPENCV

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.