LearnToGrow
LearnToGrow

Reputation: 1778

creating a grayscale video using opencv

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.

Upvotes: 0

Views: 5928

Answers (2)

Michele mpp Marostica
Michele mpp Marostica

Reputation: 2472

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);
 }

Upvotes: 3

David Nilosek
David Nilosek

Reputation: 1412

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);

Upvotes: 1

Related Questions