我正在使用这段代码从视频中抓取帧:
#include <stdio.h>
#include <stdlib.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <iostream>
using namespace cv;
using namespace std;
int main (int argc, char** argv)
{
//initializing capture from file
CvCapture * capture = cvCaptureFromAVI ("/home/<some_file>.avi");
//Capturing a frame
IplImage* img = 0;
if(!cvGrabFrame(capture)) //capture a frame
{
cout << Could not grab a framen7";
exit(0);
}
img=cvRetrieveFrame(capture); //retrieve the captured frame
//free resources
cvReleaseCapture(&capture);
}
哪个正在返回:
Could not grab a frame
其他详细信息 :我使用代码将网络摄像头视频源保存到我要从中抓取帧的文件中。我使用了这段代码:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
int main( int argc, char** argv ) {
CvCapture* capture;
capture = cvCreateCameraCapture(0);
assert( capture != NULL );
IplImage* bgr_frame = cvQueryFrame( capture );
CvSize size = cvSize(
(int)cvGetCaptureProperty( capture,
CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty( capture,
CV_CAP_PROP_FRAME_HEIGHT)
);
cvNamedWindow( "Webcam", CV_WINDOW_AUTOSIZE );
CvVideoWriter *writer = cvCreateVideoWriter( "/Users/user/Desktop/OpenCV_trial/OpenCV_trial/vidtry.AVI",
CV_FOURCC('D','I','V','X'),
30,
size
);
while( (bgr_frame = cvQueryFrame( capture )) != NULL )
{
cvWriteFrame(writer, bgr_frame );
cvShowImage( "Webcam", bgr_frame );
char c = cvWaitKey( 33 );
if( c == 27 ) break;
}
cvReleaseVideoWriter( &writer );
cvReleaseCapture( &capture );
cvDestroyWindow( "Webcam" );
return( 0 );
}
有谁知道我可能哪里出错了?我正在使用带有Ubuntu Quantal的Beagleboard -xM上运行OpenCV-2.4.3。
我不太确定你的确切问题是什么,但如果你想从视频中抓取帧,你至少应该有一个循环。
您的错误的原因可能是您的视频文件不可用。你试过另一个吗?文件的完整路径?或者将文件直接放入您的工作目录并检查它。
另一个原因可能是第一帧的问题(有时会发生这种情况)。因此,请尝试删除退出并使用遍历所有帧的循环来封装代码。
下面是一个显示给定视频文件的示例(考虑使用 C++ 接口):
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <iostream>
using namespace cv;
using namespace std;
int main (int argc, char** argv)
{
//initializing capture from file
Mat img;
VideoCapture capture("a.avi");
if(!capture.isOpened())
{
cout<<"Could not open video!n";
return 1;
}
while(true)
{
//Capturing a frame
capture >> img;
if(!img.empty())
{
imshow("Video",img);
}
else
{
cout <<"Could not grab a framen";
break;
}
if(waitKey(5) >= 0)
break;
}
return 0;
}
如果文件"a.avi"位于程序的当前工作目录中,则此程序在我的PC上运行。