Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Save CV_16UC1 Mat to binary file

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 WidthHeightsizeof(type_to_which_I_converted) raw data. convertTo() not enough to change byte per pixel for Mat?

Save CV_16UC1 Mat to binary file

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 WidthHeightWidth * Height * sizeof(type_to_which_I_converted) raw data. convertTo() not enough to change byte per pixel for Mat?

Save CV_16UC1 Mat to binary file

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();

}

Save CV_16UC1 Mat to binary file

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): 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);

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();
}

}

Save CV_16UC1 Mat to binary file

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();
}