Mat image (ROW, COL, CV_TYPE); image.at (WANT_ROW, WANT_COL); - ROW: Row - COL: Column - CV_TYPE: data type ( for example : CV_8UC3 = 8 bit 3 channels) - DATA_TYPE: Mat creation data type ( for example : float, usigned char) - WANT_ROW: access to the desired row - WANT_COL: access to the desired column [Advantage]: Access after validation progress , so safe and accurate approach . [Disadvantage]: most slow in 3 ways. 2. Ptr approach Mat image (ROW, COL, CV_TYPE); image.ptr (WANT_ROW, WANT_COL); (This access is changed to the Point) - ROW: Row - COL: Column- CV_TYPE: data type ( for example : CV_8UC3 = 8 bit 3 channels) - DATA_TYPE: Mat creation data type ( for example : float, usigned char) - WANT_ROW: access to the desired row - WANT_COL: access to the desired column [Advantage]: Access is faster than first way. [Disadvantage]: direct access to the data , but way slower than third way .
3. Data approach Mat image (ROW, COL, CV_TYPE); DATA_TYPE * data = (DATA_TYPE *) image.data;data [WANT_ROW * image.cols + WANT_COL] - ROW: Row - COL: Column - CV_TYPE: data type ( for example : CV_8UC3 = 8 bit 3 channels) - DATA_TYPE: Mat creation data type ( for example : float, usigned char) - WANT_ROW: access to the desired row - WANT_COL: access to the desired column [Advantage]: very fast. [Disadvantage]: not check validation , it is hard to know inadequate access.
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();
}
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");
}
}
Firstly, to obtain 2 adjacent images
extract good feature to track.
Match each features between 2 images
Get Homography matrix
Warp current image to old image by using H.
However, H is multiplied by cumulatively.