Ask Your Question

Cedric_K's profile - activity

2020-02-11 11:31:47 -0600 received badge  Famous Question (source)
2018-06-29 06:13:31 -0600 received badge  Notable Question (source)
2017-06-27 08:29:20 -0600 received badge  Popular Question (source)
2015-09-25 09:50:12 -0600 commented question Mat to raw Image

Thank you very much for your help. I was misunderstanding the channels 8-bit coding. Your comment makes sense. I will post feedbacks if I can figure out the raw vizualisation issue.

2015-09-25 05:53:47 -0600 commented question Mat to raw Image

Thanks. I thought RGB was encoded into one byte (3 bits for R, 3 bits for G and 2 bits for B). So why 3 times the bytes ? I have updated my code with frame.total() * frame.elemSize(); but still no chance to visualize the raw image. I will anyway consider upgrading OpenCV (from legacy system) but still I do not understand why I cannot visualize that raw image.

2015-09-25 05:24:22 -0600 asked a question Mat to raw Image

Hello,

I am having trouble writing a raw image from a Mat. I am using OpenCV 2.3.1. I have seen plenty of replies for the inverse operation (raw to Mat) but not Mat to raw. I am not even sure it is possible the way I do it since no headers are set.

The following code opens a video, and saves the 10 first frames into files. When I try to open those file with XNview (for example), I get a black and striped image. Clearly not the expected image. The .raw file starts with plenty of 00 00 00 00 before the real content. I cannot figure out what I am missing. DO I need to add headers ?

Thanks a lot in advance

Here is the code :

int main( int argc , char *argv[] )
{
  unsigned long lFrameId = 0;

  // open video file
  string mediaPath = "/home/videos/film.mp4";
  VideoCapture captureV;
  Mat frame;

  captureV.open(mediaPath);

   if( !captureV.isOpened() )
   {
     cerr << "capture error" << endl;
     exit(1);
   }

  // set media parameters
  captureV.set(CV_CAP_PROP_CONVERT_RGB, true);

  while (lFrameId < 10
  {
    captureV.read(frame);

    int rows = frame.rows;      // 880)
    int cols = frame.cols;      // 720
    uchar* buffer = frame.data; // 8-bit pixel

    // Write frame data into raw file
    ostringstream filename;
    filename << "Frame_" << lFrameId << ".raw";
    std::ofstream outfile (filename.str().c_str(), ios::out | ios::binary);
    outfile.write ((char*)(buffer), frame.total());  // In byte so frame.total() should be enough ?
    outfile.close();

    ++lFrameId;
  }

  return 0;
}