Opencv 入门三(视频滑动条窗口)

视频滑动条窗口源码如下:

#include "opencv2\highgui\highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp" 
#include <iostream> 
#include <fstream> 
using namespace std;
int g_slider_position = 0;      // 滑动条的位置
int g_run = 1, g_dontset = 0; //start out in single step mode
cv::VideoCapture g_cap;
void onTrackbarSlide(int pos, void*) {
    g_cap.set(cv::CAP_PROP_POS_FRAMES, pos);   //真正使进度条移动到我们希望的位置
    if (!g_dontset)
        g_run = 1;
    g_dontset = 0;
}

int main(int argc, char** argv)
{
    cv::namedWindow("Example2_4", cv::WINDOW_AUTOSIZE);
    g_cap.open(string(argv[1]));
    int frames = (int)g_cap.get(cv::CAP_PROP_FRAME_COUNT);
    int tmpw = (int)g_cap.get(cv::CAP_PROP_FRAME_WIDTH);
    int tmph = (int)g_cap.get(cv::CAP_PROP_FRAME_HEIGHT);
    cout << "Video has "<<frames<<" frames of dimensions("<<tmpw<<", "<<tmph <<")."<<endl;
    cv::createTrackbar("Position", "Example2_4", &g_slider_position, frames, onTrackbarSlide);
    
    cv::Mat frame;
    for (;;) {
        if (g_run != 0)
        {
            g_cap >> frame;
            if (frame.empty())break;
            int current_pos = (int)g_cap.get(cv::CAP_PROP_POS_FRAMES);
            g_dontset = 1;
            cv::setTrackbarPos("Position", "Example2_4", current_pos);
            cv::imshow("Example2_4", frame);
            g_run -= 1;
        }

        char c = (char)cv::waitKey(10);
        if (c == 's')//single step
        {
            g_run = 1;
            cout << "Single step,run =" << g_run << endl;
        }
        if (c == 'r')//run mode
        {
            g_run = -1; cout << "Run mode,run=" << g_run << endl;
        }
        if (c == 27)  // Esc 按键
            break;
    }
    
    return 0;
}

 

相关推荐

  1. 滑动窗口问题

    2023-12-22 00:40:03       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-22 00:40:03       91 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-22 00:40:03       97 阅读
  3. 在Django里面运行非项目文件

    2023-12-22 00:40:03       78 阅读
  4. Python语言-面向对象

    2023-12-22 00:40:03       88 阅读

热门阅读

  1. MySQL_15.UNDO和REDO的区别

    2023-12-22 00:40:03       50 阅读
  2. 【AI】YOLO学习笔记二

    2023-12-22 00:40:03       46 阅读
  3. JVM中的虚拟机栈的动态链接部分存放到底是什么

    2023-12-22 00:40:03       58 阅读