1 #include <opencv2/highgui/highgui.hpp> 2 #include <opencv2/imgproc/imgproc.hpp> 3 #include <string> 4 #include <iostream> 5 #include <fstream> 6 7 using namespace std; 8 9 int g_slider_position = 0; 10 int g_run = 1, g_dontset = 0; 11 cv::VideoCapture g_cap; 12 void onTrackbarSlide(int pos, void*) { 13 g_cap.set(cv::CAP_PROP_POS_FRAMES, pos); 14 15 if (!g_dontset) 16 g_run = 1; 17 g_dontset = 0; 18 } 19 int main(int argc, char** argv) { 20 std::string s = "Video/2020-06/e0927594e320877035894dc0a435f1b6.mp4"; 21 22 g_cap.open(s);//视频读取结构,通过传入字符串 23 24 int frames = (int)g_cap.get(cv::CAP_PROP_FRAME_COUNT); 25 int tmpw = (int)g_cap.get(cv::CAP_PROP_FRAME_WIDTH); 26 int tmph = (int)g_cap.get(cv::CAP_PROP_FRAME_HEIGHT); 27 28 cout << "Video has" << frames << "frames of dimensions(" << tmpw << "," << tmph << ")." << endl; 29 30 cv::createTrackbar("Position", "Example2_4", &g_slider_position, frames, onTrackbarSlide); 31 32 cv::Mat frame; 33 for (;;) { 34 if (g_run != 0) { 35 g_cap >> frame; if (frame.empty())break; 36 int current_pos = (int)g_cap.get(cv::CAP_PROP_POS_FRAMES); 37 g_dontset = 1; 38 39 cv::setTrackbarPos("Position", "Example2_4", current_pos); 40 cv::imshow("Example2_4", frame); 41 42 g_run -= 1; 43 } 44 45 char c = (char)cv::waitKey(10); 46 if (c == 's') { 47 g_run = 1; cout << "Singal step, run = " << g_run << endl; 48 } 49 if (c == 'r') { 50 g_run = -1; cout << "Run mode, run = " << g_run << endl; 51 } 52 if (c == 27) 53 break; 54 55 } 56 cv::destroyWindow("Example2_4"); 57 return 0; 58 }