今天进行了简单的图像修补
// 图像修补.cpp: 先对图像进行破坏,然后进行修补
//
#include "stdafx.h"
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<opencv2\photo\photo.hpp>
#include<iostream>
using namespace std;
using namespace cv;
#define WIN_NAME1 "【原始图】"
#define WIN_NAME2 "【修补后的效果图】"
Mat srcImage1, inpaintMask;
Point previousPoint(-1, -1);
void onmouse(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 == CV_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(WIN_NAME1,srcImage1);
}
}
int main()
{
Mat srcImage = imread("fengjing.jpg");
inpaintMask=Mat::zeros(srcImage.size(), CV_8UC1);
if(!srcImage.data)
{
cout << "读取图片错误..." << endl; return 0;
}
srcImage1=srcImage.clone();
imshow(WIN_NAME1, srcImage1);
setMouseCallback(WIN_NAME1, onmouse, 0);
while (1)
{
char ch = (char)waitKey();
if (ch == 'q' || ch == 'Q')
{
break;
}
if (ch == '2')//如果ch==2,展示原始图像,其实就是把srcImage重新搬回srcImage1
{
inpaintMask = Scalar::all(0);//这步无所谓,把inpaitmask设置为全0
srcImage.copyTo(srcImage1);
imshow(WIN_NAME1, srcImage1);
}
if (ch == '1'|| ch==' ')//如果ch==2,修补图像
{
Mat inpaintedImage;
inpaint(srcImage1, inpaintMask, inpaintedImage, 3, INPAINT_TELEA);
imshow(WIN_NAME2, inpaintedImage);
}
}
return 0;
}