Ask Your Question
0

Grayscale avi to image

asked 2013-09-25 04:15:08 -0600

updated 2013-09-25 04:23:43 -0600

Moster gravatar image

Hi :) I would like to convert a grayscale avi to images. I use the following code but the produced images are all black. When I use a colour avi, it does work perfect but it doesn't work for grayscale avi. How can I solve this problem? Thanks for your help. Best; Beldachi


int main (int argc, char **argv)
{
   if (argc !=2)
   { 
   std::cout << "USE: " << argv[0] << " <video-filename>" << std::endl;
    return -1;
   }

  //Open the video that you pass from the command line
  cv::VideoCapture cap(argv[1]);
  if (!cap.isOpened())
  {
   std::cerr << "ERROR: Could not open video " << argv[1] << std::endl;
   return -1;
  }

  int frame_count = 0;
  bool should_stop = false;

  while(!should_stop)
  {
     cv::Mat frame;
     cap >> frame; //get a new frame from the video
     if (frame.empty())
     {
        should_stop = true; //we arrived to the end of the video
        continue;
     }

      char filename[128];
      sprintf(filename, "frame_%06d.jpg", frame_count);  
      frame.convertTo(frame,CV_8U); 
      cv::imwrite(filename, frame);
      frame_count++;
 }

 return 0;
}
edit retag flag offensive close merge delete

Comments

I fixed the code, so that it highlights correctly :)

Moster gravatar imageMoster ( 2013-09-25 04:24:22 -0600 )edit

Could you show which type number you get for frame.type() after you read a frame?

Moster gravatar imageMoster ( 2013-09-25 04:35:56 -0600 )edit

Hi Moster;

In both cases (gray scale and colour), the captured frames are CV_8U.

Best, Beldachi

beldachi gravatar imagebeldachi ( 2013-09-25 07:06:59 -0600 )edit

I'm actually interested in the number that you get from frame.type()

Moster gravatar imageMoster ( 2013-09-25 07:09:20 -0600 )edit

the number is 16 :)

beldachi gravatar imagebeldachi ( 2013-09-25 07:18:51 -0600 )edit

I just wanted to make sure. So your grayscale avi also has 3 channels, since 16 = CV_8UC3. Did you also check this before you did frame.converTo()? Im going to post some code and you tell me how it looks like.

Moster gravatar imageMoster ( 2013-09-25 07:55:51 -0600 )edit

yes, I checked it before frame.converTo()

beldachi gravatar imagebeldachi ( 2013-09-25 08:12:26 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2013-09-25 07:55:03 -0600

Moster gravatar image

updated 2013-09-25 08:30:45 -0600

int main (int argc, char **argv) { if (argc !=2) { std::cout << "USE: " << argv[0] << " <video-filename>" << std::endl; return -1; }

  //Open the video that you pass from the command line
  cv::VideoCapture cap(argv[1]);
  if (!cap.isOpened())
  {
   std::cerr << "ERROR: Could not open video " << argv[1] << std::endl;
   return -1;
  }

  int frame_count = 0;
  bool should_stop = false;

  while(!should_stop)
  {
     cv::Mat frame;
     if (cap.grab() {
        cap.retrieve(frame, 1);

        char filename[64];            
        sprintf(filename, "frame_%06d.jpg", frame_count);           

        cv::imwrite(filename, frame);

        frame_count++;
      } else {
        should_stop = true;
        continue;
      }
 }

 return 0;
}
edit flag offensive delete link more

Comments

I have tried it.I can see the diffrence in colour avi but the grayscale one gives me all black for r , b and g :)

beldachi gravatar imagebeldachi ( 2013-09-25 08:23:34 -0600 )edit

I edited it, tell me if that makes a difference.

Moster gravatar imageMoster ( 2013-09-25 08:30:16 -0600 )edit

no, it doesn't make any diffrence :)

beldachi gravatar imagebeldachi ( 2013-09-25 08:40:47 -0600 )edit

Can you upload one of your grayscale avi videos? Its really strange.

Moster gravatar imageMoster ( 2013-09-25 08:45:42 -0600 )edit

how can i send it to you as i can't upload a video here :)

beldachi gravatar imagebeldachi ( 2013-09-25 08:56:11 -0600 )edit

You cannot write PMs here. You could upload it somewhere and give a link here

Moster gravatar imageMoster ( 2013-09-25 09:31:26 -0600 )edit

Which opencv version are you using btw? I just tried to read those avi files and Im getting a segmentation fault. I also found a bug tracker related to that http://code.opencv.org/issues/1915

Moster gravatar imageMoster ( 2013-09-25 15:53:40 -0600 )edit

I use OpenCV 2.3.1.Yes it seems there is a problem with the avi files. I have downloaded some grayscale avi s from http://users.soe.ucsc.edu/~milanfar/software/sr-datasets.html and it works perfect. Could you please tell me how did you find out about the segmentation fault?

beldachi gravatar imagebeldachi ( 2013-09-26 01:58:16 -0600 )edit

I was using opencv 2.4.6 and you get that segmentation fault as soon as you try to load an AVI file that has no coded specified. It seems like opencv in general has problems with files like that. I also tried opencv 2.3.1 now and all images are black. I also checked the output that you get from the VidepCapture and all values are 0, hence the black images. Im afraid that you need to wait for a new opencv release that eventually fixes that.

Moster gravatar imageMoster ( 2013-09-26 05:17:13 -0600 )edit
0

answered 2013-09-26 09:33:50 -0600

maythe4thbewithu gravatar image

updated 2013-09-26 11:03:37 -0600

I check on Mac OS X 10.8.5 + OpenCV 2.4.6 ( gcc => Apple LLVM version 5.0 (clang-500.2.76) with Qt IDE)

It works fine. No crash, the 30 images can be dumped OK for both Suize and Forman video. program end normally.

I think the problem is not caused by OpenCV itself.

The problem maybe "You don't have the correct codec for this file".

Since codec is something independent from OpenCV.

As I know, you install codec into system, OpenCV use them.

The codec of the avi is "rawvideo", which is reported by FFmpeg (installed separately, not from OpenCV).

It seems I have correct codec on my system, because FFmpeg can dump jpg also normally.

here is the information from my FFmpeg of your 41.avi


configuration: --enable-gpl --enable-version3 --enable-nonfree --enable-nonfree --enable-postproc --enable-libfdk-aac --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-libtheora --enable-libvorbis --enable-libvpx --enable-shared --enable-mmx --arch=x86_64 --enable-swscale --enable-runtime-cpudetect --enable-pthreads --enable-avfilter --enable-libspeex --enable-libopenjpeg

libavutil 52. 46.100 / 52. 46.100

libavcodec 55. 33.100 / 55. 33.100

libavformat 55. 18.102 / 55. 18.102

libavdevice 55. 3.100 / 55. 3.100

libavfilter 3. 87.100 / 3. 87.100

libswscale 2. 5.100 / 2. 5.100

libswresample 0. 17.103 / 0. 17.103

libpostproc 52. 3.100 / 52. 3.100

Input #0, avi, from '41.avi':

Duration: 00:00:01.20, start: 0.000000, bitrate: 16930 kb/s

Stream #0:0: Video: rawvideo, pal8, 351x240, 25 tbr, 25 tbn, 25 tbc

Metadata:

title : F:\video\Journal\NLM\Suzie\41.avi


here is the code, but I think nothing different


#include <iostream>
#include <string>
#include <sstream>
#include "opencv/highgui.h"

using namespace std ;
using namespace cv ;

int main()
{
    string fname = "/your/path/to/41.avi" ;
    string savep = "/your/path/to/save/frames/" ;

    VideoCapture video(fname) ;
    Mat frame ;

    if( !video.isOpened() ) {
        cout << "Open file failed" << endl ;
        return false ;
    }

   int c = 0 ;

   for(;;++c)
   {
       if( video.read( frame ) == false )
           break ;

       stringstream ss ;
       ss << savep << c << ".jpg" ;

       imwrite( ss.str() , frame ) ;
   }
   cout << c << " frame dumped" << endl ;
   return true ;
}
edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-09-25 04:15:08 -0600

Seen: 907 times

Last updated: Sep 26 '13