Keywords: hu moment, SVM,OPENCV
In the image to identify the target, it is related to the presence of a target in a specific area, SVM can be in the case of small sample size of the positive and negative samples (picture foreground background) to make a good distinction, the basic features of the picture including such as Hog, LBP, Haar, etc. In the specific object detection, consider combining the characteristics of the test to use or design new features for training and classification. In this paper, the general use process of SVM classifier in OpenCV is illustrated by the example of geometrical invariant moment, and then the Hu moment function, SVM parameter setting and example demonstration are described in turn.
1.Hu Solution
Double M[7];//hu Moment output
Moments mo; Moment variable
Src=imread (path, imread_grayscale);//Get image
Canny_output=predispose (SRC);//Primary image processing
Resize (canny_output, imagenewsize, SampleSize, cv_inter_linear);//Dimension Normalization
Calculate Hu Moment
Mo=moments (imagenewsize);
Humoments (Mo, M);
2.SVM Training Process
1) Training matrix variables
Mat Traindata (A,B,CV_32FC1);//Training Sample Set A: Total number of samples B: number of features
Mat labels (A,1,CV_32FC1);//tags corresponding to training data A: Total number of samples
2) SVM parameter setting
Cvsvmparams Svm_params; Cvsvmparams structure for defining 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, 1000, 0.01);
3) SVM Training and preservation
CVSVM SVM;
Svm.train (traindata, labels, mat (), Mat (), svm_params);
Svm.save ("Svm_para.xml");
4) SVM Sample detection
CVSVM SVM;
Svm.load ("Svm_para.xml");
float response = svm.predict (test);//test: sample characteristics to be detected
3. Training and testing examples
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/ml/ml.hpp>
#include <iostream>
using namespace CV;
using namespace Std;
Mat Predispose (Mat src);
Size samplesize (160,160);//Sample sizes
int main ()
{
Mat Traindata (20,7,CV_32FC1);//Training Sample Set
Mat labels (20,1,CV_32FC1);//tags corresponding to the training data
Mat Canny_output;
Mat imagenewsize;
Char path[90];//picture Path
Mat src;//Enter picture
Double M[7];//hu Moment
Moments mo; Moment variable
float* p; Data row variable
int train_samples=10;
for (int i=0;i<2;++i)
{
for (int j=0;j<10;++j)
{
if (i==0)
sprintf_s (Path, "Negtive/%d.jpg", j);
Else
sprintf_s (Path, "Positive/%d.jpg", j);
Src=imread (path, Imread_grayscale);
Canny_output=predispose (SRC);
Resize (canny_output, imagenewsize, SampleSize, cv_inter_linear);
Calculate Hu Moment
Mo=moments (imagenewsize);
Humoments (Mo, M);
Training Sample Set Assignment
Mat C = (mat_<double> (1,7) << m[0],m[1],m[2],m[3],m[4],m[5], m[6]);
C.convertto (Traindata (range (I*train_samples + j, I*train_samples + j + 1), range (0, traindata.cols)), CV_32FC1);
Label Assignment
Labels.at<float> (i*train_samples + j,0) = i;
}
}
Cvsvmparams Svm_params; Cvsvmparams structure for defining 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, 1000, 0.01);
CVSVM SVM;
Svm.train (traindata, labels, mat (), Mat (), svm_params);
Svm.save ("Svm_para.xml");
CVSVM SVM;
Svm.load ("Svm_para.xml");
for (int i=0;i<8;++i)
{
sprintf_s (Path, "test/%d.jpg", I);
Src=imread (path, Imread_grayscale);
Canny_output=predispose (SRC);
Resize (canny_output, imagenewsize, SampleSize, cv_inter_linear);
Imshow ("Canny", imagenewsize);
Calculate Hu Moment
Mo=moments (imagenewsize);
Humoments (Mo, M);
Sample Assignment
Mat test (1,7,CV_32FC1);
Mat C = (mat_<double> (1,7) << m[0],m[1],m[2],m[3],m[4],m[5], m[6]);
C.convertto (Test (range (0, 1), range (0, 7)), CV_32FC1);
float response = svm.predict (test);
cout<<response<<endl;
}
Waitkey (0);
return exit_success;
}
Hu moment SVM Training and detection-----OpenCV