简体   繁体   English

视频抓取对OpenCV不起作用

[英]Video Grabbing doesn't work OpenCV

I am using this piece of code to grab frames off a video : 我正在使用这段代码来抓取视频的帧:

#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 frame\n\7";
exit(0);
}
img=cvRetrieveFrame(capture);    //retrieve the captured frame



//free resources
cvReleaseCapture(&capture);

}

Which is returning : 正在返回:

Could not grab a frame

Additional details : I had used code to save webcam video feed to the file from which i want to grab frames . 其他详细信息:我曾使用代码将网络摄像头视频提要保存到我要从中抓取帧的文件中。 I used this code : 我使用此代码:

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

Does anyone know where I might be going wrong ? 有人知道我可能要去哪里了吗? I am running OpenCV-2.4.3 on a Beagleboard -xM with Ubuntu Quantal. 我正在使用Ubuntu Quantal在Beagleboard -xM上运行OpenCV-2.4.3。

I am not quite sure what your exactly question is, but if you want to grab frames from a video, you should at least have a loop. 我不确定您的确切问题是什么,但是如果您想从视频中抓取 ,则至少应该有一个循环。
A reason for your error could be, that your video file is not available. 您出错的原因可能是视频文件不可用。 Have you tried another one? 您尝试过另一种吗? The full path of the file? 文件的完整路径? Or put the file directly into your working directory and check it. 或者将文件直接放入您的工作目录中并进行检查。
Another reason could be a problem with the first frame (this sometimes happens). 另一个原因可能是第一帧有问题(有时会发生)。 So try to remove your exit and enclose your code with a loop over all frames. 因此,请尝试删除您的出口,并在所有框架上使用循环将您的代码括起来。
Here is an example that shows the given video file (Consider to use the C++-interface): 这是显示给定视频文件的示例(考虑使用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 frame\n";
        break;
    }

    if(waitKey(5) >= 0) 
        break;
}   
return 0;
}

This program runs on my PC if the file "a.avi" is in the current working directory of the program. 如果文件“ a.avi”位于程序的当前工作目录中,则该程序可在我的PC上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM