Skip to content

Instantly share code, notes, and snippets.

@raunaqbn
Last active June 14, 2016 10:42
Show Gist options
  • Save raunaqbn/f8a09db4b6129a48b365e386de2353cb to your computer and use it in GitHub Desktop.
Save raunaqbn/f8a09db4b6129a48b365e386de2353cb to your computer and use it in GitHub Desktop.
#include "chapter2.h"
// We create global variables of the cvCapture and the trackbar position
//so that it is accessible to the callback and the main function. The callback needs
//it to handle updating the frame position in the video capture. The main function needs
//it to register the callback return count and to host the actual while loop playing the video
CvCapture* g_pCapture = NULL;
int g_sliderPostion = 0;
void handle_trackbar_callback(int position)
{
cvSetCaptureProperty(g_pCapture, CV_CAP_PROP_POS_FRAMES, position);
}
void create_trackbar(char** argv)
{
g_pCapture = cvCreateFileCapture(argv[1]);
cvNamedWindow("3. Video with track-bar",CV_WINDOW_AUTOSIZE);
// Get the maximum possible number of frames to set limit of track bar (count)
int maxFrames = (int)cvGetCaptureProperty(g_pCapture, CV_CAP_PROP_FRAME_COUNT);
//Now we have the components of creating the track bar:
/*
Function:int cvCreateTrackbar(const string& trackbarname, const string& winname, int* value, int count,
TrackbarCallback onChange=0, void* userdata=0)
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.
userdata – User data that is passed as is to the callback. It can be used to handle trackbar events without
using global variables.
*/
cout<<"max frames:"<<maxFrames<<endl;
if (maxFrames > 0)
{
cvCreateTrackbar("pos", "3. Video with track-bar", &g_sliderPostion,
maxFrames, handle_trackbar_callback);
}
else
{
cout<<"There was a problem getting the max frame value"<<endl;
}
// Collect and setup some handling pointer and data
IplImage* pFrame = NULL;
int fps = (int) cvGetCaptureProperty(g_pCapture, CV_CAP_PROP_FPS);
cout<<"The video fps:"<<fps<<endl;
// Now we can go ahead and enter the while loop for displaying the video
while(1)
{
pFrame = cvQueryFrame(g_pCapture);
if (!pFrame)break;
cvShowImage("3. Video with track-bar", pFrame);
char wait = cvWaitKey(1000/fps);
if (wait == 27)break;
}
// Now we proceed to release the capture data and destroy the window
cvReleaseCapture(&g_pCapture);
cvDestroyWindow("3. Video with track-bar");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment