# include & lt; Iostream>
# include & lt; Fstream>
# include & lt; Opencv2/core/core. Hpp>
# include & lt; Opencv2 highgui/highgui. Hpp>
# include & lt; Opencv2 imgproc/imgproc. Hpp>
# include & lt; Opencv2 objdetect/objdetect. Hpp>
# include & lt; Opencv2/ml/ml. Hpp>
# include & lt; Time. H>
using namespace std;
Using the namespace CV;
1014//# define PosSamNO is sample number
819//# define NegSamNO negative sample number
//# define "TRAIN" will there be true//training, true retraining, false said reads the XML file of the SVM model
# define "TRAIN" false
# define CENTRAL_CROP true//true: training, INRIA are samples of 96 * 160 images cut out in the middle of the 64 * 128 size
//HardExample: number of negative samples, if HardExampleNO greater than 0, said after processing the initial negative sample set, continue processing HardExample negative sample set,
# define HardExampleNO 0
//inherited from CvSVM class because generated setSVMDetector () used in the testing parameters, need to use the trained SVM decision_func parameters,
//but by looking at the CvSVM source known decision_func parameter is protected type variable, no direct access to, inherit only accessed through function after
The class MySVM: public CvSVM
{
Public:
//get the SVM decision function of alpha array
Double * get_alpha_vector ()
{
Return this - & gt; Decision_func - & gt; Alpha;
}
//get the SVM decision function of rho parameters, namely the offset
Float get_rho ()
{
Return this - & gt; Decision_func - & gt; Rho.
}
};
Int main ()
{
Aiaa HOGDescriptor hog (Size (32), Size (8, 8), Size (4, 4), Size (4, 4), 9);//HOG detector, which is used to calculate the HOG descriptor
//HOGDescriptor hog (Size ((64), the Size 16 (16th) and Size (8, 8), Size (8, 8), 9);//HOG detector, which is used to calculate the HOG descriptor
Int DescriptorDim;//HOG descriptor dimension, the image size, window size, the block size, the cell number of units of the histogram bin
MySVM SVM;//the SVM classifier
//if the TRAIN is true, retraining classifier
If (TRAIN)
{
String ImgName;//picture (absolute path)
//ifstream finPos (" C: \ \ Users \ \ Administrator \ \ Desktop \ \ positive_resize_32_32 \ \ 0000 suitble_pos. TXT ");//is sample image file name list
Ifstream finPos (" E: \ \ pos. TXT ");//is sample image file name list
Ifstream finNeg (" E: \ \ neg. TXT ");//negative sample image file name list
Mat sampleFeatureMat;//all of the training sample feature vector matrix, the number of rows equal to the number of all samples, the number of columns equal to the HOG descriptor dimension
Mat sampleLabelMat;//training sample vector, the category of the number of rows equal to the number of all samples, the number of columns equal to 1. Said no 1 said, - 1
//read in turn are sample pictures, the HOG descriptor
For (int num=0; Num{
Cout<& lt;" Processing: "& lt; //ImgName="D: \ \ DataSet \ \ PersonFromVOC2012 \ " + ImgName;//plus is the sample path name
ImgName="E: \ \ pos \ " + ImgName;//plus is the sample path name
Mat SRC=https://bbs.csdn.net/topics/imread (ImgName);//read the picture
//if (CENTRAL_CROP)
//SRC=https://bbs.csdn.net/topics/src (the Rect (16,16,64,128));//put the pictures of the samples of 96 * 160 INRIA are tailored for 64 * 128, which cut to go up and down around the 16 pixels
//the resize (SRC, SRC, the Size (64128));
Vector Descriptors.//the HOG descriptor vector
Hog.com pute (SRC, descriptors, the Size (4, 4));//calculate the HOG descriptor, detection window moving step (4)
//hog.com pute (SRC, descriptors, the Size (8, 8));//calculate the HOG descriptor, detection window moving step length (8, 8)
//cout<& lt;" Describe a dimension: "& lt;
//when handling the first sample initialization matrix eigenvector matrix and category, because only know dimension of the feature vector to initialize the eigenvector matrix
If (0==num)
{
DescriptorDim=descriptors. The size ();//the dimensions of the HOG descriptor
//initialize all the training sample of eigenvector matrix, the number of lines is equal to the number of all samples, the number of columns equal to the HOG descriptor dimension sampleFeatureMat
SampleFeatureMat=Mat: : zeros (PosSamNO + NegSamNO DescriptorDim, CV_32FC1);
//training samples to initialize the category of the vector, the number of rows equal to the number of all samples, the number of columns equal to 1. 1, 0 means no
SampleLabelMat=Mat: : zeros (PosSamNO + NegSamNO, 1, CV_32FC1);
}
//will calculate good HOG descriptor is copied to the sample characteristic matrix sampleFeatureMat
for(int i=0; ISampleFeatureMat. At (num, I)=descriptors [I];//the first num characteristic vector of the sample with the ith element
SampleLabelMat. At (num, 0)=1;//samples are category 1,
}
//read in turn negative sample pictures, the HOG descriptor
For (int num=0; Num{
Cout<& lt;" Processing: "& lt; ImgName="E: \ \ neg \ " + ImgName;//plus negative sample path name
Mat SRC=https://bbs.csdn.net/topics/imread (ImgName);//read the picture
//the resize (img SRC, the Size (64128));
Vector Descriptors.//the HOG descriptor vector
Hog.com pute (SRC, descriptors, the Size (4, 4));//calculate the HOG descriptor, detection window moving step (4)
//hog.com pute (SRC, descriptors, the Size (8, 8));//calculate the HOG descriptor, detection window moving step length (8, 8)
//cout<& lt;" Describe a dimension: "& lt;
//will calculate good HOG descriptor is copied to the sample characteristic matrix sampleFeatureMat
for(int i=0; ISampleFeatureMat. At (num + PosSamNO, I)=descriptors [I];//the first PosSamNO + num characteristic vector of the sample with the ith element
SampleLabelMat. At (num + PosSamNO, 0)=1;//to 1 classes of negative samples, no
}
////processing HardExample negative samples
//if (HardExampleNO & gt; 0)
//{
//ifstream finHardExample (" HardExample_2400PosINRIA_12000NegList. TXT ");//HardExample negative sample file name list
////in turn read HardExample negative samples pictures, generate the HOG descriptor
//for (int num=0; Num//{
//cout<& lt;" Processing: "& lt; //ImgName="D: \ \ DataSet \ \ HardExample_2400PosINRIA_12000Neg \ " + ImgName;//add HardExample negative sample path name
//Mat SRC=https://bbs.csdn.net/topics/imread (ImgName);//read the picture
////the resize (img SRC, the Size (64128));
//vector Descriptors.//the HOG descriptor vector
//hog.com pute (SRC, descriptors, the Size (8, 8));//calculate the HOG descriptor, detection window moving step length (8, 8)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull