opencv 视频中选择感兴趣区域(ROI) 

opencv 使用到的主要接口:

setMouseCallback()

尝试加入C++对象的思想对主要方法进行封装。

代码如下:

MyVideoCapture.h 文件如下
//
// Created by wang on 2021/5/8.
//

#ifndef MOUSESELECT_EXAMPLE_00_MYVIDEOCAPTURE_H
#define MOUSESELECT_EXAMPLE_00_MYVIDEOCAPTURE_H
#include "MouseSelect.h"
class MyVideoCapture
{
public:
    MyVideoCapture(std::string videoDir);
    ~MyVideoCapture();
    bool openCapture(cv::VideoCapture& capture);
    void run();
private:
    std::string m_inFile;
};
#endif //MOUSESELECT_EXAMPLE_00_MYVIDEOCAPTURE_H
MyVideoCapture.cpp 文件如下
//
// Created by wang on 2021/5/8.
//
#include "MyVideoCapture.h"
MyVideoCapture::MyVideoCapture(std::string videoDir)
{
    m_inFile=videoDir;
}

MyVideoCapture::~MyVideoCapture()
{

}

bool MyVideoCapture::openCapture(cv::VideoCapture &capture)
{
    if(m_inFile.size()==1)
    {
        capture.open(atoi(m_inFile.c_str()));
    } else{
        capture.open(m_inFile);
    }

    if(capture.isOpened()){
        return true;
    }
    return true;
}

void MyVideoCapture::run() {
    cv::VideoCapture capture;
    if(!openCapture(capture))
    {
        std::cerr<<"Can't open "<<m_inFile<<std::endl;
        return;
    }
    double fps=capture.get(cv::CAP_PROP_FPS);
    MouseSelect mouseSelect;
    cv::Mat frame;
    int64 frameCount=0;
    capture.read(frame);
    cv::namedWindow("Init frame",cv::WINDOW_NORMAL|cv::WINDOW_KEEPRATIO);
    mouseSelect.selectRect(frame);
    for(;;)
    {
        capture.read(frame);
        if(frame.empty())
        {
            std::cerr<<"Frame is empty!"<<std::endl;
            break;
        }

        for(auto i=0;i<mouseSelect.m_Rects.size();i++)
        {
            cv::rectangle(frame,mouseSelect.m_Rects[i],cv::Scalar(0,0,255));
        }
        cv::imshow("Video",frame);
        int waitTime=std::max<int>(1, cvRound(1000/fps-1));
        if(cv::waitKey(waitTime)==27)
        {
            break;
        }

        ++frameCount;
    }
}

main.cpp 如下 
#include <iostream>
#include "MyVideoCapture.h"

int main()
{
    std::string videoDir="/home/.../images/mot_result_couple_2.avi";

    MyVideoCapture myVideoCapture(videoDir);
    myVideoCapture.run();

    std::cout << "Hello, World!" << std::endl;
    return 0;
}

其中:

MouseSelect类的封装使用该博主代码:

https://www.cnblogs.com/dwdxdy/p/3244117.html

参考:

https://www.cnblogs.com/dwdxdy/p/3244117.html 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值