Save CV_16UC1 Mat to binary file [closed]

asked 2018-12-28 21:30:07 -0600

Bleach gravatar image

updated 2018-12-29 04:38:49 -0600

UPD. Everything works fine. The problem was with me with 3dparty software. Sorry.

I try to save Mat to binary file:

void WriteRAW(Mat &m)
{
    ostringstream filename;
    uchar* buffer = m.data;
    filename << "e:/1/Frame_" << lNoFrame++ << ".raw";
    std::ofstream outfile(filename.str().c_str(), ios::out | ios::binary);
    outfile.write((char*)(buffer), m.total() * m.elemSize());
    outfile.close();
}

It work fine for CV_8UC(x) Mat, but if I convert Mat to CV_16UC1 (also CV_32FC1 ...) nothing changes. The size of the output file does not change, it remains the same as it was at CV_8UC1. Of course I cant read it as Width * Height * sizeof(type_to_which_I_converted) raw data. convertTo() not enough to change byte per pixel for Mat?

upd. Function for read saved 512 * 512 CV_8UC1 file (work fine):

void ReadRAW(Mat &m)
{
    ostringstream filename;
    int iSize = 512 * 512;
    char * buffer = new char[iSize];
    filename << "e:/1/Frame_0.raw";
    std::ifstream infile(filename.str().c_str(), ios::in | ios::binary);
    if (!infile)
        return;
    infile.read(buffer, iSize);

    if (infile)
        std::cout << "all characters read successfully.";
    else
        std::cout << "error: only " << infile.gcount() << " could be read";

    m = Mat(Size(512, 512), CV_8UC1, buffer);
    infile.close();
}
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by berak
close date 2018-12-29 04:40:30.722007

Comments

1
sturkmen gravatar imagesturkmen ( 2018-12-29 00:36:27 -0600 )edit
1

i cannot reproduce your problem:

Mat m(10,10,CV_16U);
ofstream out("my.bin");
out.write((char*)m.data, m.elemSize()* m.total());
out.close();
int d = system("ls -l *.bin");

-rw------- 1 u38140 dyno 200 Dec 29 08:21 my.bin
berak gravatar imageberak ( 2018-12-29 02:23:10 -0600 )edit