//---------------------------------【头文件、命名空间包含部分】----------------------------
// 描述:包含程序所使用的头文件和命名空间
//------------------------------------------------------------------------------------------------
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/photo/photo.hpp"
#include <iostream>
using namespace cv;
using namespace std;//-----------------------------------【宏定义部分】--------------------------------------------
// 描述:定义一些辅助宏
//----------------------------------------------------------------------------------------------
#define WINDOW_NAME0 "【原始图参考】" //为窗口标题定义的宏
#define WINDOW_NAME1 "【原始图】" //为窗口标题定义的宏
#define WINDOW_NAME2 "【修补后的效果图】" //为窗口标题定义的宏 //-----------------------------------【全局变量声明部分】--------------------------------------
// 描述:全局变量声明
//-----------------------------------------------------------------------------------------------
Mat srcImage0,srcImage1, inpaintMask;
Point previousPoint(-1,-1);//原来的点坐标//-----------------------------------【ShowHelpText( )函数】----------------------------------
// 描述:输出一些帮助信息
//----------------------------------------------------------------------------------------------
static void ShowHelpText( )
{//输出一些帮助信息printf("\n\n\n\t欢迎来到【图像修复】示例程序~\n"); printf( "\n\t请在进行图像修复操作之前,在【原始图】窗口中进行适量的绘制" "\n\n\t按键操作说明: \n\n" "\t\t【鼠标左键】-在图像上绘制白色线条\n\n""\t\t键盘按键【ESC】- 退出程序\n\n" "\t\t键盘按键【1】或【SPACE】-进行图像修复操作 \n\n" );
}//-----------------------------------【On_Mouse( )函数】--------------------------------
// 描述:响应鼠标消息的回调函数
//----------------------------------------------------------------------------------------------
static void On_Mouse( int event, int x, int y, int flags, void* )
{//鼠标左键弹起消息if( event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON) )previousPoint = Point(-1,-1);//鼠标左键按下消息else if( event == CV_EVENT_LBUTTONDOWN )previousPoint = Point(x,y);//鼠标按下并移动,进行绘制else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_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);}
}//--------------------------------------【main( )函数】-----------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始执行
//-----------------------------------------------------------------------------------------------
int main( int argc, char** argv )
{//改变console字体颜色system("color 2F"); //显示帮助文字ShowHelpText();//载入原始图并进行掩膜的初始化Mat srcImage = imread("1.jpg", -1);if(!srcImage.data ) { printf("读取图片错误,请确定目录下是否有imread函数指定图片存在~! \n"); return false; } srcImage0 = srcImage.clone();srcImage1 = srcImage.clone();inpaintMask = Mat::zeros(srcImage1.size(), CV_8U);//显示原始图参考imshow(WINDOW_NAME0, srcImage0);//显示原始图imshow(WINDOW_NAME1, srcImage1);//设置鼠标回调消息setMouseCallback( WINDOW_NAME1, On_Mouse, 0 );//轮询按键,根据不同的按键进行处理while (1){//获取按键键值char c = (char)waitKey();//键值为ESC,程序退出if( c == 27 )break;//键值为2,恢复成原始图像if( c == '2' ){inpaintMask = Scalar::all(0);srcImage.copyTo(srcImage1);imshow(WINDOW_NAME1, srcImage1);}//键值为1或者空格,进行图像修补操作if( c == '1' || c == ' ' ){Mat inpaintedImage;inpaint(srcImage1, inpaintMask, inpaintedImage, 3, CV_INPAINT_TELEA);imshow(WINDOW_NAME2, inpaintedImage);}}return 0;
}