OpenCV(3)-视频播放控制

OpenCV第三课(支持滚动条随着视频播放自动移动)

/*第三个OpenCV程序*/
#include "opencv/cv.h"
#include "opencv/highgui.h"

int g_slider_position = 0;
CvCapture *g_capture = NULL;
void onTrackbarSlide(int pos)
{
	/*
	 * Function:Sets a property in the VideoCapture.
	 * C: int cvSetCaptureProperty(CvCapture* capture,
	 *                       int propId, double value)
	 * parameters:	
	             propId -Property identifier. As the same with
				         cvGetCaptureProperty.
				 value – Value of the property.
	 */
	cvSetCaptureProperty(
	g_capture,
	CV_CAP_PROP_POS_FRAMES,
	pos
	);
}

int main(int argc, char **argv)
{

	cvNamedWindow("Example3", CV_WINDOW_AUTOSIZE);
    g_capture = cvCaptureFromFile("SilentWitness.avi");
	int frames = (int)cvGetCaptureProperty(
		g_capture, 
		CV_CAP_PROP_FRAME_COUNT);// 返回g_capture 即nba.avi总的帧数。
	/*
	 * Function: Returns the specified VideoCapture property.
	 * C:  double cvGetCaptureProperty(CvCapture* capture, int propId)
	 * Parameters:	propId –
	       Property identifier. It can be one of the following:
	       CV_CAP_PROP_POS_MSEC Current position of the video file 
					   in milliseconds or video capture timestamp.
	       CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be 
					   decoded/captured next.
	       CV_CAP_PROP_POS_AVI_RATIO Relative position of the video 
					   file: 0 - start of the film, 1 - end of the film.
	       CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
	       CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
	       CV_CAP_PROP_FPS Frame rate.
	       CV_CAP_PROP_FOURCC 4-character code of codec.
	       CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
	       CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
	       CV_CAP_PROP_MODE Backend-specific value indicating the current
		               capture mode.
	       CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
	       CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
	       CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
	       CV_CAP_PROP_HUE Hue of the image (only for cameras).
	       CV_CAP_PROP_GAIN Gain of the image (only for cameras).
	       CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
	       CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images 
					   should be converted to RGB.
	       CV_CAP_PROP_WHITE_BALANCE Currently not supported
	       CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras
                       (note: only supported by DC1394 v 2.x backend currently).     
	 *Note: When querying a property that is not supported by the backend used by
	 *      the VideoCapture class, value 0 is returned.
	 *
	 */
	if(frames != 0)
	{
		cvCreateTrackbar(
			"Position",
			"Example3",
			&g_slider_position,
			frames,
			onTrackbarSlide
			);
		/*
		 * Function: Creates a trackbar(轨迹条) and attaches it to the specified window.
		 *           创建一个轨迹条(滚动条)并把它附在指定的窗口上。
		 * C: int cvCreateTrackbar(const char* trackbarName, const char* windowName, 
		 *                       int* value, int count, CvTrackbarCallback onChange)
		 * Parameters:	
		        trackbarname – Name of the created trackbar.
		        winname – Name of the window that will be used as a parent of the 
					      created trackbar.
		        value – Optional pointer to an integer variable whose value reflects
					    the position of the slider. Upon creation, the slider position
				        is defined by this variable.
		        count – Maximal position of the slider. The minimal position 
					    is always 0.
		        onChange – Pointer to the function to be called every time the 
					       slider.changes position. This function should be prototyped 
						   as void Foo(int,void*); , where the first parameter is the 
						   trackbar position and the second parameter is the user data 
						   (see the next parameter). If the callback is the NULL
						   pointer,(no callbacks are called, but only value is updated
						   指向函数的指针,每次滚动条变换位置都要调用这个函数。这个函数应该被
						   声明为void Foo(int); 如果没有回调函数,这个值可以设为NULL。
		 * Note: 每次滚动条被拖动调用onChange函数时,滚动条的位置会被作为一个32位整数传入。
		 */
	}	
	IplImage *frame;
	while(1)
	{
		frame = cvQueryFrame(g_capture);
		if(!frame) break;
		cvShowImage("Example2", frame);
		/*获取当前活动帧的地址*/ 
		int pos = (int)cvGetCaptureProperty(g_capture, CV_CAP_PROP_POS_FRAMES);
		/*设置滚动条的新位置*/
		cvSetTrackbarPos("Position", "Example3", pos);
		char c = cvWaitKey(33);// 等待33ms
		if(c == 27) break;// 按键为27-ESC退出
		
	}
	cvReleaseCapture(&g_capture);
	cvDestroyWindow("Example2");
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值