1. 程式人生 > >回掉函式滑鼠事件畫矩形

回掉函式滑鼠事件畫矩形

用了一個回掉函式滑鼠畫矩形,菜鳥差點原地爆炸了。

#include <highgui.h>
#include <iostream>  
using namespace cv;
using namespace std;
int x,y;
Rect rect;
Mat ima;
Point m,n;
void mousecallback(int event,int x,int y,int flags,void *param);
int main(int argc,char** argv){
	ima=imread(argv[1]);
    imshow("image",ima);  
	setMouseCallback("image",mousecallback);  
	waitKey();  
	destroyAllWindows();
}
void mousecallback(int event,int x,int y,int flags,void *param){
	switch (event){
	case CV_EVENT_LBUTTONDOWN: 
        cout<<"觸發滑鼠左鍵按下事件"<<endl;
		m=Point(x,y);
		cout<<"x:"<<x<<",y:"<<y<<endl;
		break;
       
	case CV_EVENT_LBUTTONUP: 
		cout<<"觸發左鍵擡起事件"<<endl;
		Point n=Point(x,y);
		cout<<"x:"<<x<<",y:"<<y<<endl;
		rect=Rect(m,n);
		rectangle(ima,rect,Scalar(255,0,0));
		imshow("image",ima);
	}
}