本文实例为大家分享了opencv实现画笔功能的具体代码,供大家参考,具体内容如下
#include
#include
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是清除画面
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。
希望与广大网友互动??
点此进行留言吧!