關於opencv2中滑鼠響應操作
阿新 • • 發佈:2019-01-05
暫時只接觸了兩個關於opencv2滑鼠響應操作的函式,下面分別介紹一下:
1.1 回撥函式
opencv2.4.5中,提供的滑鼠回撥函式是 setMouseCallback,函式宣告如下:
- CV_EXPORTS void setMouseCallback(const string& winname, MouseCallback onMouse, void* userdata = 0);
const string& winname,windows視窗名稱,對名為winname的視窗進行滑鼠監控。
MouseCallback onMouse,滑鼠響應處理函式,監聽滑鼠的點選,移動,鬆開,判斷滑鼠的操作型別,並進行響應的函式處理。
void* userdata = 0 滑鼠響應處理函式的ID,與滑鼠相應處理函式相匹配就行,暫時只用到預設為0的情況。
函式使用例項:
- namedWindow("img");
- setMouseCallback("img",on_mouse,0);
1.2 滑鼠響應處理函式
opencv2.4.5中,滑鼠相應處理函式一般預設形參和返回引數,函式形式如下:
- void on_mouse(int event,int x,int y,int flags,void *ustc)
函式引數介紹:
int event,滑鼠操作時間的整數代號,在opencv2.4.5中,event滑鼠事件總共有10中,從0-9依次代表如下:
Event:
- #define CV_EVENT_MOUSEMOVE 0 滑動
- #define CV_EVENT_LBUTTONDOWN 1 左鍵點選
- #define CV_EVENT_RBUTTONDOWN 2 右鍵點選
- #define CV_EVENT_MBUTTONDOWN 3 中間點選
- #define CV_EVENT_LBUTTONUP 4 左鍵釋放
- #define CV_EVENT_RBUTTONUP 5 右鍵釋放
- #define CV_EVENT_MBUTTONUP 6 中間釋放
- #define CV_EVENT_LBUTTONDBLCLK 7 左鍵雙擊
- #define CV_EVENT_RBUTTONDBLCLK 8 右鍵雙擊
- #define CV_EVENT_MBUTTONDBLCLK 9 中間釋放
int x,int y,代表滑鼠位於視窗的(x,y)座標位置,視窗左上角預設為原點,向右為x軸,向下為y軸,
int flags,代表滑鼠的拖拽事件,以及鍵盤滑鼠聯合事件,總共有32種事件,依次如下:
flags:
- #define CV_EVENT_FLAG_LBUTTON 1 左鍵拖拽
- #define CV_EVENT_FLAG_RBUTTON 2 右鍵拖拽
- #define CV_EVENT_FLAG_MBUTTON 4 中間拖拽
- #define CV_EVENT_FLAG_CTRLKEY 8 (8~15)按Ctrl不放事件
- #define CV_EVENT_FLAG_SHIFTKEY 16 (16~31)按Shift不放事件
- #define CV_EVENT_FLAG_ALTKEY 32 (32~39)按Alt不放事件(後面8-39還有待研究)
void *ustc,函式引數的編號(暫時用不到)
第二節 滑鼠操作例項
2.1 示例程式程式碼
程式如下,已經附上說明:
- #include <opencv2/core/core.hpp>
- #include <opencv2/highgui/highgui.hpp>
- #include <stdio.h>
- usingnamespace cv;
- cv::Mat org,dst,img,tmp;
- void on_mouse(int event,int x,int y,int flags,void *ustc)//event滑鼠事件代號,x,y滑鼠座標,flags拖拽和鍵盤操作的代號
- {
- static Point pre_pt = (-1,-1);//初始座標
- static Point cur_pt = (-1,-1);//實時座標
- char temp[16];
- if (event == CV_EVENT_LBUTTONDOWN)//左鍵按下,讀取初始座標,並在影象上該點處劃圓
- {
- org.copyTo(img);//將原始圖片複製到img中
- sprintf(temp,"(%d,%d)",x,y);
- pre_pt = Point(x,y);
- putText(img,temp,pre_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255),1,8);//在視窗上顯示座標
- circle(img,pre_pt,2,Scalar(255,0,0,0),CV_FILLED,CV_AA,0);//劃圓
- imshow("img",img);
- }
- elseif (event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))//左鍵沒有按下的情況下滑鼠移動的處理函式
- {
- img.copyTo(tmp);//將img複製到臨時影象tmp上,用於顯示實時座標
- sprintf(temp,"(%d,%d)",x,y);
- cur_pt = Point(x,y);
- putText(tmp,temp,cur_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255));//只是實時顯示滑鼠移動的座標
- imshow("img",tmp);
- }
- elseif (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))//左鍵按下時,滑鼠移動,則在影象上劃矩形
- {
- img.copyTo(tmp);
- sprintf(temp,"(%d,%d)",x,y);
- cur_pt = Point(x,y);
- putText(tmp,temp,cur_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255));
- rectangle(tmp,pre_pt,cur_pt,Scalar(0,255,0,0),1,8,0);//在臨時影象上實時顯示滑鼠拖動時形成的矩形
- imshow("img",tmp);
- }
- elseif (event == CV_EVENT_LBUTTONUP)//左鍵鬆開,將在影象上劃矩形
- {
- org.copyTo(img);
- sprintf(temp,"(%d,%d)",x,y);
- cur_pt = Point(x,y);
- putText(img,temp,cur_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255));
- circle(img,pre_pt,2,Scalar(255,0,0,0),CV_FILLED,CV_AA,0);
- rectangle(img,pre_pt,cur_pt,Scalar(0,255,0,0),1,8,0);//根據初始點和結束點,將矩形畫到img上
- imshow("img",img);
- img.copyTo(tmp);
- //擷取矩形包圍的影象,並儲存到dst中
- int width = abs(pre_pt.x - cur_pt.x);
- int height = abs(pre_pt.y - cur_pt.y);
- if (width == 0 || height == 0)
- {
- printf("width == 0 || height == 0");
- return;
- }
- dst = org(Rect(min(cur_pt.x,pre_pt.x),min(cur_pt.y,pre_pt.y),width,height));
- namedWindow("dst");
- imshow("dst",dst);
- waitKey(0);
- }
- }
- void main()
- {
- org = imread("1.jpg");
- org.copyTo(img);
- org.copyTo(tmp);
- namedWindow("img");//定義一個img視窗
- setMouseCallback("img",on_mouse,0);//呼叫回撥函式
- imshow("img",img);
- cv::waitKey(0);
- }