Trying to perform grayscale transformation on frames on a video ,but keep getting Assertation failed error when I get to cvCvtColor. Does anyone see where my error is?
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);
}
Actually, the problem is that cvCvtColor()
with CV_RGB2GRAY
expects an output image with nChannels == 1
.
To solve the issue, you must adjust your copy procedure to:
frame_copy = cvCreateImage(cvSize(frame->width,frame->height), IPL_DEPTH_8U, 1);
cvCvtColor(frame, frame_copy, CV_RGB2GRAY);
You're calling cvCvtColor
with the same frame as source and destination image.
This isn't possible, since cvCvtColor
must change the number of channels in the image in order to convert rgb to grey.
Call cvtColor
with a new image as the destination, instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.