Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a vector of keypoints using openCV

Tags:

c

file-io

opencv

I was wondering if it was possible to save out a vector of cv::KeyPoints using the CvFileStorage class or the cv::FileStorage class. Also is it the same process to read it back in?

Thanks.

like image 793
Seb Avatar asked Jul 28 '11 20:07

Seb


People also ask

What is Point2f?

Point2f(Double, Double) Initializes a new two-dimensional point from two double-precision floating point numbers as coordinates. Coordinates will be internally converted to floating point numbers. This might cause precision loss.

How do I save an image in C++ OpenCV?

OpenCV provides imwrite() function to save an image to a specified file. The file extension represents the image format. Here, "Destination" is where we want to save the image. In this program, we save the image as "Lakshmi.

What are Keypoints in OpenCV?

The keypoint is characterized by the 2D position, scale (proportional to the diameter of the neighborhood that needs to be taken into account), orientation and some other parameters. The keypoint neighborhood is then analyzed by another algorithm that builds a descriptor (usually represented as a feature vector).


2 Answers

I am not sure about what you really expect : The code I provide you is simply an example, to show how the file storage works in the OpenCV C++ bindings. It assumes here that you write in the XML file all the Keypoints separately, with their name being their position in the vector they were stored in.

It assumes aswell that when you read them back, you know the number of them you want to read, if not, the code is a little bit more complex. You'll find a way (if for instance you read the filestorage and test what it gives you, if it doesn't give you anything, then it means there is no more point to read) -it's just an idea, you have to find a solution, maybe this piece of code will be enough for you. I should precise that i use ostringstream to put the integer in the 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,

like image 118
jmartel Avatar answered Sep 19 '22 13:09

jmartel


char* key;
FileStorage f;
vector<Keypoint> keypoints;

//writing 
write(f, key, keypoints);

//reading
read(f[key], keypoints);
like image 27
TheRisingEdge Avatar answered Sep 18 '22 13:09

TheRisingEdge