Learning OPENCV--HOG+SVM

Source: Internet
Author: User
Tags svm

#include"cv.h"#include"highgui.h"#include"stdafx.h"#include<ml.h>#include<iostream>#include<fstream>#include<string>#include<vector>using namespaceCV;using namespacestd;intMainintargcChar**argv) {Vector<string>Img_path; Vector<int>IMG_CATG; intNLine =0; stringbuf; Ifstream Svm_data ("E:/svm_data.txt" ); unsignedLongN;  while(svm_data) {if(Getline (Svm_data, buf)) {NLine++; if(NLine%2==0) {img_catg.push_back (Atoi (Buf.c_str () ));//atoi converting strings to integers, flags (0,1)            }            Else{img_path.push_back (BUF);//Image Path}}} svm_data.close ();//Close FileCvmat*data_mat, *Res_mat; intNimgnum = nLine/2;//number of read-in Samples    ////Sample Matrix, Nimgnum: Horizontal axis is the number of samples, WIDTH * HEIGHT: Sample eigenvector, that is, image sizeData_mat = Cvcreatemat (Nimgnum,1764, CV_32FC1);    Cvsetzero (Data_mat); //type matrix, which stores the type flags for each sampleRes_mat = Cvcreatemat (Nimgnum,1, CV_32FC1);    Cvsetzero (Res_mat); Iplimage*src; Iplimage* Trainimg=cvcreateimage (Cvsize ( -, -),8,3);//images that need to be analyzed     for(string:: Size_type i =0; I! = Img_path.size (); i++) {src=cvloadimage (Img_path[i].c_str (),1); if(src = =NULL) {cout<<"can not load the image:"<Endl; Continue; } cout<<"processing"<Endl;   Cvresize (SRC,TRAINIMG); //Reading PicturesHogdescriptor *hog=NewHogdescriptor (Cvsize ( -, -), Cvsize ( -, -), Cvsize (8,8), Cvsize (8,8),9);//See reference article for specific meaningsvector<float>descriptors;//result ArrayHog->compute (Trainimg, Descriptors,size (1,1), Size (0,0));//call the calculation function to start the calculationcout<<"HOG dims:"<<descriptors.size () <<Endl; //cvmat* Svmtrainmat=cvcreatemat (Descriptors.size (), 1,CV_32FC1);n=0;  for(vector<float>::iterator Iter=descriptors.begin (); Iter!=descriptors.end (); iter++) {Cvmset (Data_mat,i,n,*ITER); N++; }                //cout<<svmtrainmat->rows<<endl;Cvmset (Res_mat, I,0, Img_catg[i]); cout<<"End Processing"<" "<Endl; } CVSVM SVM=CVSVM ();      Cvsvmparams param;      Cvtermcriteria criteria; Criteria= Cvtermcriteria (Cv_termcrit_eps, +, Flt_epsilon); Param= Cvsvmparams (Cvsvm::c_svc, CVSVM::RBF,10.0,0.09,1.0,10.0,0.5,1.0, NULL, criteria); /*Type of SVM: Cvsvm::c_svc kernel type: CVSVM::RBF degree:10.0 (not used this time) gamma:8.0 coef0:1.0 (not used this time)                                                           c:10.0 nu:0.5 (not used this time) p:0.1 (not used this time) then normalizes the training data and places it in an array of cvmat types. */         //☆☆☆☆☆☆☆☆☆ (5) SVM Learning ☆☆☆☆☆☆☆☆☆☆☆☆Svm.train (Data_mat, res_mat, null, NULL, param); //★ ☆ Using training data and determined learning parameters, the SVM learning ☆☆☆☆Svm.save ("Svm_data.xml" ); //test SampleIplimage *test; Vector<string>Img_tst_path; Ifstream IMG_TST ("E:/svm_test.txt" );  while(IMG_TST) {if(Getline (IMG_TST, buf)) {img_tst_path.push_back (BUF);    }} img_tst.close (); Cvmat*test_hog = Cvcreatemat (1,1764, CV_32FC1); Charline[ +]; Ofstream Predict_txt ("Svm_predict.txt" );  for(string:: Size_type j =0; J! = Img_tst_path.size (); J + +) {Test= Cvloadimage (Img_tst_path[j].c_str (),1); if(Test = =NULL) {cout<<"can not load the image:"<Endl; Continue;        } cvzero (TRAINIMG);   Cvresize (TEST,TRAINIMG); //Reading PicturesHogdescriptor *hog=NewHogdescriptor (Cvsize ( -, -), Cvsize ( -, -), Cvsize (8,8), Cvsize (8,8),9);//See reference article for specific meaningsvector<float>descriptors;//result ArrayHog->compute (Trainimg, Descriptors,size (1,1), Size (0,0));//call the calculation function to start the calculationcout<<"HOG dims:"<<descriptors.size () <<Endl; Cvmat* Svmtrainmat=cvcreatemat (1, Descriptors.size (), CV_32FC1); N=0;  for(vector<float>::iterator Iter=descriptors.begin (); Iter!=descriptors.end (); iter++) {Cvmset (Svmtrainmat,0, n,*ITER); N++; }        intRET =svm.predict (Svmtrainmat); sprintf (line,"%s%d\r\n", Img_tst_path[j].c_str (), ret); Predict_txt<<Line ; } predict_txt.close ();//cvreleaseimage (&SRC);//cvreleaseimage (&sampleimg);//cvreleaseimage (&TST);//cvreleaseimage (&tst_tmp);Cvreleasemat (&Data_mat); Cvreleasemat (&Res_mat);return 0;}

from:http://blog.csdn.net/yangtrees/article/details/7471222

Learning OPENCV--HOG+SVM

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.