# include
# include
# include
# include
# include
# include
using namespace std;
Using the namespace CV;
Using the namespace CV: : ml;
8792//# define PosSamNO is sample number
8968//# define NegSamNO negative sample number
# define "TRAIN" true
# define CENTRAL_CROP true
Int main ()
{
HOGDescriptor hog (Size ((64), the Size 16 (16th) and Size (8, 8), Size (8, 8), 9);
Int DescriptorDim;
Ptr
If (TRAIN)
{
String ImgName;
Ifstream finPos (" pos. TXT ");
Ifstream finNeg (" neg. TXT ");
if (! FinPos | |! FinNeg)
{
cout <"Pos/Neg imglist reading failed... "& lt;
}
Mat sampleFeatureMat;
Mat sampleLabelMat;
//loading the original positive examples...
For (int num=0; Num & lt; PosSamNO & amp; & Getline (finPos ImgName); Num++)
{
cout <"Now processing the original positive image:" & lt;
Mat SRC=https://bbs.csdn.net/topics/imread (ImgName);
/* if (CENTRAL_CROP)
16,16,64,64 SRC=https://bbs.csdn.net/topics/src (the Rect ()); */
Vector
Hog.com pute (SRC, descriptors, the Size (4, 4));//calculate the hog descriptor, detection window moving step length (8, 8)
//cout & lt; <"Go" & lt;
If (0==num)
{
DescriptorDim=descriptors. The size ();
SampleFeatureMat=Mat: : zeros (PosSamNO + NegSamNO DescriptorDim, CV_32FC1);
SampleLabelMat=Mat: : zeros (PosSamNO + NegSamNO, 1, CV_32FC1);
//sampleLabelMat data type must be a signed integer
}
//will calculate good HOG descriptor is copied to the sample characteristic matrix sampleFeatureMat
for (int i=0; I & lt; DescriptorDim; I++)
SampleFeatureMat. At
SampleLabelMat. At
}
FinPos. Close ();
//loading the original negtive examples...
For (int num=0; Num & lt; NegSamNO & amp; & Getline (finNeg ImgName); Num++)
{
cout <"Now processing the original negtive image:" & lt;
Mat SRC=https://bbs.csdn.net/topics/imread (ImgName);
Vector
Hog.com pute (SRC, descriptors, the Size (4, 4));
for (int i=0; I & lt; DescriptorDim; I++)
SampleFeatureMat. At
SampleLabelMat. At
}
FinNeg. Close ();
The SVM - & gt; SetType (SVM: : C_SVC);
The SVM - & gt; SetC (0.01);
The SVM - & gt; SetKernel (SVM: : LINEAR);
The SVM - & gt; SetTermCriteria (TermCriteria (TermCriteria: : MAX_ITER, 3000, 1 e - 6));
cout <"Starting training... "& lt;
cout <"Finishing training... "& lt;
The SVM - & gt; Save (" SVM_HOG. XML ");
cout <"SVM_HOG. XML have saved!"
The else
{
//the SVM=SVM: : load & lt; CV: : ml: : SVM & gt; (" SVM_HOG. XML ");//or SVM=Statmodel: : load
//the SVM - & gt; Load
//vm. The load (" SVM_HOG. XML ");
Ptr & lt; Ml: : SVM & gt; The SVM=ml: : SVM: : load (" SVM_HOG. XML ");
}
Mat svecsmat=SVM - & gt; GetSupportVectors ();//svecsmat element data type to float
Int svdim=SVM - & gt; GetVarCount ();//eigenvector digits
Int numofsv=svecsmat. Rows;
Mat alphamat=Mat: : zeros (numofsv svdim, CV_32F);//alphamat and svindex must be initialized, otherwise getDecisionFunction () function complains
Mat svindex=Mat: : zeros (1, numofsv, CV_64F);
Mat Result;
Double rho=SVM - & gt; GetDecisionFunction (0, alphamat, svindex);
Alphamat. ConvertTo (alphamat CV_32F);//alphamat element data type to convert CV_32F
Result=1 * alphamat * svecsmat;//float
Vector
for (int i=0; I & lt; Svdim; + + I)
{
Vec. Push_back (Result. At
}
Vec. Push_back (rho);
//saving HOGDetectorForOpenCV. TXT
Ofstream fout (" HOGDetectorForOpenCV. TXT ");
for (int i=0; I & lt; Vec. The size (); + + I)
{
Fout & lt;
return 0;
}
CodePudding user response:
See which other error, step through, in according to the error information modify, refueling (? ?? _???? )?Also, help post, would get error message directly to a few large section of the code will be carefully to see,,,
CodePudding user response:
The