The method to extract HOG feature is refer to -> http://feelmare.blogspot.kr/2014/04/example-source-code-of-extract-hog.html
The method to training SVM of HOG feature is refer to -> http://feelmare.blogspot.kr/2014/04/example-source-code-hog-feature-to.html
The method is using training data.
Again training data make HOG feature, and check the feature is positive or not using trained SVM data.
The example source code is like that.
...
#include < stdio.h> #include < opencv2\opencv.hpp> //#include < opencv2\gpu\gpu.hpp> using namespace cv; using namespace std; #ifdef _DEBUG #pragma comment(lib, "opencv_core247d.lib") #pragma comment(lib, "opencv_imgproc247d.lib") //MAT processing #pragma comment(lib, "opencv_objdetect247d.lib") //HOGDescriptor //#pragma comment(lib, "opencv_gpu247d.lib") //#pragma comment(lib, "opencv_features2d247d.lib") #pragma comment(lib, "opencv_highgui247d.lib") #pragma comment(lib, "opencv_ml247d.lib") //#pragma comment(lib, "opencv_stitching247d.lib"); //#pragma comment(lib, "opencv_nonfree247d.lib"); #else #pragma comment(lib, "opencv_core247.lib") #pragma comment(lib, "opencv_imgproc247.lib") #pragma comment(lib, "opencv_objdetect247.lib") //#pragma comment(lib, "opencv_gpu247.lib") //#pragma comment(lib, "opencv_features2d247.lib") #pragma comment(lib, "opencv_highgui247.lib") #pragma comment(lib, "opencv_ml247.lib") //#pragma comment(lib, "opencv_stitching247.lib"); //#pragma comment(lib, "opencv_nonfree247.lib"); #endif void main() { //variables char FullFileName[100]; char FirstFileName[100]="./images/upperbody"; //"./NegaImages/Negative"; // int FileNum=96; //262; //Load trained SVM xml data CvSVM svm; svm.load("trainedSVM.xml"); //count variable int nnn=0, ppp=0; for(int i=0; i< FileNum; ++i) { sprintf_s(FullFileName, "%s%d.png", FirstFileName, i+1); //printf("%s\n", FullFileName); //read image file Mat img, img_gray; img = imread(FullFileName); //resizing //resize(img, img, Size(16,8) ); //Size(64,48) ); //Size(32*2,16*2)); //Size(80,72) ); resize(img, img, Size(64,48) ); //Size(32*2,16*2)); //Size(80,72) ); //gray cvtColor(img, img_gray, CV_RGB2GRAY); //Extract HogFeature HOGDescriptor d( Size(32,16), Size(8,8), Size(4,4), Size(4,4), 9); vector< float> descriptorsValues; vector< Point> locations; d.compute( img_gray, descriptorsValues, Size(0,0), Size(0,0), locations); //vector to Mat Mat fm = Mat(descriptorsValues); //Classification whether data is positive or negative int result = svm.predict(fm); printf("%s - > %d\n", FullFileName, result); //Count data if(result == 1) ppp++; else nnn++; //show image imshow("origin", img); waitKey(5); } printf(" positive/negative = (%d/%d) \n", ppp, nnn); }---
μλ νμΈμ" λ μ§λ¬Έλλ¦¬κ² λλ€μ;;
ReplyDeleteμμ΄λ‘ λ΅λ³ν΄μ£Όμλλ°;; μ§λ¬Έμ κ³μ νκΈλ‘ λλ € μ£μ‘ν©λλ€;;
λΈλ‘κ·Έμ μ€λͺ ν΄μ£Όμ λλ‘ λ°λΌ νλ©΄μ trainedSVM.xml νμΌμ μ»μ μ μμμ΅λλ€.
μ xmlνμΌμ μ¬μ©ν΄μ 보νμλ₯Ό κ²μΆν΄λ³΄κ³ μ ν©λλ€. κ·Έλμ
HOGDescriptor hog;
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
--> hog.load("trainedSVM.xml");
getDefaultPeopleDetector()λ₯Ό μ¬μ©νμ§ μκ³ νμ΅ν xmlμ μ¬μ©νκΈ° μν΄μ
μμ κ°μ΄ μμ νμμ΅λλ€. κ·Έλ¦¬κ³ hog.detectMultiScale()μ μννμλλ°
μλ¬κ° λ°μνμμ΅λλ€;; μ€κ°μ λ€λ₯Έ μ½λκ° νμνμ§... μλλ©΄... νμ΅λ xmlλ°μ΄ν° ν¬λ©§μ μμ ν΄μΌ νλμ§;; κΆκΈν©λλ€...
HOGDescriptor hog;
hog.load("trainedSVM.xml");
hog.detectMultiScale(matImage, Peds, 0, cv::Size(8, 8), cv::Size(0, 0), 1.05, 1);
for(int i=0; i<(int)Peds.size(); i++)
{
rectangle(matImage, Peds[i], Scalar(0,255,0), 1);
}
μ΄λ° μμΌλ‘ μ¬μ©νμ΅λλ€.
OpenCV Error : Assertion failed (dsize.area() || (in_scale_x >0 && inv_scale_y > 0))
in cv::resize, file ..\..\..\soureces\modules\imgproc\src\imgwarp.cpp, line 1835
μλ¬λ μμ κ°μ΄ λ°μνμ΅λλ€;;
μ€λλ λ§μ΄ λ°°μκ°λλ€. κ°μ¬ν©λλ€.
This comment has been removed by the author.
ReplyDeleteYou need to convert "trainedSVM.xml" file learned by SVM to 1D vector for using function of hog.detectMultiscale().
DeleteAfter training by SVM, use this source code.
--
//make firstly, inherited class to access alpha vector and value
int svmVectorSize = svm.get_support_vector_count();
int featureSize = pCol;
//prepare, variables
Mat sv = Mat(svmVectorSize, featureSize, CV_32FC1, 0.0);
Mat alp = Mat(1, svmVectorSize, CV_32FC1, 0.0);
Mat re = Mat(1, featureSize, CV_32FC1, 0.0);
Mat re2 = Mat(1, featureSize+1, CV_32FC1, 0.0);
//set value to variables
for(int i=0; i< svmVectorSize; ++i)
memcpy( (sv.data + i*featureSize), svm.get_support_vector(i), featureSize*sizeof(float) ); //ok
double * alphaArr = svm.get_alpha();
int alphaCount = svm.get_alpha_count();
for(int i=0; i< svmVectorSize; ++i)
{
alp.at< float>(0, i) = (float)alphaArr[i];
//printf("alpha[%d] = %lf \n", i, (float)alphaArr[i] );
}
//cvMatMul(alp, sv, re);
re = alp * sv;
for(int i=0; i< featureSize; ++i)
re2.at< float>(0,i) = re.at< float>(0,i) * -1;
re2.at< float>(0,featureSize) = svm.get_rho();
//save to 1d vector to XML format!!
FileStorage svmSecondXML(SVM_HOGDetectorFile, FileStorage::WRITE);
svmSecondXML << "SecondSVMd" << re2 ;
svmSecondXML.release();
--
SVM_HOGDetectorFile means new name of xml file.
Deleteex) AA.xml
thank you
λ΅λ³ν΄μ£Όμ μ κ°μ¬ν©λλ€!!
DeleteμΌμΉμμ§λ§...;; λ μ§λ¬Έλ립λλ€;;
http://feelmare.blogspot.kr/2014/04/example-source-code-hog-feature-to.html
λΈλ‘κ·Έμ μ λ΄μ©μ μ΄μ΄μ μ¬λ €μ£Όμ μ½λλ₯Ό μμ±νμκ³ ,
μ λ opencv 2.4.9. λ²μ μ μ¬μ©ν΄μ libλ§ 2.4.7μμ 2.4.9. λ‘ λ°κΏμ μμ λ₯Ό μ€ν보μμ΅λλ€
μ μ½λμμ svmꡬ쑰체μ λ©€λ²(?)λ₯Ό μ½μ§ λͺ»νλ λΆλΆμ΄ λ°μνμμ΅λλ€. svm.get_alpha(), svm.get_alpha_count(), svm.get_rho()
μ΄ μΈ κ°μ§μ λλ€.
κ·Έλ¦¬κ³ SVM_HOGDetectorFileμ΄ μλ‘μ΄ *.xmlνμΌμ μ΄λ¦μ΄λΌκ³ νμ ¨λ
λ°, μ΄λΆλΆλ μλ¬κ° λ°μν©λλ€;;
λμμ£Όμ μ κ°μ¬ν©λλ€""
I'm sorry, I don't teach you exact description.
DeleteI will post about the article(hog.detectMultiscale) tomorrow.
Thank you.
λ€" λ΅λ³ κ°μ¬ν©λλ€""
Deleterefer to this page
Deletehttp://feelmare.blogspot.kr/2014/11/opencv-svm-learning-method-and-xml.html
thank you.
Hey JH, I still got the problem "OpenCV Error : Assertion failed (dsize.area() || (in_scale_x >0 && inv_scale_y > 0))
Deletein cv::resize, file ..\..\..\soureces\modules\imgproc\src\imgwarp.cpp, line 1835" after used 1D vector.
you have any suggestion?
Hey i have problem , when i get to svm.predict it throw me some exception. Looks like its even don t load svm from xml.
ReplyDeleteAny idea why ? I just copypaste your codes, even extracting hog and train svm
Could you show your code or send source code to me?
DeleteThank you.
Oh... i find out that i changed size of hog descriptor in training source code(that was the problem - i am new in this so i am littlebit confused in some things)...
DeleteJust if i could ask another things:
Should be training images for svm in same aspect ratio ? I saw in source codes befor ... that you resizing them, so i gues it should be in same ratio as you resize them.Am i right ?
Nex question: after i have converted it in 1d vector like in upper comments i just load big image , use detectmultiscale and it returns some vector of rectangles(or what is return of detectmultiscale)? Didn t find topic of it here, but maybe i am blind.
Sorry for too much questions. By the way a really appreciate this site and all codes here. I learned much more than anywhere else.
Thank you
Sorry there is topic, but with gpu, so i will check it
DeleteHi, how can I use this algorithm to see exactly what kind of images I have ? For example If I trained 30 photo with cars, 30 with planes and 30 with houses, and another 100 negative images, when I read an image with a car to figure out that in my pictures I have a car. Here we find out only if it is a positive picture or negative. Thanks.
ReplyDeleteμλ νμΈμ μ λ μμμ²λ¦¬λ₯Ό λ°°μ°κ³ μλ νμμ λλ€.
ReplyDeleteSVM νμ΅μ λλ΄κ³ xmlνμΌμ setSVMDetector()μ μ μ© μν€κ³ μΆμλ° κ·Έ λΆλΆμ΄ μ΄λ ΅μ΅λλ€.
μ¬λ¦¬μ μμ€ μ½λλ₯Ό 보면 svm["SecondSVMd"]λΌλ κ²μ xMatμ λ£μ ν vectorμ dataλ₯Ό μ μ₯νμλλ° μ λ xmlνμΌμ μμ±ν λ svm.saveλ₯Ό μ΄μ©ν΄μ "SecondSVMd"λΌλ κ²μ΄ xmlνμΌμ μμ΅λλ€.
νΉμ μ΄λ° κ²½μ°μ νμ΅μν¨ λ°μ΄ν°λ₯Ό μ΄λ»κ² λΆλ¬μ¬ μ μλμ§ μλ €μ£Όμ€μ μλμ..? λΆνλ립λλ€.
This comment has been removed by the author.
ReplyDelete