Opencv实现画笔功能

时间:2021-05-20

本文实例为大家分享了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; 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); } return 0;}

按1是清除画面

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章