0

I am facing the following problem. As I want to record and play back RAW image data from a camera, I am currently recording the images using

VideoWriter outputVideo1("camera1.avi", CV_FOURCC('Y','8','0','0'), frameRate, frameSize, false);

This works fine and I am able to play the video file using VLC player, e.g. MediaInfo also confirms, that the Format is GrayScale and the Codec ID is Y800.

Unfortunately, when I try to open this file in OpenCV using

VideoCapture cap1("camera1.avi");
Mat frame1;
for(;;)
{
cap1 >> frame1;
}

the program crashes inside the for loop with the exception

Unhandled exception at 0x715f39c2 in cam_interface.exe: 0xC0000005: Access violation reading location 0x010a0040.

When I use the Huffman lossless Codec ('H','F','Y','U'), I am able to open the AVI file. But as my data consists only of one channel, I only want to save one channel. Using the Huffman Codec for a monochrome image, the same value for all three channels are stored, which is not what I wanted to do.

If do appreciate any help or ideas on how to solve this.

Thank you very much!

  • have a look at this: http://answers.opencv.org/question/2133/videocapture-not-working-with-uncompressed-files/ – Micka Jan 08 '14 at 16:24
  • thank you, I used the search function but somehow I searched for the wrong buzzwords and didn't see that the thread is already open... – inexperiencedUser Jan 08 '14 at 16:51
  • that's because it's on opencv.org and not on stackoverflow =) But the presented workaround there might work for you, too. – Micka Jan 08 '14 at 17:04
  • this hint was extremely helpful! I was close to despair. thank you so much again! Is there no way to give you +1 in a comment? – inexperiencedUser Jan 08 '14 at 19:53
  • added link and Pierre's posting as an answer. Just in case that openCV answer gets lost it's in stackoverflow now. Hope that's ok, I don't feel so nice getting credit for someone else's work :D – Micka Jan 09 '14 at 14:55

1 Answers1

0

I found a workaround on http://answers.opencv.org where the user Pierre had the same problem and created the workaround himself. I'll repost the original answer found here: http://answers.opencv.org/question/2133/videocapture-not-working-with-uncompressed-files/

answer by Pierre:

The problem is not solved but I find a procedure to skirt this problem by reading images separately. First it is necessary to open the sequence to get the FOURCC code and information on the video file, size of images, number of images ...Then if the FOURCC corresponds to a non compressed AVI you close the sequence and re open it by classical C function fopen and jump the header, and then use the fread function to get each image. see the following code :

void CMFC_OpencvDlg::OnBnClickedLitSeqAvi()
{
 static CString Filter=_T("Video files (*.avi; *.mpg) |*.avi;*.mpg|All Files (*.*)|*.*||");
 CFileDialog Load(TRUE, _T("*.bitmap"), NULL, OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY,Filter,NULL); 
 Load.m_ofn.lpstrTitle= _T("Lecture video");
 int value = 0 ;
 double Height, Width, Nb_images, Code  ;
 FILE *Seq_Avi ;
 Mat Img ;
 if (Load.DoModal() == IDOK) 
 {
     img_path = Load.GetPathName() ;
     Nom_de_Sequence = Load.GetPathName() ; 
     Nom_de_Sequence.Truncate(Nom_de_Sequence.GetLength()-4) ;
     VideoCapture Sequence(img_path);
     Nb_images = Sequence.get(CV_CAP_PROP_FRAME_COUNT ) ;
     Width = Sequence.get(CV_CAP_PROP_FRAME_WIDTH ) ;
     Height = Sequence.get(CV_CAP_PROP_FRAME_HEIGHT ) ;
     Code = Sequence.get(CV_CAP_PROP_FOURCC ) ;
     Nb_Images_Seq = 0 ;
     if (Code == CV_FOURCC('Y','8','0','0')|| (Code == 0 ))
     {
         Sequence.release() ;  // Fermeture du fichier 
         char *tmp, c ;
         string Tag = "movi00d" ;
         int i =0 ;
         fpos_t pos;
         BOOLEAN Fin = TRUE ;
         tmp = (char *)malloc(Width*Height) ;
         fopen_s(&Seq_Avi,img_path.c_str(),"rb") ;
         do  // Gestion de la taille d'entête variable
         {
             fread(&c,1,1,Seq_Avi) ;
             if (c==Tag[i]) 
             {
                 i++ ;
                 if (i==7) Fin = FALSE ;
                 if (i==4)  fgetpos(Seq_Avi,&pos) ;
             }
             else i = 0 ;
         }
         while (Fin) ;
         fsetpos( Seq_Avi, &pos ) ;
         for (i= 0 ; i < Nb_images ;i++)
         {
             fread(tmp,1,8,Seq_Avi) ;  // Saut du séparateur, "00dc"+taille image
             fread(tmp,1,Width*Height,Seq_Avi) ;
             Img = Mat(Height, Width, CV_8U, tmp) ;
             Seq_image[Nb_Images_Seq] = Img.clone() ;
             if (Code == 0 ) flip(Seq_image[Nb_Images_Seq],Seq_image[Nb_Images_Seq],0 ) ;  // Lorsque le code est 0 les images sont stokées bas en haut 
             imshow("image", Seq_image[Nb_Images_Seq++]);
     //      AfxMessageBox(_T("Image suivante"),0,0) ;
         }
         fclose(Seq_Avi) ;
         free(tmp) ;
     }
     else
     {
     // Cette partie ne marche pas avec les AVI non compressés !!!!!
         while( Sequence.read(src)) //Relecture de out le fichier et traitement associé
         {
             Seq_image[Nb_Images_Seq++] = src ;
             imshow("image", src);
     //      AfxMessageBox(_T("Image suivante"),0,0) ;
         }
     }
 }
}
Micka
  • 19,585
  • 4
  • 56
  • 74