forked from oreillymedia/Learning-OpenCV-3_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_09-03.cpp
71 lines (64 loc) · 1.95 KB
/
example_09-03.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Example 9-3. Using a trackbar to create a “switch” that the user can turn on and off;
//this program plays a video and uses the switch to create a pause functionality
//
// An example program in which the user can draw boxes on the screen.
//
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
//
// Using a trackbar to create a "switch" that the user can turn on and off.
// We make this value global so everyone can see it.
//
int g_switch_value = 1;
void switch_off_function() { cout << "Pause\n"; }; //YOU COULD DO MORE
void switch_on_function() { cout << "Run\n"; };
// This will be the callback that we give to the trackbar.
//
void switch_callback( int position, void* ) {
if( position == 0 ) {
switch_off_function();
} else {
switch_on_function();
}
}
void help(char ** argv) {
cout << "Example 9-3. Using a trackbar to create a “switch” that the user can turn on and off"
<< "\n this program plays a video and uses the switch to create a pause functionality."
<< "\n\nCall:\n" << argv[0] << " <path/video_file>"
<< "\n\nShows putting a pause button in a video; Esc to quit\n" << endl;
}
int main( int argc, char** argv ) {
cv::Mat frame; // To hold movie images
cv::VideoCapture g_capture;
help(argv);
if( argc < 2 || !g_capture.open( argv[1] ) ){
cout << "Failed to open " << argv[1] << " video file\n" << endl;
return -1;
}
// Name the main window
//
cv::namedWindow( "Example", 1 );
// Create the trackbar. We give it a name,
// and tell it the name of the parent window.
//
cv::createTrackbar(
"Switch",
"Example",
&g_switch_value,
1,
switch_callback
);
// This will cause OpenCV to idle until
// someone hits the Esc key.
//
for(;;) {
if( g_switch_value ) {
g_capture >> frame;
if( frame.empty() ) break;
cv::imshow( "Example", frame);
}
if( cv::waitKey(10)==27 ) break; //esc
}
return 0;
}