How to save keypoint vector using openCV

I was wondering if the cv :: KeyPoints vector was saved using the CvFileStorage class or the cv :: FileStorage class. Also is it the same process to read it back?

Thanks.

+8
c file-io opencv
source share
3 answers

I'm not sure what you really expect: The code I provide you with is just an example to show how file storage works in OpenCV C ++ bindings. It is assumed that you write all the key points separately in the XML file, and their name is their position in the vector in which they were saved.

It is also assumed that when you read them, you know the number of them that you want to read, and if not, the code is a bit more complicated. You will find a way (if, for example, you are reading a file and checking what it gives you, if it does not give you anything, then there’s no point in reading) - this is just an idea, you should find a solution, maybe this piece of code will be enough for you. I must clarify that I am using ostringstream to put an integer in a string and, by the way, change the place where it will be written in the * .yml file.

//TO WRITE vector<Keypoint> myKpVec; FileStorage fs(filename,FileStorage::WRITE); ostringstream oss; for(size_t i;i<myKpVec.size();++i) { oss << i; fs << oss.str() << myKpVec[i]; } fs.release(); //TO READ vector<Keypoint> myKpVec; FileStorage fs(filename,FileStorage::READ); ostringstream oss; Keypoint aKeypoint; for(size_t i;i<myKpVec.size();<++i) { oss << i; fs[oss.str()] >> aKeypoint; myKpVec.push_back(aKeypoint); } fs.release(); 

Julien

+7
source share
 char* key; FileStorage f; vector<Keypoint> keypoints; //writing write(f, key, keypoints); //reading read(f[key], keypoints); 
+7
source share
 int main() { String filename = "data.xml"; FileStorage fs(filename,FileStorage::WRITE); Vector<Mat> vecMat; Mat A(3,3,CV_32F, Scalar(5)); Mat B(3,3,CV_32F, Scalar(6)); Mat C(3,3,CV_32F, Scalar(7)); vecMat.push_back(A); vecMat.push_back(B); vecMat.push_back(C); //ostringstream oss; for(int i = 0;i<vecMat.size();i++) { stringstream ss; ss << i; string str = "x" + ss.str(); fs << str << vecMat[i]; } fs.release(); vector<Mat> matVecRead; FileStorage fr(filename,FileStorage::READ); Mat aMat; int countlabel = 0; while(1) { stringstream ss; ss << countlabel; string str = "x" + ss.str(); cout << str << endl; fr[str] >> aMat; if (fr[str].isNone() == 1) { break; } matVecRead.push_back(aMat.clone()); countlabel ++; } fr.release(); for( unsigned j = 0; j < matVecRead.size(); j++){ cout << matVecRead[j] << endl; } } 

Put a letter, for example, 'a' infront of numbering, as the OPENCV XML format indicates that the xml KEY should begin with a letter.

This is the code for saving Vector<Mat> for visual studio 2010, I think it will work for Vector<KeyPoints>

0
source share

All Articles