opencv操作:鼠标回调的放大缩小、移动、画矩形框保存

//滚轮事件
//初始缩放值
//放大后的图
Mat g_moveimg;
//从放大图截取的矩形
Rect g_moverect;
double g_scale = 1.0;
float  g_scaleMin = 1.0;
float  g_scaleMax = 4.0;
Point  g_zoompoint;
void zoomInAndOut(const float scale, const Point pt, const Mat srcImg, Mat &dstImg)
{
	//缩放后
	resize(srcImg, dstImg, Size(), scale, scale, INTER_AREA);
	g_moveimg = dstImg.clone();
	//计算显示区域坐标
	Point pt1, pt2;

	pt1.x = pt.x *scale-pt.x;
	pt2.x = pt.x *scale+(srcImg.cols-pt.x);

	pt1.y = pt.y*scale-pt.y;
	pt2.y = pt.y*scale+(srcImg.rows-pt.y);

	Rect zoomrect = {pt1,pt2};
	g_moverect = { pt1,pt2};
	dstImg = dstImg(zoomrect).clone();
}

//拖动事件
//参考
//http://www.cppcns.com/wangluo/aspnet/250376.html
Point g_rldownpoint;
Point g_rlonpoint;
//移动后的矩形
Rect g_movenewrect;
//计数
int g_movecount;
void move(Mat& dst)
{
	//偏移量
	int dx = g_rlonpoint.x - g_rldownpoint.x;
	int dy = g_rlonpoint.y - g_rldownpoint.y;
	
	
	
	//Rect newrect;
	//第一次进来用放大后截取的矩形区域,其余的用移动后的矩形
	if (g_movecount==0)
	{
		g_movenewrect = { g_moverect.x - dx,g_moverect.y - dy,g_moverect.width,g_moverect.height };
	}
	else
	{
		g_movenewrect = { g_movenewrect.x - dx,g_movenewrect.y - dy,g_moverect.width,g_moverect.height };
	}
	
	
	g_movecount++;
	//判断是否越界
	if ((g_movenewrect.x + g_movenewrect.width) >= g_moveimg.cols)
	{
		g_movenewrect.x = g_moveimg.cols - g_movenewrect.width;
	}
	if ((g_movenewrect.y + g_movenewrect.height)>=g_moveimg.rows)
	{
		g_movenewrect.y = g_moveimg.rows - g_movenewrect.height;
	}
	if (g_movenewrect.x <=0)
	{
		g_movenewrect.x = 0;
	}
	if (g_movenewrect.y<=0)
	{
		g_movenewrect.y = 0;
	}


	dst = g_moveimg(g_movenewrect).clone();
}


/*
鼠标回调函数
功能:主要观察数据采集是否准确
图像右上角分别是 1、帧序号(第一行) 2、(第二行)矩形框的左上顶点 与 面积
图像颜色说明:
黄色:表示在图像边缘,扩展超出边界的
红色:表示识别到火花
白色:表示未识别到火花
绿色:表示矩形框的面积小于10,未进行检测

左上角是
火花矩形的中心位置
*/

//鼠标监听事件  
Point g_leftpoint;
Point g_rightpoint;
vector<Mat>g_mouserect;
Mat g_displyimg;
void on_mouse(int event, int x, int y, int flags, void* ustc)
{

	string pathname = "F:\\demo\\地铁燃弧\\高清燃弧识别\\样本\\新增样本\\1";
	string  outputfile = (pathname + "\\");
	//时间戳
	time_t t;
	time(&t);
	string img_name;
	img_name = outputfile + "A" + to_string((int)t - 0) + ".jpg";
	Rect tmprect;

	//缩放变量
	float step = 0.08;
	double value;

	//左键单击与抬起(画矩形)
	if (event == EVENT_LBUTTONDOWN)
	{
		g_leftpoint = Point(x, y);
	}
	if (event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON))
	{
		if (y>= g_mouserect[1].rows)
		{
			y = g_mouserect[1].rows;
		}

		if (x>= g_mouserect[1].cols)
		{
			x = g_mouserect[1].cols;
		}

		g_rightpoint = Point(x, y);

		g_displyimg = g_mouserect[1].clone();
		tmprect = Rect(g_leftpoint, g_rightpoint);
		rectangle(g_displyimg, tmprect, Scalar(0, 255, 255), 1, 8, 0);
		imshow("colorimg", g_displyimg);

	}
	if (event == EVENT_LBUTTONUP)
	{
		tmprect = Rect(g_leftpoint, g_rightpoint);
		//g_vec.push_back(tmprect);
		//rectangle(g_mouserect[1], tmprect, Scalar(0, 255, 255), 1, 8, 0);
		//imshow("colorimg", g_mouserect[1]);

		Mat tmpimg = Mat(g_mouserect[0], tmprect);
		imwrite(img_name.c_str(), tmpimg);
	}
	if (event==EVENT_MOUSEWHEEL)
	{
		//缩放事件
		value = getMouseWheelDelta(flags);
		g_displyimg = g_mouserect[1].clone();
		g_zoompoint = Point(x,y);
		if (value > 0)
		{
			g_scale += step;
		}
		else if(value < 0)
		{
			g_scale -= step;
		}
		g_scale = (g_scale > g_scaleMin) ? g_scale:(g_scaleMin);
		g_scale = (g_scale < g_scaleMax) ? g_scale:(g_scaleMax);

		zoomInAndOut(g_scale, g_zoompoint,g_displyimg, g_displyimg);
		imshow("colorimg", g_displyimg);
	}
	
	//图片拖动还有点问题
	// static声明静态局部变量,值在函数调用结束后不消失而保留原值,
	//即其占用的存储单元不释放,在下次该函数调用时,该变量保留上一次函数调用结束时的值
	//右键按下时,窗口显示图像左上角在源图像中x、y
	
	//右键按下
	if (event == EVENT_RBUTTONDOWN)
	{
		g_rldownpoint = Point(x, y); //获取鼠标右键按下时的起始点
	}
	if (event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_RBUTTON))
	{
		//拖动事件
		g_rlonpoint = Point(x,y);
		g_displyimg = g_mouserect[1].clone();
		move(g_displyimg);
		imshow("colorimg", g_displyimg);
	}
	
	return;
}



/*********

g_mouserect里面只有两张图,一张用于显示,一张用于保存
**************/
	cv::Mat colorimg;
		cv::cvtColor(g_fileinfo[i].img, colorimg, cv::COLOR_GRAY2BGR);
		//鼠标事件变量
		//保存
		if (g_mouserect.size()==2)
		{
			g_mouserect.clear();
		}
		g_mouserect.push_back(g_fileinfo[i].img);
		g_mouserect.push_back(colorimg);
		

		namedWindow("colorimg");
		setMouseCallback("colorimg", on_mouse, (void*)&g_mouserect);
		
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值