OpenCV第二个程序
/*第二个OpenCV程序*/
#include "opencv/highgui.h"
int main(int argc, char **argv)
{
cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE);
//CvCapture *capture = cvCreateFileCapture(argv[1]);
//CvCapture *capture = cvCreateFileCapture("nba.avi");
CvCapture *capture = cvCaptureFromFile("nba.avi");
/*
* Function: VideoCapture constructors.
* C: CvCapture* cvCaptureFromFile(const char* filename)
* Parameters:
* filename – name of the opened video file
*
* Note: 1.#define cvCaptureFromFile cvCreateFileCapture
* cvCreateFileCapture == cvCaptureFromFile
* 2.In C API, when you finished working with video,
* release CvCapture structure with cvReleaseCapture(),
* or use Ptr<CvCapture> that calls cvReleaseCapture()
* automatically in the destructor
*/
IplImage *frame;
while(1)
{
frame = cvQueryFrame(capture);
/*
* Function: Grabs(捕获), decodes(解码) and returns(返回)
* the next video frame(下一帧).
* C: IplImage* cvQueryFrame(CvCapture* capture)
* Parameters:
* capture - the pointer to CVcapture
* 指向CVcapture结构的指针。
* return: the next video frame or NULL pointer.
* Note: The methods/functions combine VideoCapture::grab()
* and VideoCapture::retrieve() in one call. This is
* the most convenient method for reading video files
* or capturing data from decode and retruen the just
* grabbed frame. If no frames has been grabbed
* (camera has been disconnected, or there are no more
* frames in video file), the methods return false and
* the functions return NULL pointer.
* 当后续没有帧时返回NULL空指针。
*/
if(!frame) break;
cvShowImage("Example2", frame);
char c = cvWaitKey(30);// 等待30ms
if(c == 27) break;// 按键为27-ESC退出
}
cvReleaseCapture(&capture);
/*
* Function: Closes video file or capturing device.
* C版本是关闭文件。
* C: void cvReleaseCapture(CvCapture** capture)
* Note: 1.The methods are automatically called by subsequent
* VideoCapture::open() and by VideoCapture destructor.
* 2.The C function also deallocates memory and clears *capture
* pointer
* 3.frame 对应的帧图片保存在CvCapture中, 所以不需要自己释放。
*/
cvDestroyWindow("Example2");
return 0;
}