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
参考: