1. 程式人生 > >Opencv之滑鼠響應setMouseCallback()的用法

Opencv之滑鼠響應setMouseCallback()的用法

平時在寫程式碼時可能用到滑鼠在圖上做標記等其他作用,opencv主要用setMouseCallback()這個函式,下面來講解:

一、setMouseCallback()

 void setMousecallback(const string& winname, MouseCallback onMouse, void* userdata=0)
    winname:視窗的名字
    onMouse:滑鼠響應函式,回撥函式。指定窗口裡每次滑鼠時間發生的時候,被呼叫的函式指標。 這個函式的原型應該為void on_Mouse(int event, int x, int y, int flags, void* param);
    userdate:傳給回撥函式的引數 
再來介紹MouseCallback onMouse的函式原型:
void on_Mouse(int event, int x, int y, int flags, void* param);
event是 CV_EVENT_*變數之一
x和y是滑鼠指標在影象座標系的座標(不是視窗座標系) 
flags是CV_EVENT_FLAG的組合, param是使用者定義的傳遞到setMouseCallback函式呼叫的引數。
附常用的event:

#defineCV_EVENT_MOUSEMOVE

#defineCV_EVENT_LBUTTONDOWN 

#defineCV_EVENT_RBUTTONDOWN   

#defineCV_EVENT_LBUTTONUP    

#defineCV_EVENT_RBUTTONUP   

和標誌位flags有關的:

#defineCV_EVENT_FLAG_LBUTTON 

注意:flags & CV_EVENT_FLAG_LBUTTON 的意思是 提取flags的CV_EVENT_FLAG_LBUTTON 標誌位,!()的意思是 標誌位無效
二、程式一(畫直線)

用if()else()方法:

#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;

#define WINDOW "原圖"
Mat g_srcImage,g_dstImage;
Point previousPoint;
bool P = false;
void On_mouse(int event, int x, int y, int flags, void*);
int main()
{
	g_srcImage = imread("D://vvoo//face.jpg", 1);
	imshow(WINDOW, g_srcImage);
	setMouseCallback(WINDOW, On_mouse, 0);

	waitKey(0);
	return 0;
}
void On_mouse(int event, int x, int y, int flags, void*)
{
	/*if (event == EVENT_LBUTTONUP ||!( flags&EVENT_FLAG_LBUTTON))
	{
		previousPoint = Point(-1, -1);
	}*/
	if (event == EVENT_LBUTTONDOWN)
	{
		previousPoint = Point(x, y);
	}
	else if (event == EVENT_MOUSEMOVE && (flags&EVENT_FLAG_LBUTTON))
	{
		Point pt(x, y);
		line(g_srcImage, previousPoint, pt, Scalar(0,0,255), 2, 5, 0);
		previousPoint = pt;
		imshow(WINDOW, g_srcImage);
	}
}

參考switch()方法:
void On_mouse(int event, int x, int y, int flags, void*)
{
	switch (event)
	{
	case EVENT_LBUTTONUP:
	{
							P = false;
	}
		break;
	case EVENT_LBUTTONDOWN:
	{

							  previousPoint = Point(x, y);

			                  P = true;
	}
		break;

	case EVENT_MOUSEMOVE :
	{
							if(P)
							 {
								 Point pt(x, y);
								 line(g_srcImage, previousPoint, pt, Scalar(0,0,255), 2, 5, 0);
								 previousPoint = pt;
								 imshow(WINDOW, g_srcImage);
							 }
	}
		break;
	}
}

兩者的實現方法不同,但是結果都是一樣的,方法一沒有用到EVENT_LBUTTONUP這個滑鼠左鍵按下響應。

結果:


二、程式2(畫矩形)

#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;

Rect select;
bool select_flag = false;
Mat img, showImg;

void A_on_Mouse(int event, int x, int y, int flags, void*param)//實現畫矩形框
{
	Point p1, p2;
	if (event == EVENT_LBUTTONDOWN)
	{
		select.x = x;
		select.y = y;
		select_flag = true;
	}
	else if (select_flag &&event == 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 == EVENT_LBUTTONUP)
	{
		select_flag = false;
	}
}
void B_on_Mouse(int event, int x, int y, int flags, void*param)//實現畫矩形框並截圖
{

	Point p1, p2;
	switch (event)
	{

	case  EVENT_LBUTTONDOWN:
	{
								  select.x = x;
								  select.y = y;
								  select_flag = true;
	}
		break;
	case EVENT_MOUSEMOVE:
	{
							   if (select_flag)
							   {
					
							   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);
							   }					  
	} 
		break;
	case EVENT_LBUTTONUP:
	{
							   //顯示框出的ROI
                                                           //改成Rect roi = Rect(p1, p2);為什麼不對?
							   Rect roi = Rect(Point(select.x, select.y), Point(x, y));
							   if (roi.width && roi.height)//點一下時會沒有反應
							   {
								   Mat roiImg = img(roi);
								   imshow("roi", roiImg);
							   }
							   select_flag = false;
							   
	}
		break;
	}
}

int main()
{
	img = imread("D://vvoo//face.jpg", 1);
	showImg = img.clone();
	select.x = select.y = 0;
	imshow("img", showImg);

	
	while (1)
	{
		int key = waitKey(10);
switch (key)
	{
	case 'a':
				setMouseCallback("img", A_on_Mouse, 0);
		break;
	case 'b':
				setMouseCallback("img", B_on_Mouse, 0);
		break;
	}
if (key == 27||key=='q')
break;
	}
	waitKey(0);
	return 0;
}
結果:

根據按鍵實現兩種功能,這裡只截圖了第二種畫矩形框並把它截圖功能:


三、參考資料