0

I want to save every single frame of a real time video in .pgm format. I am working with opencv 2.4.8 in c++. My problem is that despite all frames are saved and phenomenically in the right format (for example as frame_1.pgm) , when I open them they are not in grayscale, as a .pgm photo should be. Moreover I opened the images with a hexeditor and compared them with a correct pgm photo and there are obvious differences!Can somebody give any advice on what I am doing wrong??

here is the crucial part of my code:

//////////////////////////////////////////////////////////////

vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PXM_BINARY);
compression_params.push_back(0);

   captureDevice1>> captureFrame1;


   imwrite("frame_55.pgm",captureFrame1,compression_params);


   //string imagename=argv[2];
   string imagename="frame_";
   //string imagename="frame_";
   char numberfile[17];
   sprintf_s(numberfile,17,"%u",im);
   imagename+=numberfile;
   imagename+=".pgm";

   std::ifstream in( imagename.c_str(), std::ios::binary );
   if ( !in )
   {
      std::cout << "Error in Image: " << imagename << std::endl;
      continue;
   }
   in.close();

///////////////////////////////////////////////////////////////////////

Now if i try to load the image:frame_55.pgm I get an "Unsupported format" error while it works fine with other .pgm images!!

Thnx in advance guys for your time!!!

m_papas
  • 91
  • 1
  • 12

1 Answers1

0

This is the corrected code:

//Device1

VideoCapture captureDevice1;
captureDevice1.open(0);

//setup files used in capture process
//Device1

Mat captureFrame1;
Mat grayscaleFrame1;



   for(int im=0; im<=numimages; im++){

    // Load the source image into our simple image class.


   captureDevice1>> captureFrame1;

   if(captureFrame1.empty())
   {
       cout<<"empty frame skipped"<<endl;
   continue;
    }


// name each frame as frame_numOframe

stringstream strs;
strs << im;
string temp_str = strs.str(); 
temp_str="frame_"+temp_str+".pgm";//assign coords into one string

//parameters for saving as .pgm

vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PXM_BINARY);
compression_params.push_back(0);

cvtColor(captureFrame1,grayscaleFrame1, CV_BGR2GRAY);//convert to grayscale
imwrite(temp_str,grayscaleFrame1);//save image
m_papas
  • 91
  • 1
  • 12