convert RGB video to gray

asked 2014-07-02 03:24:07 -0500

Mahavir gravatar image

updated 2014-07-02 04:46:50 -0500

I am doing rgb video to gray video conversion and wrote following code

using namespace cv;
int main() 
    int fps;
    char c;
    cvNamedWindow( "Input Video", CV_WINDOW_NORMAL); //Name of window is input window and it is autosized
    cvNamedWindow( "Output Video", CV_WINDOW_NORMAL);
    CvCapture* capture = cvCreateFileCapture("C:\\Users\\MAHAVIR\\Desktop\\1_1.mp4"); //give path of video file
    IplImage* frame; // Pointer frame is assigned
    fps=cvGetCaptureProperty (capture,CV_CAP_PROP_FPS);// Show frame rate
    CvSize size=cvSize((int)cvGetCaptureProperty (capture,CV_CAP_PROP_FRAME_HEIGHT),(int)cvGetCaptureProperty (capture,CV_CAP_PROP_FRAME_WIDTH));// Show frame size
    printf("The Frame Rate is %d",fps);
    printf("\nThe Frame size %dx%d",size);
    IplImage* gray_frame = cvCreateImage(size,IPL_DEPTH_8U,1);
    //CvVideoWriter *writer = cvCreateVideoWriter("C:\\Users\\MAHAVIR\\Desktop\\out.avi",CV_FOURCC('M','J','P','G'),fps,size,1);//create ouput location


    frame = cvQueryFrame(capture); // Read each frame from the video
    cvShowImage( "Input Video",frame );
    cvShowImage( "Output Video",gray_frame );
    if( c == 27 ) break; // Exit out of loop if Esc is pressed(ASCII of Esc=27)

but getting error of

image description

how to slove it?

edit retag flag offensive close merge delete



rule 1: if it has iplimages, avoid it.

(please use the c++ api, cv::Mat instead)

berak gravatar imageberak ( 2014-07-02 03:31:40 -0500 )edit

you've got the Size in reverse, it should be W,H, you got H,W.

and again, all of it is only needed, because you insist on using the outdated c-api.

berak gravatar imageberak ( 2014-07-02 04:53:01 -0500 )edit

Just a comment on your code: You do not not need two cvWatKey, just one of them after the second display will suffice. Also, since you have frame rate available, use that to compute frame delay and use frame delay as the parameter for cvWaitKey. Of course, as @berak has pointed out, your will be better off using the C++ interface.

unxnut gravatar imageunxnut ( 2014-07-02 07:29:15 -0500 )edit