一杯清酒邀明月
天下本无事,庸人扰之而烦耳。
 1 #include "opencv2/highgui.hpp"
 2 #include "opencv2/imgproc.hpp"
 3 #include "opencv2/photo.hpp"
 4 #include<iostream>
 5 using namespace std;
 6 using namespace cv;
 7  
 8 #define WINDOW_NAME1 "原始图"   
 9  
10 Mat imgSrc, inpaintMask;
11 Point prePoint;  //上一个鼠标点
12  
13 static void On_Mouse(int event, int x, int y, int flags, void*)  //鼠标画线响应函数
14 {
15     if (event == EVENT_LBUTTONDOWN)
16         prePoint = Point(x, y);
17     else if (event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON))//画白色线条
18     {
19         Point pt(x, y);
20         line(inpaintMask, prePoint, pt, Scalar::all(255), 3, 8, 0);//单通道掩膜,需要修复的像素此时为白色
21         line(imgSrc, prePoint, pt, Scalar::all(255), 3, 8, 0);
22         prePoint = pt;
23         imshow(WINDOW_NAME1, imgSrc);
24     }
25 }
26  
27  
28 //程序主要部分
29 int main( int argc, char** argv )
30 {
31      imgSrc = imread("zhi.jpg", 1);//三通道彩色图像
32      Mat imgTemp=imgSrc.clone();
33      imshow("最初图像",imgTemp);
34     if (!imgSrc.data)
35     {
36         cout << "no image!filepath error!~!" << endl;
37         return false;
38     }
39     inpaintMask = Mat::zeros(imgSrc.size(), CV_8UC1);
40     imshow(WINDOW_NAME1, imgSrc);
41     imshow("最初图像",imgTemp);
42     setMouseCallback(WINDOW_NAME1, On_Mouse, 0);
43  
44     while (1)
45     {
46         int key = waitKey();
47         switch (key)
48         {
49  
50             case 'a':
51             {
52                     Mat inpaintedImage;
53                     inpaint(imgSrc, inpaintMask, inpaintedImage, 5, INPAINT_TELEA);
54                     imshow("修补方法:_TELEA", inpaintedImage);
55             }
56             break;
57             case 'b':
58             {
59                     Mat inpaintedImage;
60                     inpaint(imgSrc, inpaintMask, inpaintedImage,5, INPAINT_NS);
61                     imshow("修补方法:_NS" , inpaintedImage);
62             }
63             break;
64         }
65         if (key == 27)
66             break;
67     }
68       return 0;
69 }

原图:

 把痣的位置圈出来:

 见证奇迹的时刻,修补后是这样了

 

posted on 2022-02-25 15:31  一杯清酒邀明月  阅读(403)  评论(0编辑  收藏  举报