Home > other >  SVM HOG do sample training times wrong, change the path or not, also for help, thank you
SVM HOG do sample training times wrong, change the path or not, also for help, thank you

Time:09-24

# include
# 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 The SVM=SVM: : create ();

If (TRAIN)
{
String ImgName;
Ifstream finPos (" pos. TXT ");
Ifstream finNeg (" neg. TXT ");

if (! FinPos | |! FinNeg)
{
cout <"Pos/Neg imglist reading failed... "& lt; return 1;
}
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; ImgName=DATA \ \ positive \ \ "" + ImgName;
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 Descriptors.
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 (num, I)=descriptors [I];
SampleLabelMat. At (num, 0)=1;
}
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; ImgName=DATA \ \ negtive \ \ "" + ImgName;
Mat SRC=https://bbs.csdn.net/topics/imread (ImgName);

Vector Descriptors.
Hog.com pute (SRC, descriptors, the Size (4, 4));

for (int i=0; I & lt; DescriptorDim; I++)
SampleFeatureMat. At (num + PosSamNO, I)=descriptors [I];
SampleLabelMat. At (num + PosSamNO, 0)=1;
}
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; The SVM - & gt; "Train" (sampleFeatureMat ROW_SAMPLE, sampleLabelMat);
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 (" SVM_HOG. XML "); The static function
//the SVM - & gt; Load (" SVM_HOG. XML "); So not use
//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 Vec.
for (int i=0; I & lt; Svdim; + + I)
{
Vec. Push_back (Result. At (0, I));
}
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
oh roar reference 1 floor? Response:
see which other error, step through, in according to the error information modify, refueling (? ?? _???? )?
Also, for help post, would get error message directly to a few large section of the code will be carefully to see,,,


Thanks, just looked at it and I found my SRC is empty, as if is the path of the picture has a problem,

CodePudding user response:

Comparing to the print path

CodePudding user response:

The
oh roar reference 3 floor? nullnullnullnullnullnullnullnullnullnullnullnull
  • Related