Opencv用鼠标画圆

#include
#include

#define WINDOW_NAME "鼠标事件窗口"
#define EVENT_WINDOW "滚动条窗口"
#define PI 3.1415926

using namespace std;
using namespace cv;

void DrawCircle(Mat &img, Point center, int radius, Scalar color, int thickness = 2, int lineType = 8);
void DrawRectangle(Mat &img, Rect box);
void onMouse(int event, int x, int y, int flag, void *param);
void on_Trackbar_b(int, void *);
void on_Trackbar_g(int, void *);
void on_Trackbar_r(int, void *);
void on_Trackbar_switch(int, void *);
void on_Trackbar_proportion(int, void *);
void on_Trackbar_Thick(int, void *);

Rect g_rectangle;
bool g_bDrawingBox = false;
RNG g_rng(12345);
Point g_startpoint;
double g_angle;
Point g_currentPoint;
int g_CurRadius;
Scalar g_EllCurColor;
double proportion = 0;
int EllCurThick;

//滑动条
int g_nAlphaValueSlider_b, g_nAlphaValueSlider_g, g_nAlphaValueSlider_r, g_nAlphaValueSlider_switch, g_nAlphaValueSlider_pro,
g_EllCurThick;
const int g_nMaxAlphaValue = 255, g_switch = 1, g_proportion_max = 11, g_EllCurThickMax = 100;
int switch_flag = 0;

int main()
{
	Mat srcImage(600, 800, CV_8UC3);
	Mat winImage(100, 600, CV_8UC3);
	Mat tempImage;

	srcImage = Scalar::all(0);
	winImage = Scalar::all(0);

	srcImage.copyTo(tempImage);

	/*namedWindow(WINDOW_NAME);
	setMouseCallback(WINDOW_NAME, onMouse, (void *)&srcImage);
	onMouse(0, 0, 0, 0, 0);*/

	g_nAlphaValueSlider_b = 50;
	g_nAlphaValueSlider_g = 50;
	g_nAlphaValueSlider_r = 50;
	g_nAlphaValueSlider_switch = 0;
	g_nAlphaValueSlider_pro = 0;
	namedWindow(EVENT_WINDOW);
	imshow(EVENT_WINDOW, winImage);
	createTrackbar("blue", EVENT_WINDOW, &g_nAlphaValueSlider_b, g_nMaxAlphaValue, on_Trackbar_b);
	on_Trackbar_b(g_nAlphaValueSlider_b, 0);
	createTrackbar("green", EVENT_WINDOW, &g_nAlphaValueSlider_g, g_nMaxAlphaValue, on_Trackbar_g);
	on_Trackbar_g(g_nAlphaValueSlider_g, 0);
	createTrackbar("red", EVENT_WINDOW, &g_nAlphaValueSlider_r, g_nMaxAlphaValue, on_Trackbar_r);
	on_Trackbar_r(g_nAlphaValueSlider_r, 0);
	createTrackbar("Thick", EVENT_WINDOW, &g_EllCurThick, g_EllCurThickMax, on_Trackbar_Thick);
	on_Trackbar_switch(g_EllCurThick, 0);
	createTrackbar("proportion", EVENT_WINDOW, &g_nAlphaValueSlider_pro, g_proportion_max, on_Trackbar_proportion);
	on_Trackbar_switch(g_nAlphaValueSlider_pro, 0);
	createTrackbar("switch", EVENT_WINDOW, &g_nAlphaValueSlider_switch, g_switch, on_Trackbar_switch);
	on_Trackbar_switch(g_nAlphaValueSlider_switch, 0);

	while (1)
	{
		if (switch_flag)
		{
			namedWindow(WINDOW_NAME);
			setMouseCallback(WINDOW_NAME, onMouse, (void *)&srcImage);

			while (1)
			{
				srcImage.copyTo(tempImage);

				if (g_bDrawingBox)
					DrawCircle(tempImage, g_startpoint, g_CurRadius, g_EllCurColor, EllCurThick);

				imshow(EVENT_WINDOW, winImage);
				imshow(WINDOW_NAME, tempImage);

				if (!switch_flag)
					break;

				if (waitKey(10) == 27)
					break;
			}
		}

		if (waitKey(10) == 27)
			break;
	}

	return 0;
}

void DrawCircle(Mat &img, Point center, int radius, Scalar color, int thickness, int lineType)
{
	circle(img, center, radius, color, thickness, lineType);
}

void DrawRectangle(Mat &img, Rect box)
{
	rectangle(img, box.tl(), box.br(), Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255)));
}

void onMouse(int event, int x, int y, int flag, void *param)
{
	Mat &img = *(cv::Mat*)param;

	switch (event)
	{
		//移动鼠标的时候
	case CV_EVENT_MOUSEMOVE:
	{
							   if (g_bDrawingBox)
							   {
								   g_currentPoint = Point(x, y);
								   g_CurRadius = (int)pow((g_startpoint.x - g_currentPoint.x) *
									   (g_startpoint.x - g_currentPoint.x) + (g_startpoint.y - g_currentPoint.y) *
									   (g_startpoint.y - g_currentPoint.y), 0.5);
							   }
	}
		break;
		//点击鼠标左键时
	case CV_EVENT_LBUTTONDOWN:
	{
								 g_CurRadius = 0;
								 g_bDrawingBox = true;
								 g_startpoint = Point(x, y);
	}
		break;
		//松开鼠标左键时
	case CV_EVENT_LBUTTONUP:
	{
							   g_bDrawingBox = false;

							   DrawCircle(img, g_startpoint, g_CurRadius, g_EllCurColor, EllCurThick);
	}
		break;
	}
}

void on_Trackbar_b(int, void *)
{
	g_EllCurColor = Scalar(g_nAlphaValueSlider_b, g_nAlphaValueSlider_g, g_nAlphaValueSlider_r);
}
void on_Trackbar_g(int, void *)
{
	g_EllCurColor = Scalar(g_nAlphaValueSlider_b, g_nAlphaValueSlider_g, g_nAlphaValueSlider_r);
}
void on_Trackbar_r(int, void *)
{
	g_EllCurColor = Scalar(g_nAlphaValueSlider_b, g_nAlphaValueSlider_g, g_nAlphaValueSlider_r);
}
void on_Trackbar_switch(int, void *)
{
	switch_flag = g_nAlphaValueSlider_switch;
}
void on_Trackbar_proportion(int, void *)
{
	proportion = g_nAlphaValueSlider_pro;
}
void on_Trackbar_Thick(int, void *)
{
	EllCurThick = g_EllCurThick;
}


Opencv用鼠标画圆_第1张图片

Opencv用鼠标画圆_第2张图片

Opencv用鼠标画圆_第3张图片

你可能感兴趣的:(opencv学习之路)