0

I need to save a grayscale video from a GIge camera using OpenCV on Mac os X 10.8. I used this code:

 namedWindow("My video",CV_WINDOW_AUTOSIZE);
 Size frameSize(659, 493);
 VideoWriter oVideoWriter ("MyVideo.avi",-1, 30, frameSize, false);

 While(1)
 {
 ...
     Mat Image=Mat(Size(GCamera.Frames[Index].Width,GCamera.Frames[Index].Height),CV_8UC1,GCamera.Frames[Index].ImageBuffer);

     oVideoWriter.write(Image);
 ...   
 }

I got this error:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /Users/rosa/OpenCV-2.4.3/modules/imgproc/src/color.cpp, line 3270 libc++abi.dylib: terminate called throwing an exception The program has unexpectedly finished.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
LearnToGrow
  • 1,656
  • 6
  • 30
  • 53
  • Maybe your frame is empty. Please check this post [this](http://stackoverflow.com/questions/21871540/opencv-error-assertion-failed-scn-3-scn-4). He got the same error. – Hadi Apr 14 '14 at 17:50
  • @Constantine, No because I show and save frames on hard disk and it is correct. – LearnToGrow Apr 14 '14 at 17:52
  • @Constantine, In my code i did not use cvtColor anywhere because the acquired frame is originally grayscale. I remark that the error is in this line oVideoWriter.write(Image); – LearnToGrow Apr 14 '14 at 17:56

2 Answers2

3

I made it this way:

 VideoWriter oVideoWriter ("MyVideo.avi",CV_FOURCC('M','J','P','G'), 30, frameSize);

 While(1)
 {
     Mat Image=Mat(Size(GCamera.Frames[Index].Width,GCamera.Frames[Index].Height),CV_8UC1,GCamera.Frames[Index].ImageBuffer);
     Mat colorFrame;
     cvtColor(Image, colorFrame, CV_GRAY2BGR);
     oVideoWriter.write(colorFrame);
 }
scottmrogowski
  • 2,063
  • 4
  • 23
  • 32
Michele mpp Marostica
  • 2,445
  • 4
  • 29
  • 45
1

Your issue is your operating system. Checking the documentation, it says the greyscale feature is supported on Windows only.

Easy enough fix though,

cv::Mat imageGrey;
cv::Mat imageArr[] = {Image, Image, Image};
cv::merge(imageArr, 3, imageGrey);

oVideoWriter.write(imageGrey);
David Nilosek
  • 1,422
  • 9
  • 13
  • the error is disappeared but the video does not work and it has a fixed size 414 bytes – LearnToGrow Apr 17 '14 at 17:01
  • Again, operating system. You can only use the -1 flag in the video writer constructor on windows. Otherwise you need CV_FOURCC(...), I'd check the doc. and make sure you have it all right: http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videowriter – David Nilosek Apr 17 '14 at 19:16
  • I have used CV_FOURCC('8','B','P','S') and it is working now! – LearnToGrow Apr 17 '14 at 19:17