OpenCV 第1天
OpenCV 第1天

OpenCV 第1天

内容纲要

安装依赖

  • 下载OepnCV

  • 加入Path

  • 创建VS项目

  • 在属性管理器中新建Opencv.props, 添加Include, lib

    在包含目录添加(根据自己实际安装目录)
    .\Opencv4.1\opencv\build\include
    .\Opencv4.1\opencv\build\include\opencv2
    在库目录添加(根据自己实际安装目录)
    .\Opencv4.1\opencv\build\x64\vc15\lib

  • 在链接器-输入-附加依赖项,添加opencv_world440d.lib

First Step

#include <opencv2/opencv.hpp>

int main(int argc, char** argv){
    cv::Mat img = cv::imread(argv[1],-1);
    if (img.empty()) return-1;
    cv::namedWindow("Example1",cv::WINDOW_AUTOSIZE);
    cv::imshow("Example1",img);
    cv::waitKey(0);
    cv::destroyWindow("Example1");
    return 0;
}

Video viewer

全局变量的含义

  • g_run: 持续播放
  • g_dontset: 拖动slider的时候使用单步模式
  • g_slider_position: slider位置

    #include <opencv2/opencv.hpp>
    #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/imgproc/imgproc.hpp"
    #include<iostream>
    #include<fstream>
    int g_slider_position = 0;
    int g_run = 1;
    int g_dontset = 0;
    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",cv::WINDOW_AUTOSIZE);
    g_cap.open(argv[1]);
    int frames = (int)g_cap.get(cv::CAP_PROP_FRAME_COUNT);  //CAP_PROR_FRAME_COUNT -- Frames in the video
    int tmpw = (int)g_cap.get(cv::CAP_PROP_FRAME_WIDTH);
    int tmph = (int)g_cap.get(cv::CAP_PROP_FRAME_HEIGHT);
    cout << "视频有" << frames << " frames of dimensions(" << tmpw << "," << tmph << ")." << endl;
    //createTrackbar(track bar name, window name, bar index, total frames, call back function);
    //call back function activate on chaging the value of track bar
    cv::createTrackbar("Position", "Example2", &g_slider_position, frames,onTrackbarSlide);
    cv::Mat frame;
    for (;;)
    {
        //at single mode g_run stoped in one loop
        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", current_pos);
            current_pos = cv::getTrackbarPos("Position", "Example2");
            cout << current_pos <<" run: " << g_run <<" dontset: "<< g_dontset << endl;
            cv::imshow("Example2", frame);
            g_run -= 1;
        }
        char c = (char)cv::waitKey(16.67);
        if (c == 's') {
            g_run = 1;
            cout << "Single Step, run = " << g_run << endl;
        }
        if (c == 'r') {
            g_run = -1;
            cout << "Run_mode, run = " << g_run << endl;
        }
        if (c == 27)
            break;
    }
    
    return 0;
    }

总结

好像是个阳间东西。还不错。

发表评论

您的电子邮箱地址不会被公开。