支持滾動條隨着視頻播放自動移動
/*第三個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;
}