#include<opencv2/opencv.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream>
using namespace std;
using namespace cv;
#define WINDOW_NAME1 "【原始图】"
#define WINDOW_NAME2 "【修复图】"
Mat srcImage1, inpaintMask;
static void ShowHelpText();
Point previousPoint(-1, -1);
//响应鼠标的回调函数
static void On_Mouse(int event, int x, int y, int flags, void *)
{
//鼠标左键弹起消息
if (event == EVENT_LBUTTONUP || !(flags & EVENT_FLAG_LBUTTON))
previousPoint = Point(-1, -1);
//鼠标左键按下
else if (event == EVENT_LBUTTONDOWN)
previousPoint = Point(x, y);
//鼠标移动进行绘制
else if (event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON))
{
Point pt(x,y);
if (previousPoint.x < 0)
previousPoint = pt;
//绘制白色线条
line(inpaintMask, previousPoint, pt, Scalar::all(255), 5, 8, 0);
line(srcImage1, previousPoint, pt, Scalar::all(255), 5, 8, 0);
previousPoint = pt;
imshow(WINDOW_NAME1, srcImage1);
}
}
int main()
{
system("color 2F");
ShowHelpText();
Mat srcImage = imread("E:\\Pec\\蜘蛛侠.jpg", -1);
srcImage1 = srcImage.clone();
inpaintMask = Mat::zeros(srcImage1.size(), CV_8U);
imshow(WINDOW_NAME1, srcImage);
//设置鼠标回调消息
setMouseCallback(WINDOW_NAME1, On_Mouse, 0);
while (1)
{
int c = waitKey();
if ((char)c == 27)
break;
//按键2按下时,恢复源图
if (c == '2')
{
inpaintMask = Scalar::all(0);
srcImage.copyTo(srcImage1);
imshow(WINDOW_NAME1, srcImage1);
}
//若检测到按键为1或者空格,则进行处理
if ((char)c == '1' || (char)c == ' ')
{
Mat inpaintedImage;
inpaint(srcImage1, inpaintMask, inpaintedImage, 3, INPAINT_TELEA);
imshow(WINDOW_NAME2, inpaintedImage);
}
}
return 0;
}
static void ShowHelpText()
{
cout << "\n\t欢迎来到【图像修复】示例程序" << endl;
cout << endl;
cout << "\t\t在对图像操作之前,在【原始图】窗口进行适量绘制" << endl;
cout << "\t\t按键操作如下:" << endl;
cout << "\t\t\t按下按键【鼠标左键】-- 在图像绘制白条" << endl;
cout << "\t\t\t按下按键【ESC】--退出程序" << endl;
cout << "\t\t\t按下按键【1】 或者 【空格】-- 图像修复" << endl;
}