您好,登錄后才能下訂單哦!
小編給大家分享一下如何實現Opencv畫筆功能的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
具體內容如下
#include<iostream> #include<opencv2/opencv.hpp> using namespace std; using namespace cv; Rect g_rectangle; bool g_bDrawing = false; RNG g_rng(12345); Point g_nCurrPoint; int g_nThick = 0, g_nBlue = 255, g_nGreen = 255, g_nRed = 0; /*注意:不能在毀掉函數中寫入未初始化的矩陣類,所以需要用時,需要寫一個標志位,然后再在while(1)循環內使用*/ void onMouse(int event, int x, int y, int flag, void *param) { Mat &img = *(cv::Mat*)param; switch (event) { //移動鼠標的時候 case CV_EVENT_MOUSEMOVE: { g_nCurrPoint = Point(x, y); } break; //點擊鼠標左鍵時 case CV_EVENT_LBUTTONDOWN: { g_bDrawing = true; g_nCurrPoint = Point(x, y); } break; //松開鼠標左鍵時 case CV_EVENT_LBUTTONUP: { g_bDrawing = false; } break; } } int main() { Mat srcImage(600, 800, CV_8UC3); Mat tempImage; srcImage.copyTo(tempImage); srcImage = Scalar::all(0); namedWindow("【鼠標事件窗口】", 0); setMouseCallback("【鼠標事件窗口】", onMouse, (void *)&srcImage); namedWindow("【滾動條窗口】", 0); createTrackbar("thick", "【滾動條窗口】", &g_nThick, 100, 0); createTrackbar("Blue", "【滾動條窗口】", &g_nBlue, 255, 0); createTrackbar("Green", "【滾動條窗口】", &g_nGreen, 255, 0); createTrackbar("Red", "【滾動條窗口】", &g_nRed, 255, 0); char key; while (1) { if (g_bDrawing) { circle(srcImage, g_nCurrPoint, 0, Scalar(g_nBlue, g_nGreen, g_nRed), g_nThick + 1); } imshow("【鼠標事件窗口】", srcImage); key = waitKey(1); if (key == 27) break; if (key == '1') srcImage = Scalar::all(0); /*DrawRectangle(srcImage, g_rectangle); imshow(WINDOW_NAME, srcImage); if (waitKey(10) == 27) break;*/ } return 0; }
按1是清除畫面
看完了這篇文章,相信你對如何實現Opencv畫筆功能的方法有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。