0

i'm using a code which computes SIFT descriptors from an image. I have to process each frame from a video and store the sequence of each computed descriptor. For each image the descriptor is made by two arrays:

Frames = (double*)realloc(Frames, 4 * sizeof(double)* nframes); 
Descr = (vl_uint8*)realloc(Descr, 128 * sizeof(vl_uint8)* nframes);

how can i save the sequence of these two arrays in a file and then recover these data (for each frame of the video) from the file?

7
  • Search for "binary IO in C/C++" in Google.
    – R Sahu
    Jan 27, 2015 at 16:45
  • possible duplicate of Reading and writing binary file
    – Mgetz
    Jan 27, 2015 at 16:50
  • 2
    Search the web for "c++ serialization images". Jan 27, 2015 at 16:58
  • @Mgetz what OP is asking is that you have to store multiple images as the file,not single one, so I would not call it duplicate. Thomas has a good idea of serialization.
    – marol
    Jan 27, 2015 at 20:13
  • @marol fundamentally what the OP is asking for is to save binary data to a file. It is a duplicate.
    – Mgetz
    Jan 27, 2015 at 20:15

2 Answers 2

2

You can use ofstream to write to a file. The below code, which probably doesn't work straight out the can, should point in the right direction.

#include <fstream>
#include <iterator>
#include <algorithm>

void writeDescriptors() {

    std::ofstream output( "C:\\descriptors.txt", std::ios::binary );
    for ( int i = 1 ; i < Frames.size(); i++ )
    {
        out << ",  " << Frames[i];
    }
}

In order to read the descriptors back in, simply use ifstream and reverse the algorithm

2
  • Write should work, what about read? Just "reversed" algorithm, like std::ifstream?
    – marol
    Jan 27, 2015 at 20:11
  • 1
    Seems fine to me, +1. What about editing the answer with that remark?
    – marol
    Jan 27, 2015 at 20:14
0

Try looking for fwrite and fread.

If you have a variable number of frames (nframes) it might help to use the first 2*4bytes in the file to store the exact number of frames the file contains.

EDIT: http://www.cplusplus.com/reference/cstdio/fwrite/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.