openCV里的图像修复函数是inpaint()。
void inpaint(InputArray src, InputArray inpaintMask, OutputArray dst, double inpaintRadius, int flags)
参数说明:
openCV提供了一个例子,代码如下:
#include<opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
static void help()
{
cout << "\nCool inpainging demo. Inpainting repairs damage to images by floodfilling the damage \n"
<< "with surrounding image areas.\n"
"Using OpenCV version %s\n" << CV_VERSION << "\n";
cout << "Hot keys: \n"
"\tESC - quit the program\n"
"\tr - restore the original image\n"
"\ti or SPACE - run inpainting algorithm\n"
"\t\t(before running it, paint something on the image)\n" << endl;
}
Mat img, inpaintMask;
Point prevPt(-1,-1);
static void onMouse( int event, int x, int y, int flags, void* )
{
if( event == EVENT_LBUTTONUP || !(flags & EVENT_FLAG_LBUTTON) )
prevPt = Point(-1,-1);
else if( event == EVENT_LBUTTONDOWN )
prevPt = Point(x,y);
else if( event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON) )
{
Point pt(x,y);
if( prevPt.x < 0 )
prevPt = pt;
line( inpaintMask, prevPt, pt, Scalar::all(255), 5, 8, 0 );
line( img, prevPt, pt, Scalar::all(255), 5, 8, 0 );
prevPt = pt;
imshow("原图", img);
}
}
int main(){
Mat img0 = imread("apple.jpg");
if(img0.empty())
{
cout << "Couldn't open the image "<< endl;
return 0;
}
help();
img = img0.clone();
inpaintMask = Mat::zeros(img.size(), CV_8U);
imshow("原图", img);
setMouseCallback( "原图", onMouse, 0 );
for(;;)
{
char c = (char)waitKey();
if( c == 27 )
break;
if( c == 'r' )
{
inpaintMask = Scalar::all(0);
img0.copyTo(img);
imshow("原图", img);
}
if( c == 'i' || c == ' ' )
{
Mat inpainted;
inpaint(img, inpaintMask, inpainted, 3, INPAINT_TELEA);
imshow("修复后", inpainted);
imshow("图像掩码",inpaintMask);
}
}
return 0;
}
效果如下:
原图:
修复过程:
图像掩码:
用鼠标在原图上画出要修复的部分,按R键或空格进行修复。这个代码对去掉照片上的水印或者线条状的干扰时很有用,不过如果想修复照片上的整块区域时,需要不停地画一块区域,不太方便,所以我又写了一个鼠标拖拽出矩形的demo。代码如下:
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
Point startPoint,endPoint;
Mat srcImage, inpaintMask, dstImage;
string imgName = "xue.jpg";
enum{NOT_SET = 0, IN_PROCESS = 1, SET = 2};
uchar rectState;//鼠标状态
void onMouse( int event, int x, int y, int flags, void* ustc){
switch( event ){
case CV_EVENT_LBUTTONDOWN:
if( rectState == NOT_SET){
rectState = IN_PROCESS;
startPoint = Point(x,y);
endPoint = Point(x,y);
}
break;
case CV_EVENT_LBUTTONUP:
if( rectState == IN_PROCESS ){
rectState = SET;
rectangle(inpaintMask,startPoint,endPoint,Scalar::all(255),-1);//-1表示直接填充矩形区域
inpaint(srcImage, inpaintMask, dstImage, 1, INPAINT_TELEA);
imshow("图像掩码", inpaintMask);
imshow("修复后", dstImage);
rectState = NOT_SET;//实现反复画矩形,矩形累加
}
break;
case CV_EVENT_MOUSEMOVE:
if( rectState == IN_PROCESS ){
endPoint = Point(x,y);
srcImage = imread( imgName, 1 );
rectangle(srcImage,startPoint,endPoint,Scalar(0,255,0),1);
imshow("原图", srcImage);
}
break;
}
}
int main(){
rectState == NOT_SET;
srcImage = imread(imgName);
if(!srcImage.data)
{
cout << "Couldn't open the image "<< endl;
return 0;
}
inpaintMask = Mat::zeros(srcImage.size(),CV_8UC1);
dstImage = Mat::zeros(srcImage.size(),CV_8UC3);
imshow("原图", srcImage);
setMouseCallback("原图", onMouse, NULL);
waitKey(0);
return 0;
}
效果如下:(去掉雪地里的脚印)
修复算法原理请看下面链接的文章:
算法原理