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?

user2614596
  • 620
  • 2
  • 10
  • 28
  • Search for "binary IO in C/C++" in Google. – R Sahu Jan 27 '15 at 16:45
  • possible duplicate of [Reading and writing binary file](http://stackoverflow.com/questions/5420317/reading-and-writing-binary-file) – Mgetz Jan 27 '15 at 16:50
  • 2
    Search the web for "c++ serialization images". – Thomas Matthews Jan 27 '15 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 '15 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 '15 at 20:15
  • It seems different case to store multiple images in one file. Given that file, how can you say how to deserialize images from it without any knowledge where are boundaries between them in that binary file? You need sth like separator/delimater etc or fixed size. – marol Jan 27 '15 at 20:18
  • or you could just output the number of images, then prefix the binary data with the size – Mgetz Jan 27 '15 at 20:20

2 Answers2

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

GPPK
  • 6,019
  • 4
  • 29
  • 54
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/

tebe
  • 316
  • 1
  • 8