mouse select

#include "opencv2/core/core.hpp"  
#include "opencv2/objdetect/objdetect.hpp"  
#include "opencv2/highgui/highgui.hpp"  
#include "opencv2/imgproc/imgproc.hpp"  
#include <tchar.h>
#include <fstream>  
#include <iostream>  
#include <stdio.h>  
 
using namespace std;
using namespace cv;
 
Rect select;
bool select_flag = false;
Mat img,showImg,selectImg;
 
void onMouse(int event,int x,int y,int flags,void*param)
{
	Point p1,p2;
	if(event==CV_EVENT_LBUTTONDOWN)
	{
		select.x = x;
		select.y = y;
		select_flag = true;
	}
	else if(select_flag &&event == CV_EVENT_MOUSEMOVE)
	{
		img.copyTo(showImg);
		p1 = Point(select.x,select.y);
		p2 = Point(x,y);	
    	rectangle(showImg,p1,p2,Scalar(0,255,0),2);
		imshow("img",showImg);
	}
	else if(select_flag && event == CV_EVENT_LBUTTONUP)
	{
		//显示框出的图
		Rect roi = Rect(Point(select.x,select.y),Point(x,y));
		cout<<"**************************"<<endl;
		cout<<select.x<<endl;
		cout<<select.y<<endl;
		cout<<x-select.x<<endl;
		cout<<y-select.y<<endl;
		if(roi.width && roi.height)//点一下时会报错
		{
			Mat roiImg = img(roi);
			imshow("roi",roiImg);

			//waitKey(0);
		}
		select_flag = false;		
	}	
}
 
//框图超过画面时会报错
int _tmain(int argc, _TCHAR* argv[])
{
    img = imread("./2394.tiff",1);
	showImg = img.clone();
	//showImg = img;
	select.x=select.y = 0;
	namedWindow("img");
	imshow("img",showImg);
	setMouseCallback("img",onMouse,0);
	waitKey(0);
	system("pause");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值