And save descriptors to XML file.
The source code explain how to use HOGDescriptor function.
..
#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"; char SaveHogDesFileName[100] = "Positive.xml"; int FileNum=96; vector< vector < float> > v_descriptorsValues; vector< vector < Point> > v_locations; 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(64,48) ); //Size(64,48) ); //Size(32*2,16*2)); //Size(80,72) ); //gray cvtColor(img, img_gray, CV_RGB2GRAY); //extract feature 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); //printf("descriptor number =%d\n", descriptorsValues.size() ); v_descriptorsValues.push_back( descriptorsValues ); v_locations.push_back( locations ); //show image imshow("origin", img); waitKey(5); } //refer to this address -> http://feelmare.blogspot.kr/2014/04/the-example-source-code-of-2d-vector.html //save to xml FileStorage hogXml(SaveHogDesFileName, FileStorage::WRITE); //FileStorage::READ //2d vector to Mat int row=v_descriptorsValues.size(), col=v_descriptorsValues[0].size(); printf("col=%d, row=%d\n", row, col); Mat M(row,col,CV_32F); //save Mat to XML for(int i=0; i< row; ++i) memcpy( &(M.data[col * i * sizeof(float) ]) ,v_descriptorsValues[i].data(),col*sizeof(float)); //write xml write(hogXml, "Descriptor_of_images", M); //write(hogXml, "Descriptor", v_descriptorsValues ); //write(hogXml, "locations", v_locations ); hogXml.release(); }...
same with above code.
< gist code start>
< gist code end >
This comment has been removed by the author.
ReplyDelete์๋ ํ์ธ์"" ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ์กฐํด์ Positive, Negative data๋ฅผ ๋ง๋ค๊ณ SVM tratining์ ํด๋ณด๋ ค๊ณ ํ๊ณ ์์ต๋๋ค.
ReplyDelete์ ๊ณตํด์ฃผ์ ์ฝ๋๋ก Positive.xml์ ์์ฑํด๋ณด์๋๋ฐ, Negative.xml๋ ๋ง์ฐฌ๊ฐ์ง๋ก ํ๋ฉด ๋๋๊ฑด์ง ๊ถ๊ธํด์ ์ง๋ฌธ๋๋ฆฝ๋๋ค. ์๋๋ฉด Negative.xml์ ์ด๋ฏธ์ง์ ์ด๋ฆ๋ง ์ ์ด์ฃผ๋ฉด ๋๋๊ฑด๊ฐ์??
๊ทธ๋ฆฌ๊ณ , ์ฌ๋์ ๊ฒ์ถํ๊ณ ์ ํ๋ค๋ฉด Positive.xml ์์ฑ์์ ์ฌ์ฉ๋๋ ์ด๋ฏธ์ง๋ ์ด๋ฏธ์ง์์ ์ฌ๋๋ง ์๋ผ์ ์ฌ์ฉํด์ผ ํ๋๊ฑด๊ฐ์?
Negative ์ด๋ฏธ์ง๋ ์ฌ๋์ด ์๋ ์ด๋ฏธ์ง๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํด์ผํ๊ตฌ์?
๋์์์ด ์ง๋ฌธ๋๋ ค ์ฃ์กํฉ๋๋ค;;
You also should make negative.xml file.
ReplyDeleteThe negative image should not contain positive image ex) face, human body...
positive.xml have to make similar images.
refer to this weg page" http://feelmare.blogspot.kr/search/label/Data"
negative.xml was made by natural images not include positive images.
and that is good way using background image of actual testing. then you can avoid false positive detection of you predicted.
->์ค์ ์ ์ฉํ ๊ณณ์ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค. ๊ทธ๋ฌ๋ฉด ์ค๊ฒ์ถ ์์ ๋ถ๋ถ์ ์์จ ์ ์๊ฒ ์ฃ ..
thank you.
์๋ ํ์ธ์.
ReplyDelete๊ถ๊ธํ์ ์ด ์๊ฒจ ์ด๋ ๊ฒ ์ง๋ฌธ ๋๋ฆฝ๋๋ค.
์์ ๋ณด๋ฉด ="./images/upperbody"; ๋ถ๋ถ์ด ๋ฐ์ดํฐ์ ์์น ์ ๋ณด๊ฐ์๋ฐ
ํด๋์ธ๊ฐ์???
์ฝ๋ฉ์ ๊ณ์ ์ฒ๋ฆฌ๋์ง ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค๊ณ ํ๋ค์
๋ต๋ตํ๋ง์์ ์ฌ์ญค๋ด ๋๋ค
๋ค์์ ์ด๋ฏธ์ง๊ฐ ์๋ ํด๋์ฌ์ผํ๋์ง.. .xmlํ์์ ์๋ฃ์ฌ์ผํ๋์ง..
Delete๋ํ ์ด๋ฏธ์ง ์ฌ์ด์ฆ๋ ์๊ด์๋๊ฑด์ง... ๊ถ๊ธํฉ๋๋ค.
์ด๋ณด๊ฐ ์ด๋ฌํ ์ง๋ฌธํด์ ์ฃ์กํฉ๋๋ค.
์ง๊ธ ์์ค๋ ๋ค์์ ์ด๋ฏธ์ง๋ฅผ for๋ก ๋ฃจํ๋๋ฉด์ hough ํน์ง์ ์ถ์ถํ๊ณ ์๊ณ ์
Delete์์ ์ฌ์ด์ฆ๋ฅผ ๊ฐ๊ฒ ํ๊ธฐ ์ํด์ ๋ฃจํ์์์ resize๋ฅผ ํด์ฃผ๊ณ ์์ต๋๋ค.
hough ํน์ง์ ๋๊ฐ์ ์ฌ์ด์ฆ๋ก ๋ฝ๊ณ ๊ทธ๊ฒ์ ํ์ตํ ๋ ์ฌ์ฉํฉ๋๋ค.
๊ฐ์ฌํฉ๋๋ค.
"./images/upperbody"; ๋ ํด๋๊ฐ ๋ง๊ณ ์
Deletesprintf_s(FullFileName, "%s%d.png", FirstFileName, i+1);
์ด ๋ฌธ์ฅ์ ์ํด์ ํ์ผ๋ช ์ด ๋ง๋ค์ด ์ง๋๋ค.
๋ค์์ ํ์ผ์ด ์์๋๋ก ์ ๋ฆฌ ๋์ด ์์ต๋๋ค.
See detail in for loop.
ReplyDeleteHow to use FirstFileName char(line 47)
And Why code use resize function(line 55)
Increase your code reading skill.
Cheer up!
thx for this example
ReplyDeleteThank you very much.
DeleteThis comment has been removed by the author.
ReplyDeleteHi, I wonder if there is a version of this code that can be implemented in Android studio?
ReplyDeleteEm.. actually I don't know android environment.
Deletebut even though android, that can load xml file, may be.
so first make xml file on pc, and after load xml on android.
Thank you.
Library Link Error!! Do You Kindly Share Complete Package Of Source Code
ReplyDeletesorry, do you meet opencv library link error?
DeleteDo you use 2.xx version like me?
Thank you.
์๋ ํ์ธ์.
ReplyDelete๊ถ๊ธํ์ ์ด ์์ด์ ์ง๋ฌธ๋๋ฆฝ๋๋ค.
๋ง์ง๋ง์ hogXml.release();๋ฅผ for๋ฌธ ์์ ๋ฃ์ผ๋ฉด ์ ์ฅํ ๊ณต๊ฐ์ด ์๋ค๋ error ๋ฉ์ธ์ง๊ฐ ๋จ๋๋ฐ
for๋ฌธ ๋ฐ์ผ๋ก ๊บผ๋ด์ write๋ฅผ ๋ค ๋๋ธ ํ์ release๋ฅผ ํ๋ฉด ์๋๋๊ฑด์ง ๊ถ๊ธํฉ๋๋ค.
๊ทธ๋ฆฌ๊ณ ํด๋ณธ ๊ฒฐ๊ณผ ์๊ฐ์ด ์ค๋ ๊ฑธ๋ฆฌ๋๋ฐ ์ค๋ ๊ฑธ๋ฆฌ๋๊ฒ ๋ง๋๊ฑด์ง...๊ถ๊ธํฉ๋๋ค.
์์ง ๋ฐฐ์ฐ๊ณ ์๋ ๋จ๊ณ์ฌ์ ์ฃ์กํฉ๋๋ค.
๋ง์ํ์ ๋ฐฉ๋ฒ์ด ๋ง๋ ๊ฒ ๊ฐ์ต๋๋ค.
Delete์ฝ๋ ๋ผ์ธ์ด ๋ณด๊ธฐ ํ๋ค์ด์ gist๋ก ๋ค์ ์ฌ๋ ธ์ต๋๋ค.
๊ฐ์ฌํฉ๋๋ค.
์๋ ํ์ธ์. ๊ถ๊ธํ์ ์ด ์์ด ์ง๋ฌธ๋๋ฆฝ๋๋ค.
ReplyDeletehog.compute ํจ์๋ฅผ ์จ์ ์ ๊ฐ ์ํ๋ ๊ฐ์ฒด์ hog ํน์ง์ ๋ฝ๊ณ ํ์ตํ๋๋ฐ ์ ์ด์ฉํ๊ณ ์์๋๋ฐ, ๋ฌธ์ ๊ฐ ์๊ฒผ์ต๋๋ค.
์ข๋ ์์ธํ feature ๋ฅผ ๋ฝ๊ณ ์ถ์ด์ ํฌ๊ฒ ๋ฝ๊ณ ์ถ์ด์ ๋ค์๊ณผ ๊ฐ์ด ์ฝ๋๋ฅผ ์งฏ๋๋ฐ์. ์ ์ ์ด๋ก ์์ผ๋ก๋ ํ์คํ๋ฐ ๋ญ๊ฐ ๋ฌธ์ ์ธ์ง ๋ชจ๋ฅด๊ฒ ๋ค์. compute ํจ์๊ฐ window ์ฌ์ด์ฆ์ limitation ์ ๊ฑธ์ด๋ ผ๊ฑด์ง.. ์๋๋ฉด ์ ๊ฐ ์ ๋ง ์๋ชป ์ดํดํ๊ณ ์๋๊ฑด์ง ๋ชจ๋ฅด๊ฒ ์ด์ Mare๋์ ๋์์ ์ป์์ ์์๊น ์ถ์ด ์ด๋ ๊ฒ ์ง๋ฌธ์ ๋๋ฆฝ๋๋ค.
resize(mat_input, mat_input, Size(160,100));
cvtColor(mat_input, mat_gray, CV_RGB2GRAY);
์ด๋ฏธ์ง ์ฌ์ด์ฆ๋ ์์ 160,100 ์ผ๋ก ์ค์ ํ์์ต๋๋ค.
//extract feature
HOGDescriptor hogDescriptor(Size(160, 100), Size(40, 40), Size(20, 20), Size(20, 20), 9);
vector< float> vf_DescriptorsValues;
vector< Point> v_LocationPoint ;
cv::Size winStride(0, 0);
cv::Size winPadding(0, 0);
hogDescriptor.compute(mat_gray, vf_DescriptorsValues, winStride, winPadding, v_LocationPoint);
์ ๊ฐ ์๊ฐํ๊ธฐ๋ก๋ ๋ค์๊ณผ ๊ฐ์ด ์ค์ ์ ํ์ ๊ฒฝ์ฐ, 1008 feature dimension์ด ๋์ฌ๊ฒ์ด๋ผ ์์ํ๋๋ฐ,
128,80 ๊น์ง์ ์๋์ฐ ๊น์ง๋ ํ์ฉ์ ํ์์ผ๋, ๊ทธ ์ด์์ผ๋ก๋ ๊ณ์ฐ์ด ๋์ง ์์ต๋๋ค.
์ ๊ฐ ์ค์ ํ ์ฝ๋์ ๋ฌธ์ ๊ฐ ์๋๊ฒ์ธ๊ฐ์? ํ์ธ ๋ถํ๋๋ฆฝ๋๋คใ ใ
๋ฌธ์ ๊ฐ ์๋๊ฒ์ด๋ฉด, ์ ๋ 320,200์ ๋์ ์ด๋ฏธ์ง์์ hog feature๋ฅผ ๋ฝ๊ณ ์ถ์๋ฐ, ํน์ ํฐํฌ๊ธฐ์ ์์์์ ๋ฝ๋ ๋ฐฉ๋ฒ์ ์๊ณ ๊ณ์ ์ง ์ฌ์ญค๋ด๋ ๋ ๊น์?
๋ค ๊ฐ์ฌํฉ๋๋ค. hog ํน์ง ์์ฑํ ๋, ํ๋ผ๋ฏธํฐ๊ฐ ์ข ์ ๋ชป ๋๊ฒ ๊ฐ์๋ฐ์.. ์ข๋ ์์ธํ ์์๋ณด๋ ค๋ฉด ์ ๋ ์๊ฐ์ด ํ์ํ๊ฒ ๋ค์.. ์ ๋ hog ์ํ์ง ์ค๋๋์ด์ ๊ธฐ์ต์ด ๊ฐ๋ฌผ๊ฐ๋ฌผํ๊ฑฐ๋ ์.. ^^
Delete์ง๊ธ ์ ๋ฌด๋ ๋ง๊ณ ํด์.. ์ ๊ฐ ๋นจ๋ฆฌ ๋ต๋ณ ๋ชป๋๋ฆฌ๋ ์ ์ฃ์กํฉ๋๋ค.
how can i train my HOG descriptor to detect partial parts of the human body
ReplyDeleteyour meaning is, you want to create your own hog feature right?
ReplyDeleteand training by svm.
if that case, this post will help
http://study.marearts.com/2016/01/svm-hog-learning-and-detection-methods.html
Thanks for your interesting.
thank you #JH Kim for your help.
DeleteHy Mister, I'm new in HOG Feature, I want to ask, when i used hog.compute in a loop like u do, it always crash and throwing an exception, it said "Invalid_parameter:
ReplyDelete_SCL_SECURE_INVALID_ARGUMENT_NO_ASSERT;"
it only compute 1 image then always crashing in the next image and throw that exception (it can't looping). Please help me solve my problem here. Thanks
Hi, The first loop succeeded, but does it fail from the second?
DeleteDoes the image size change?
Thank you.
Thank you for your reply, I'm really sorry I can't reply faster, there are no change of the images except before calculating HOG, like u did above. This is my code, can you check it where I did wrong, Thank you for your response.
ReplyDeleteint main(int argc, const char* argv[])
{
string folder = "D:\\Project\\TA\\Data_Trainer\\Clip_Images\\Positif Training1\\pisang\\"; // folder input
string suffix = ".png";
int counter = 0;
Mat img, img_gray;
Mat img1, img2;
for (int i = 0; i < 10; i++) {
stringstream ss;
cout << "check point 0 " << endl;
ss << setw(4) << setfill('0') << counter; // 0000, 0001, 0002, etc...
//ss << counter; // 0000, 0001, 0002, etc...
string number = ss.str();
string name = folder + number + suffix;
img = imread(name);
if (img.empty()) cout << "No image Loaded" << endl;
else cout << "image " << counter << " Loaded Successfully" << endl;
cout << name << endl;
//Parameter
setWidth = 32, setHeight = 32;
//resizing
resize(img, img, Size(setWidth, setHeight)); //Size(64,48) ); //Size(32*2,16*2)); //Size(80,72) );
//GrayScaling
cvtColor(img, img_gray, CV_RGB2GRAY); //gray
//extract feature
HOGDescriptor hog(Size(setWidth, setHeight), Size(8, 8), Size(4, 4), Size(4, 4), 9);
// Size(32,16), //winSize
// Size(8,8), //blocksize
// Size(4,4), //blockStride,
// Size(4,4), //cellSize,
// 9, //nbins,
vector< float> descriptorsValues;
vector< Point> locations;
hog.compute(img_gray, descriptorsValues, Size(0, 0), Size(0, 0), locations);
// img, descriptor, winStride, Padding, Location
if (descriptorsValues.empty()) cout << "descriptorValues empty" << endl;
if (locations.empty()) cout << "location empty" << endl;
v_descriptorsValues.push_back(descriptorsValues);
v_locations.push_back(locations);
printf("row =%d ", v_descriptorsValues.size());
printf(" ,col =%d\n", v_descriptorsValues[0].size());
//show image
//imshow("origin", img);
waitKey(3);
cout << "loop: " << counter << endl;
counter++;
//img.release();
//img_gray.release();
cout << "check point 1" << endl;
descriptorsValues.clear();
locations.clear();
}
Hopefully anyone can help me about this, I really need your help :)
ReplyDelete