#pragma once
#ifndef PREVIEWIMG_H
#define PREVIEWIMG_H
#include <opencv2/opencv.hpp>
using namespace cv;
class PreviewImg
{
public:
PreviewImg( Mat &Img,std::string winName, Size size=Size(800,600));
PreviewImg(const std::string &url,std::string winName,Size size=Size(800,600));
enum Mode {
PreviewMode,
ALMODE
};
Mat getImg();
void setImage(Mat &m);
typedef void(*ALMODE_CALLBACK)(int event, int srcX, int srcY,void *user);
static void mouseEvent(int event, int x, int y, int flags, void* userdata);
void setMode(Mode nmode);
int show(int ms);
//设置算法模式下的回调函数
void setAlModeCallback(ALMODE_CALLBACK callback,void *ptr);
void addCircle(Point2d center,double radius,Vec3b color);
void addLine(Point2d b,Point2d e, Vec3b color);
void addText(const std::string &textContent ,Point2d pos, Vec3b color);
//强制重新渲染
void update();
//清空所有绘制元素
void clearAllItem();
void exit();
};
#endif
调用OpenCV的highgui封装成一个类,开箱即用,OpenCV大于2.4的版本即可使用,支持静态图片、动态视频,支持多窗口。
支持回调函数做算法处理
//调用方式简单
void runFunction() {
VideoCapture cp("xxx.avi");
Mat m;
cp >> m;
PreviewImg *p = new PreviewImg(m, "测试窗口");
p->addCircle({ 500,200 }, 50, { 255,0,255 });
p->addLine({ 1000,1000 }, {1200,1300}, { 0,0,255 });
p->addText("Test Text", { 1500,1500 }, { 255,0,0 });
while (p->show(30) != 27) {
cp >> m;
if (!m.empty()) {
p->setImage(m);
}
}
delete p;
}
源码有偿 需要的私信