OpenCV(2)-播放AVI视频

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值