本文實例為大家分享了Opencv使用鼠標任意形狀摳圖的具體代碼,供大家參考,具體內容如下
主要的方法思路是:首先利用鼠標在圖上畫任意形狀,利用掩碼將任意形狀摳出來
主要難點是怎么填充,因為鼠標在畫線的時候,滑動越快,點是不連續的,利用floodFill和drawContours都是沒有辦法進行填充的,從另一個方面想,一個面是由很多個點組成的,雖然鼠標滑動保存下來的就是一系列點,可以利用這一系列點構成一個面,利用面的性質進行填充就比較簡單了。
一、首先使用鼠標點擊事件,鼠標點擊事件的函數為:
void on_mouse(int event, int x, int y, int flags, void* ustc)
常見的鼠標事件有:
event事件有:
1
2
3
4
5
6
7
8
9
10
11
12
|
CV_EVENT_MOUSEMOVE =0, //鼠標移動 CV_EVENT_LBUTTONDOWN =1, //按下左鍵 CV_EVENT_RBUTTONDOWN =2, //按下右鍵 CV_EVENT_MBUTTONDOWN =3, //按下中鍵 CV_EVENT_LBUTTONUP =4, //放開左鍵 CV_EVENT_RBUTTONUP =5, //放開右鍵 CV_EVENT_MBUTTONUP =6, //放開中鍵 CV_EVENT_LBUTTONDBLCLK =7, //左鍵雙擊 CV_EVENT_RBUTTONDBLCLK =8, //右鍵雙擊 CV_EVENT_MBUTTONDBLCLK =9, //中鍵雙擊 CV_EVENT_MOUSEWHEEL =10, //滾輪滾動 CV_EVENT_MOUSEHWHEEL =11 //橫向滾輪滾動 |
flag事件有:
1
2
3
4
5
6
|
CV_EVENT_FLAG_LBUTTON =1, //左鍵拖拽 CV_EVENT_FLAG_RBUTTON =2, //右鍵拖拽 CV_EVENT_FLAG_MBUTTON =4, //中鍵拖拽 CV_EVENT_FLAG_CTRLKEY =8, //按住CTRL拖拽 CV_EVENT_FLAG_SHIFTKEY =16, //按住Shift拖拽 CV_EVENT_FLAG_ALTKEY =32 //按住ALT拖拽 |
首先鼠標在圖像上的操作有三個步驟:
1、按下左鍵,建立一個起點
1
2
3
4
|
if (event == CV_EVENT_LBUTTONDOWN) { //畫起點 } |
2、按下左鍵的同時,進行左鍵拖拽畫線
1
2
3
4
|
else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON)) { //在拖動過程中畫線 } |
3、放下左鍵,起點與當前點進行連接
1
2
3
4
|
else if (event == CV_EVENT_LBUTTONUP) { //在結束之后進行起點和終點的連接 } |
二、對一系列點組成的封閉圖形進行填充
采用多邊形填充函數fillpoly 取一系列隊的首地址,進行多邊形的填充,vctPoint保存鼠標滑過的一系列的點
1
2
3
4
5
6
|
const cv::Point * ppt[1] = { &vctPoint[0] }; //取數組的首地址 int len = vctPoint.size(); int npt[] = { len }; org.copyTo(maskImage); maskImage.setTo(cv::Scalar(0,0, 0, 0)); cv::fillPoly(maskImage, ppt, npt, 1, cv::Scalar(0,255, 255, 255)); |
完整的代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#include <iostream> #include <vector> #include <opencv.hpp> #include <opencv2\opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/core/core.hpp> static std::vector<std::vector<cv::Point>> vctvctPoint; cv::Mat org = cv::imread( "images/kaola.jpg" ); cv::Mat dst, maskImage; static std::vector<cv::Point> vctPoint; static cv::Point ptStart = (-1, -1); //初始化起點 static cv::Point cur_pt = (-1, -1); //初始化臨時節點 char temp[16]; void on_mouse( int event, int x, int y, int flags, void *ustc) //event鼠標事件代號,x,y鼠標坐標,flags拖拽和鍵盤操作的代號 { if (event == CV_EVENT_LBUTTONDOWN) { std::cout << "x:" << x << " y:" << y << std::endl; ptStart = cv::Point(x, y); vctPoint.push_back(ptStart); cv::circle(org, ptStart, 1, cv::Scalar(255, 0, 255), CV_FILLED, CV_AA, 0); cv::imshow( "圖片" , org); //cv::putText(tmp, temp, ptStart, cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0, 0), 1, 8); } else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON)) { std::cout << "x:" << x << " y:" << y << std::endl; cur_pt = cv::Point(x, y); cv::line(org, vctPoint.back(), cur_pt, cv::Scalar(0, 255, 0, 0), 1, 8, 0); cv::circle(org, cur_pt, 1, cv::Scalar(255, 0, 255), CV_FILLED, CV_AA, 0); cv::imshow( "圖片" , org); vctPoint.push_back(cur_pt); //cv::putText(tmp, temp, cur_pt, cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0, 0)); } else if (event == CV_EVENT_LBUTTONUP) { std::cout << "x:" << x << " y:" << y << std::endl; cur_pt = cv::Point(x, y); cv::line(org, ptStart, cur_pt, cv::Scalar(0, 255, 0, 0), 1, 8, 0); cv::circle(org, cur_pt, 1, cv::Scalar(255, 0, 255), CV_FILLED, CV_AA, 0); cv::imshow( "圖片" , org); vctPoint.push_back(cur_pt); vctvctPoint.push_back(vctPoint); //把點構成任意多邊形進行填充 const cv::Point * ppt[1] = { &vctPoint[0] }; //取數組的首地址 int len = vctPoint.size(); int npt[] = { len }; // cv::polylines(org, ppt, npt, 1, 1, cv::Scalar(0,0, 0, 0), 1, 8, 0); org.copyTo(maskImage); maskImage.setTo(cv::Scalar(0,0, 0, 0)); cv::fillPoly(maskImage, ppt, npt, 1, cv::Scalar(0,255, 255, 255)); org.copyTo(dst ,maskImage); cv::imshow( "摳圖" ,dst); cv::waitKey(0); } } int main() { //鼠標點擊 cv::namedWindow( "圖片" ); //定義一個img窗口 cv::setMouseCallback( "圖片" , on_mouse, 0); //調用回調函數 cv::imshow( "圖片" , org); cv::waitKey(0); return 0; } |
效果如下所示:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/helloworldding/article/details/83375408