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