0

I have a custom structure:

struct custom_roi{
    Mat roi;
    int x;
    int y;
};

I'm trying to save that structure to a binary file in c++. I'm using the following code:

    ofstream wf;
    wf.open("roi.dat", ios::out | ios::binary);

    if (!wf) {
        cout << "Cannot open file!" << endl;
        return 1;
    }

    string image_path = "D:/images/pic1.jpg";

    Mat LoadedImage;
    LoadedImage = imread(image_path, IMREAD_COLOR);

    FileStorage fs;

    custom_roi wsf;
    Rect Rec(100, 100, 400, 400);
    Mat roi = LoadedImage(Rec);

    wsf.x = 100;
    wsf.y = 100;
    roi.copyTo(wsf.roi);

    wf.write((char*)&wsf, sizeof(custom_roi));

    wf.close();

Quick view custom_roi variable

When I try to read the file back the x, y and the roi dimensions values are correct but there is a message saying "Error reading characters of string". What am I doing wrong? Your help will be really appreciated. Thank you.

John Smith
  • 41
  • 1
  • 10
  • Saving pointer variables to file won't be useful data. What do you want to do by saving `Mat` to the file? – MikeCAT Aug 01 '20 at 16:26
  • Thank you for your reply. I want to use that specific ```Mat``` later and analyze the pixel values . – John Smith Aug 01 '20 at 16:30

1 Answers1

0

After a lot of research I found what I need:

fs.write(mat.ptr<char>(0), (mat.dataend - mat.datastart));

I found it here Efficiently load a large Mat into memory in OpenCV

John Smith
  • 41
  • 1
  • 10