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

1.opencv相关函数

1.1 SetMouseCallback ( name, onMouse, param )
Parameters:
  • name – Window name
  • onMouse – Mouse callback. See OpenCV samples, such ashttps://code.ros.org/svn/opencv/trunk/opencv/samples/cpp/ffilldemo.cpp, on how to specify and use the callback.
  • param – The optional parameter passed to the callback.
第一个参数为使用窗口名,第二个为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;
}

效果如下:
[opencv]鼠标画彩图(改变绘制颜色和线宽)_第1张图片
绘画水平略渣。。。


资源见http://download.csdn.net/detail/kingcooper/9718582



你可能感兴趣的:(计算机视觉,Opencv从入门到精通)