4/18/2014

To test SVM trained data is whether reliable or not. (example source code)

After training SVM, we should test the trained XML data is reliable or not..

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);

}
---


Example source code Hog feature to learning by SVM, (SVM, HOGdescriptor)

After extract HOG feature from images, we have to learn for classify data.
This post introduces the method to use machine learning of SVM.

Firstly, you have to prepare postive hog features data and negative hog features data by XML, or TXT, and so on...
The method to get Hog feature, refer to this page -> http://feelmare.blogspot.kr/2014/04/example-source-code-of-extract-hog.html

I already got postive.xml and negative.xml from images.
This example source code learn using SVM from postive.xml, negative.xml.
And save the result fo learning to XML file.

After get SVM trained xml data, we can classify input data whether  positive or not.

This is training source code using SVM.

#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()
{
 
 //Read Hog feature from XML file
 ///////////////////////////////////////////////////////////////////////////
 printf("1. Feature data xml load\n");
 //create xml to read
 FileStorage read_PositiveXml("Positive.xml", FileStorage::READ);
 FileStorage read_NegativeXml("Negative.xml", FileStorage::READ);

 //Positive Mat
 Mat pMat;
 read_PositiveXml["Descriptor_of_images"] >> pMat;
 //Read Row, Cols
 int pRow,pCol;
 pRow = pMat.rows; pCol = pMat.cols;

 //Negative Mat
 Mat nMat;
 read_NegativeXml["Descriptor_of_images"] >> nMat;
 //Read Row, Cols
 int nRow,nCol;
 nRow = nMat.rows; nCol = nMat.cols;

 //Rows, Cols printf
 printf("   pRow=%d pCol=%d, nRow=%d nCol=%d\n", pRow, pCol, nRow, nCol );
 //release
 read_PositiveXml.release();
 //release
 read_NegativeXml.release();
 /////////////////////////////////////////////////////////////////////////////////

 //Make training data for SVM
 /////////////////////////////////////////////////////////////////////////////////
 printf("2. Make training data for SVM\n");
 //descriptor data set
 Mat PN_Descriptor_mtx( pRow + nRow, pCol, CV_32FC1 ); //in here pCol and nCol is descriptor number, so two value must be same;
 memcpy(PN_Descriptor_mtx.data, pMat.data, sizeof(float) * pMat.cols * pMat.rows );
 int startP = sizeof(float) * pMat.cols * pMat.rows;
 memcpy(&(PN_Descriptor_mtx.data[ startP ]), nMat.data, sizeof(float) * nMat.cols * nMat.rows );
 //data labeling
 Mat labels( pRow + nRow, 1, CV_32FC1, Scalar(-1.0) );
    labels.rowRange( 0, pRow ) = Scalar( 1.0 );
 /////////////////////////////////////////////////////////////////////////////////

 //Set svm parameter
 /////////////////////////////////////////////////////////////////////////////////
 printf("4. SVM training\n");
 CvSVM svm;
 CvSVMParams params;
 params.svm_type = CvSVM::C_SVC;
    params.kernel_type = CvSVM::LINEAR;
    params.term_crit = cvTermCriteria( CV_TERMCRIT_ITER, 10000, 1e-6 );
 /////////////////////////////////////////////////////////////////////////////////

 //Training
 /////////////////////////////////////////////////////////////////////////////////
 svm.train(PN_Descriptor_mtx, labels, Mat(), Mat(), params);

 //Trained data save
 /////////////////////////////////////////////////////////////////////////////////
 printf("5. SVM xml save\n");
 svm.save( "trainedSVM.xml" );
 
// FileStorage hogXml("testXML.xml", FileStorage::WRITE); //FileStorage::READ
// write(hogXml, "Data", PN_Descriptor_mtx);
// write(hogXml, "Label", labels);
// hogXml.release();
}
---

After learning, the method to classify is refer to http://feelmare.blogspot.kr/2014/04/to-test-svm-trained-data-is-whether.html

4/17/2014

OpenCV Study, Merging to extended Mat from 2 Mat (example source code)

Example of merging to extended Mat from 2 Mat

For example,
A=[1 2 3; 4 5 6];
B=[7 8 9; 3 2 1];
C=[A; B];  < - how to make this merging Mat??

refer to this example source code.

---

Mat A(3, 10, CV_32F);
 Mat B(4, 10, CV_32F);

 int cnt=0;
 for(int i=0; i< A.rows; ++i)
 {
  for(int j=0; j< A.cols; ++j)
  {
   A.at< float>(i,j) = float(i*j);
  }
 }

 cout << "A" << endl;
 cout << A << endl << endl;
 
 for(int i=0; i< B.rows; ++i)
 {
  for(int j=0; j< B.cols; ++j)
  {
   B.at< float>(i,j) = float(i*j)*10;
  }
 }

 cout << "B" << endl;
 cout << B << endl << endl;
 


 Mat C(A.rows + B.rows, A.cols, CV_32F);
 memcpy(C.data, A.data, sizeof(float) * A.cols * A.rows );
 int startP = sizeof(float) * A.cols * A.rows;
 memcpy(&(C.data[ startP ]), B.data, sizeof(float) * B.cols * B.rows );

 cout << "C = [A; B]" << endl;
 cout << C << endl << endl;


...
The result of example source code..
 

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();

}



...


same with above code.
< gist code start>

< gist code end >


The example source code of 2d vector write and read to the XML file, using OpenCV ( and also introcuded 2D vector conver to Mat and Mat to 2d Vector converting example source code)

1d vector convert to Mat
Mat convert to 1d vector
refert to this post http://feelmare.blogspot.kr/2014/01/opencv-vector-to-mat-mat-to-vector.html


This post is about 2d vector write to the XML file.
And read the XML file and data assigned to 2d vector again.

To complete this process, I use 2d vector convert to Mat and Mat convert to 2D vector.
The method to converting 2D vector, Mat is like that..

...
//2D vector to Mat
//create Mat   
Mat M(row,col,CV_32F);
//copy 2d vector to mat  
for(int i=0; i< row; ++i)  
memcpy( &(M.data[col * i * sizeof(float) ]) ,vv_Test[i].data(),col*sizeof(float)); 



//Mat to 2D vector
//copy from Mat to 2d Vector
for(int i=0; i< row; ++i)
{
 vector< float > temp;
 int start=col * i * sizeof(float);
 int end=start + col*sizeof(float)-1;
 temp.assign( (float*)(&(M2.data[start])), (float*)(&(M2.data[end])) );
 vv_Test2.push_back(temp);
}
---





This example source is save 2d vector to XML and read xml and copy to 2D vector variable.
...
#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()
{
 /////////////////////////////////////////////////////////////
 ////Write xml example

 //variables
 vector< vector < float > > vv_Test;
 int row = 5, col = 10;
 //make vector values
 for(int i=0; i< 5; ++i)
 {
  vector< float > vTest;
  for(int j=0; j< 10; ++j)
   vTest.push_back(i*j);

  vv_Test.push_back( vTest );
 }

 //create xml to write
 FileStorage write_hogXml("V_writeTest.xml", FileStorage::WRITE); //FileStorage::READ
 //create Mat   
 Mat M(row,col,CV_32F);
 //copy 2d vector to mat  
 for(int i=0; i< row; ++i)  
  memcpy( &(M.data[col * i * sizeof(float) ]) ,vv_Test[i].data(),col*sizeof(float)); 
 //write xml
 write(write_hogXml, "vectorTest",  M);
 //release
 write_hogXml.release();


 ///////////////////////////////////////////////////////////////////////////
 //read xml example 
 //create xml to read
 FileStorage read_hogXml("V_writeTest.xml", FileStorage::READ); //FileStorage::READ
 //Create Mat
 int row2,col2;
 //create Mat, 2d vector
 Mat M2; 
 vector< vector < float > > vv_Test2;
 //read data into Mat
 read( read_hogXml["vectorTest"], M2);
 row2 = M2.rows;
 col2 = M2.cols;
 printf("%d %d\n", row2, col2);
 //read_hogXml["vectorTest"] >> M2; //same 
 //copy from Mat to 2d Vector
 for(int i=0; i< row2; ++i)
 {
  vector< float > temp;
  int start=col2 * i * sizeof(float);
  int end=start + col2*sizeof(float)-1;
  temp.assign( (float*)(&(M2.data[start])), (float*)(&(M2.data[end])) );
  vv_Test2.push_back(temp);
 }
 //release
 read_hogXml.release();
 

 ///////////////////////////////////////////////////////////////////////////////////
 printf("read data confirm!! \n");
 for(int i=0; i< vv_Test2.size(); ++i)
 {
  vector< float > vTest;
  for(int j=0; j< vv_Test2[i].size(); ++j)
   printf("%.0f ", vv_Test2[i][j] );

  printf("\n");
 }

}

---

result of save XML file.
 
result of read XML and print 2D vector