opencv3实现图像修补-inpaint函数

#include
#include

using namespace std;
using namespace cv;

bool g_bDrawing = false;
Point g_CurrPoint, g_OrgPoint;
int g_nThick = 5, g_nBlue = 255, g_nGreen = 255, g_nRed = 0;
int g_nImageOneValue = 49;
Mat srcImage;
Mat grayImage;
Mat maskImage;

/*注意:不能在毁掉函数中写入未初始化的矩阵类,所以需要用时,需要写一个标志位,然后再在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_OrgPoint = g_CurrPoint;
							   g_CurrPoint = Point(x, y);

							   if (g_bDrawing == 1)
							   {
								   line(srcImage, g_CurrPoint, g_OrgPoint, Scalar(g_nBlue, g_nGreen, g_nRed), g_nThick);
								   imshow("【鼠标事件窗口】", srcImage);

								   //在掩膜图上进行显示
								   line(maskImage, g_CurrPoint, g_OrgPoint, Scalar(g_nBlue, g_nGreen, g_nRed), g_nThick);
								   imshow("【掩膜图像】", maskImage);
							   }
	}
		break;
		//点击鼠标左键时
	case CV_EVENT_LBUTTONDOWN:
	{
								 g_bDrawing = true;
								 g_OrgPoint = Point(x, y);
								 g_CurrPoint = g_OrgPoint;
	}
		break;
		//松开鼠标左键时
	case CV_EVENT_LBUTTONUP:
	{
							   g_bDrawing = false;
	}
		break;
	}
}

int main()
{
	Mat tempImage;
	RNG &rng = theRNG();

	srcImage = imread("lili.jpg");

	//用一个变量来存储原图像
	Mat g_srcImage;
	srcImage.copyTo(g_srcImage);

	//为掩膜图 分配空间
	maskImage.create(srcImage.size(), CV_8UC1);
	maskImage = Scalar::all(0);

	namedWindow("【鼠标事件窗口】");
	setMouseCallback("【鼠标事件窗口】", onMouse, 0);

	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)
	{
		imshow("【鼠标事件窗口】", srcImage);
		key = waitKey();
		if (key == 27)
			break;

		//如果检测到 键值是1 则恢复原图
		if (key == '1')
		{
			g_srcImage.copyTo(srcImage);
			maskImage = Scalar::all(0);
			imshow("【鼠标事件窗口】", srcImage);
		}

		//如果检测到空格 则开始执行图像修复
		Mat dstImage;
		dstImage.create(srcImage.size(), srcImage.type());
		if (key == ' ')
		{
			inpaint(srcImage, maskImage, dstImage, 3, INPAINT_TELEA);
			imshow("【修补后的图像】", dstImage);
		}
	}

	return 0;
}

opencv3实现图像修补-inpaint函数_第1张图片

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