Opencv视频帧给我一个错误

use*_*950 2 c++ opencv image-processing

尝试对视频上的帧执行灰度转换,但是当我到达cvCvtColor时,继续获取Assertation failed错误.有谁看到我的错误在哪里?

    IplImage *frame, *frame_copy = 0;

// capture frames from video
CvCapture *capture = cvCaptureFromFile( "lightinbox1.avi");
//Allows Access to video propertys
cvQueryFrame(capture);

//Get the number of frames
int nframe=(int) cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT);

    //Name window
cvNamedWindow( "video:", 1 );

    //start loop
for(int i=0;i<nframe;i++){
//prepare capture frame extraction
cvGrabFrame(capture);
cout<<"We are on frame "<<i<<"\n";
//Get this frame
frame = cvRetrieveFrame( capture );
con2txt(frame);
    frame_copy = cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,frame->nChannels );
//show and destroy frame
    cvCvtColor( frame,frame,CV_RGB2GRAY);

cvShowImage("video:",frame);
cvWaitKey(33);}
cvReleaseCapture(&capture);
Run Code Online (Sandbox Code Playgroud)

}

kar*_*lip 5

其实,问题是,cvCvtColor()CV_RGB2GRAY希望与输出图像nChannels == 1.

要解决此问题,您必须将复制过程调整为:

frame_copy = cvCreateImage(cvSize(frame->width,frame->height), IPL_DEPTH_8U, 1);
cvCvtColor(frame, frame_copy, CV_RGB2GRAY);
Run Code Online (Sandbox Code Playgroud)