4/17/2014

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

This example source code is to extract HOG feature from images.
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();

}



...

10 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. 안녕하세요"" 블로그를 참조해서 Positive, Negative data를 만들고 SVM tratining을 해보려고 하고있습니다.
    제공해주신 코드로 Positive.xml은 생성해보았는데, Negative.xml도 마찬가지로 하면 되는건지 궁금해서 질문드립니다. 아니면 Negative.xml은 이미지의 이름만 적어주면 되는건가요??
    그리고, 사람을 검출하고자 한다면 Positive.xml 생성시에 사용되는 이미지는 이미지에서 사람만 잘라서 사용해야 하는건가요?
    Negative 이미지는 사람이 없는 이미지를 그대로 사용해야하구요?

    두서없이 질문드려 죄송합니다;;

    ReplyDelete
  3. You also should make negative.xml file.
    The 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
  4. 안녕하세요.
    궁금한점이 생겨 이렇게 질문 드립니다.
    위에 보면 ="./images/upperbody"; 부분이 데이터셋위치 정보같은데
    폴더인가요???
    코딩시 계속 처리되지 않은 오류가 발생한다고하네요
    답답한마음에 여쭤봅니다

    ReplyDelete
    Replies
    1. 다수의 이미지가 있는 폴더여야하는지.. .xml형식의 자료여야하는지..
      또한 이미지 사이즈도 상관있는건지... 궁금합니다.
      초보가 이러한 질문해서 죄송합니다.

      Delete
  5. See detail in for loop.
    How to use FirstFileName char(line 47)
    And Why code use resize function(line 55)
    Increase your code reading skill.
    Cheer up!

    ReplyDelete
  6. Anonymous6/5/16 10:54

    thx for this example

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Hi, I wonder if there is a version of this code that can be implemented in Android studio?

    ReplyDelete
  9. Library Link Error!! Do You Kindly Share Complete Package Of Source Code

    ReplyDelete