1、实现图像修补的函数
void inpaint(InputArray src,InputArray inpaintMask,OutputArray dst,double inpaintRadius,int flags)
参数一:输入图像阵列,Mat 类且是8位单通道或三通道图像
参数二:修复掩摸,8位单通道图像
参数三:输出图像
参数四:修复算法的参考半径
参数五:修补方法的标志符
INPAINT_NS 基于Noviers_stokes方程的方法
INPAINT_TELEA Alexandru telea方法
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/photo/photo.hpp>
#include <iostream>
#define WINDOW_NAME1 "原始图"
#define WINDOW_NAME2 "修复图"
using namespace std;
using namespace cv;
Mat srcImage1,inpaintMask;
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(int argc,char *argv[])
{
if(argc!=2)
{
cout<<"input error"<<endl;
return -1;
}
Mat srcImage = imread(argv[1],-1);
if(srcImage.empty())
{
cout<<"read photo fail"<<endl;
return -1;
}
srcImage1 = srcImage.clone();
inpaintMask = Mat::zeros(srcImage1.size(),CV_8U);
imshow(WINDOW_NAME1,srcImage1);
setMouseCallback(WINDOW_NAME1,on_Mouse,0);
while(1)
{
char c = (char )waitKey(0);
if(c == 27)
break;
if(c == '2')
{
inpaintMask = Scalar::all(0);
srcImage.copyTo(srcImage1);
imshow(WINDOW_NAME1,srcImage1);
}
if(c=='1'||c==' ')
{
Mat inpaintedImage;
inpaint(srcImage1,inpaintMask,inpaintedImage,3,INPAINT_TELEA);
imshow(WINDOW_NAME2,inpaintedImage);
}
}
return 0;
}
来自:opencv编程入门 毛星云pdf