[opencv]鼠标画彩图(改变绘制颜色和线宽)

本文介绍如何使用OpenCV实现鼠标绘制彩色图像的功能,并通过创建滚动条来改变绘制的颜色和线宽。文中详细解释了SetMouseCallback和CreateTrackbar等关键函数的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.opencv相关函数

1.1 SetMouseCallback ( name, onMouse, param )
Parameters:
第一个参数为使用窗口名,第二个为onMouse鼠标消息消息处理函数,第三个为传给鼠标消息的任定参数。

1.2 CvMouseCallback鼠标消息回调函数

typedef void (CV_CDECL *CvMouseCallback )(int eventint xint yint flagsvoidparam);

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         中鍵雙擊
x,y:
鼠标的坐标
flag:
#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不放事件
param:
传给callback的参数。

1.3CreateTrackbar(trackbarName, windowName, value, count, onChange)

Parameters:
  • trackbarname – Name of the created trackbar.
  • winname – Name of the window that will be used as a parent of the created trackbar.
  • value – Optional pointer to an integer variable whose value reflects the position of the slider. Upon creation, the slider position is defined by this variable.
  • count – Maximal position of the slider. The minimal position is always 0.
  • onChange – Pointer to the function to be called every time the slider changes position. This function should be prototyped as void Foo(int,void*); , where the first parameter is the trackbar position and the second parameter is the user data (see the next parameter).If the callback is the NULL pointer, no callbacks are called, but only value is updated.
  • userdata – User data that is passed as is to the callback. It can be used to handle trackbar events without using global variables.
trackbarname:滚动条名字
winname :处理窗口名字
value:处理参数
count:参数最大值
onChange:回调函数
userdata:返回参数

2.opencv实现鼠标画彩图(改变绘制颜色和线宽)

全部代码如下:
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
using namespace cv;
using namespace std;

Mat img(500, 500, CV_32FC3, Scalar(255, 255, 255)),img0;
Point bpoint;
int B = 0;
int G = 255;
int R = 0;
int thickness = 1;

void mouse(int event, int x, int y, int flags, void *param)
{
   static bool mousedown = false;
	switch (event)
	{
	case CV_EVENT_LBUTTONDOWN:
		mousedown = true;
	    bpoint= Point(x,y);
		break;
	case CV_EVENT_LBUTTONUP:
		mousedown = false;
		break;
	case CV_EVENT_MOUSEMOVE:
		if (mousedown)
		{
		    Point cpoint = Point(x, y);	
			line(img,bpoint,cpoint,Scalar(B,G,R),thickness);
			imshow("DRAW", img);
			bpoint = cpoint;
		}
			break;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	img.copyTo(img0);
	namedWindow("DRAW",1);
	setMouseCallback("DRAW",mouse,0);

	createTrackbar("颜色B分量","DRAW",&B,255,NULL);
	createTrackbar("颜色G分量", "DRAW", &G, 255, NULL);
	createTrackbar("颜色R分量", "DRAW", &R, 255, NULL);
	createTrackbar("线宽", "DRAW", &thickness,50, NULL);

	imshow("DRAW", img);
	char a = waitKey(0);
	if (a == 's')
	{
		imwrite("DRAW.jpg", img);
		waitKey(1);
	}
	else if (a == 'r')
	{
		img = img0;
		imshow("DRAW", img);
		waitKey(0);
	}
	else
	{
		waitKey(0);
	}
	return 0;
}

效果如下:

绘画水平略渣。。。


资源见http://download.youkuaiyun.com/detail/kingcooper/9718582



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值