/**
图片修复
*/
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
Point prevPt;
Mat src,inpaintMask;
void onMouse(int event, int x, int y, int flags, void* userdata){
if (event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON))
prevPt = Point(-1, -1);
else if (event == CV_EVENT_LBUTTONDOWN)
prevPt = Point(x, y);
else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))
{
Point pt(x, y);
if (prevPt.x < 0)
prevPt = pt;
line(inpaintMask, prevPt, pt, Scalar::all(255), 5, 8, 0);//mask
// 使用inpaint函数进行图片修复
inpaint(src, inpaintMask, src, 5, CV_INPAINT_TELEA);
prevPt = pt;
imshow("inpaint", src);
}
}
int main(int argc,char** argv){
string path = "/home/kaijun/Documents/resource/opencv/img/itheima_inpaint.jpg";
src = imread(path,IMREAD_COLOR);
// 定义窗口
namedWindow("inpaint",WINDOW_NORMAL);
// 定义鼠标事件
setMouseCallback("inpaint",onMouse);
// 显示原图
imshow("inpaint",src);
// 定义掩膜
inpaintMask = Mat(src.size(),CV_8UC1);
waitKey(0);
return 0;
}