1 #include "opencv2/imgcodecs.hpp"
2 #include "opencv2/highgui/highgui.hpp"
3 #include "opencv2/imgproc/imgproc.hpp"
4 #include "opencv2/photo/photo.hpp"
5
6 #include <iostream>
7
8 using namespace cv;
9 using namespace std;
10
help()11 static void help()
12 {
13 cout << "\nCool inpainging demo. Inpainting repairs damage to images by floodfilling the damage \n"
14 << "with surrounding image areas.\n"
15 "Using OpenCV version %s\n" << CV_VERSION << "\n"
16 "Usage:\n"
17 "./inpaint [image_name -- Default ../data/fruits.jpg]\n" << endl;
18
19 cout << "Hot keys: \n"
20 "\tESC - quit the program\n"
21 "\tr - restore the original image\n"
22 "\ti or SPACE - run inpainting algorithm\n"
23 "\t\t(before running it, paint something on the image)\n" << endl;
24 }
25
26 Mat img, inpaintMask;
27 Point prevPt(-1,-1);
28
onMouse(int event,int x,int y,int flags,void *)29 static void onMouse( int event, int x, int y, int flags, void* )
30 {
31 if( event == EVENT_LBUTTONUP || !(flags & EVENT_FLAG_LBUTTON) )
32 prevPt = Point(-1,-1);
33 else if( event == EVENT_LBUTTONDOWN )
34 prevPt = Point(x,y);
35 else if( event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON) )
36 {
37 Point pt(x,y);
38 if( prevPt.x < 0 )
39 prevPt = pt;
40 line( inpaintMask, prevPt, pt, Scalar::all(255), 5, 8, 0 );
41 line( img, prevPt, pt, Scalar::all(255), 5, 8, 0 );
42 prevPt = pt;
43 imshow("image", img);
44 }
45 }
46
47
main(int argc,char ** argv)48 int main( int argc, char** argv )
49 {
50 char* filename = argc >= 2 ? argv[1] : (char*)"../data/fruits.jpg";
51 Mat img0 = imread(filename, -1);
52 if(img0.empty())
53 {
54 cout << "Couldn't open the image " << filename << ". Usage: inpaint <image_name>\n" << endl;
55 return 0;
56 }
57
58 help();
59
60 namedWindow( "image", 1 );
61
62 img = img0.clone();
63 inpaintMask = Mat::zeros(img.size(), CV_8U);
64
65 imshow("image", img);
66 setMouseCallback( "image", onMouse, 0 );
67
68 for(;;)
69 {
70 char c = (char)waitKey();
71
72 if( c == 27 )
73 break;
74
75 if( c == 'r' )
76 {
77 inpaintMask = Scalar::all(0);
78 img0.copyTo(img);
79 imshow("image", img);
80 }
81
82 if( c == 'i' || c == ' ' )
83 {
84 Mat inpainted;
85 inpaint(img, inpaintMask, inpainted, 3, INPAINT_TELEA);
86 imshow("inpainted image", inpainted);
87 }
88 }
89
90 return 0;
91 }
92