Opencv用鼠标绘制填充多边形

时间:2020-12-06 09:03:17
#include<iostream>
#include<opencv2/opencv.hpp>

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

using namespace std;
using namespace cv;

//滚动条
int g_CurNum;
const int g_NumMax = 500, g_nMaxAlphaValue = 255, g_switch = 1;

Rect g_rectangle;
bool g_bDrawingBox = false;
RNG g_rng(12345);

Point g_CurPoint;
Point g_rookPoints[1][g_NumMax];
int g_nAlphaValueSlider_b, g_nAlphaValueSlider_g, g_nAlphaValueSlider_r, g_nAlphaValueSlider_switch;
Scalar g_PolyCurColor;

int g_PointNum = 10;
int switch_flag = 0;

void DrawPolygon(Mat &img, const Point **pts, const int *npts, int ncontours, Scalar color);
void onMouse(int event, int x, int y, int flag, void *param);
void on_Trackbar(int, void *);
void on_Trackbar_b(int, void *);
void on_Trackbar_g(int, void *);
void on_Trackbar_r(int, void *);
void on_Trackbar_switch(int, void *);

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

	srcImage.copyTo(tempImage);

	srcImage = Scalar::all(0);

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

	g_CurNum = 10;
	g_nAlphaValueSlider_b = 255;
	g_nAlphaValueSlider_g = 0;
	g_nAlphaValueSlider_r = 0;
	namedWindow("滚动条窗口");
	createTrackbar("点数", EVENT_WINDOW, &g_CurNum, g_NumMax, on_Trackbar);
	on_Trackbar(g_CurNum, 0);
	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("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)
				{
					const Point *ppt[1] = { g_rookPoints[0] };
					int npt[] = { g_CurNum };
					DrawPolygon(tempImage, ppt, npt, 1, g_PolyCurColor);
				}

				imshow(WINDOW_NAME, tempImage);

				if (!switch_flag)
					break;

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

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

	return 0;
}

void DrawPolygon(Mat &img, const Point **pts, const int *npts, int ncontours, Scalar color)
{
	fillPoly(img, pts, npts, ncontours, color);
}

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

	switch (event)
	{
	//	//移动鼠标的时候
	//case CV_EVENT_MOUSEMOVE:
	//{
	//						   if (g_bDrawingBox)
	//						   {
	//							   g_rectangle.width = x - g_rectangle.x;
	//							   g_rectangle.height = y - g_rectangle.y;
	//						   }
	//}
	//	break;
		//点击鼠标左键时
	case CV_EVENT_LBUTTONDOWN:
	{
								 g_bDrawingBox = true;
								 g_CurPoint = Point(x, y);
	}
		break;
		//松开鼠标左键时
	case CV_EVENT_LBUTTONUP:
	{
							   g_bDrawingBox = false;
							   g_rookPoints[0][_flag++] = g_CurPoint;

							   if (_flag == g_CurNum)
							   {
								   _flag = 0;

								   const Point *ppt[1] = { g_rookPoints[0] };
								   int npt[] = { g_CurNum };
								   DrawPolygon(img, ppt, npt, 1, g_PolyCurColor);
							   }
	}
		break;
	}
}

void on_Trackbar(int, void *)
{
	g_PointNum = g_CurNum;
}
void on_Trackbar_b(int, void *)
{
	g_PolyCurColor = Scalar(g_nAlphaValueSlider_b, g_nAlphaValueSlider_g, g_nAlphaValueSlider_r);
}
void on_Trackbar_g(int, void *)
{
	g_PolyCurColor = Scalar(g_nAlphaValueSlider_b, g_nAlphaValueSlider_g, g_nAlphaValueSlider_r);
}
void on_Trackbar_r(int, void *)
{
	g_PolyCurColor = Scalar(g_nAlphaValueSlider_b, g_nAlphaValueSlider_g, g_nAlphaValueSlider_r);
}
void on_Trackbar_switch(int, void *)
{
	switch_flag = g_nAlphaValueSlider_switch;
}


先打开开关才能绘制!!!!!!


Opencv用鼠标绘制填充多边形