OpenCV实现鼠标滚轮图片拖放和放大缩小

#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;
}

请添加图片描述

源码有偿 需要的私信

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值