Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/cpp/inpaint.cpp
16337 views
1
#include "opencv2/imgcodecs.hpp"
2
#include "opencv2/highgui.hpp"
3
#include "opencv2/imgproc.hpp"
4
#include "opencv2/photo.hpp"
5
6
#include <iostream>
7
8
using namespace cv;
9
using namespace std;
10
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
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
48
int main( int argc, char** argv )
49
{
50
cv::CommandLineParser parser(argc, argv, "{@image|../data/fruits.jpg|}");
51
help();
52
53
string filename = parser.get<string>("@image");
54
Mat img0 = imread(filename, -1);
55
if(img0.empty())
56
{
57
cout << "Couldn't open the image " << filename << ". Usage: inpaint <image_name>\n" << endl;
58
return 0;
59
}
60
61
namedWindow( "image", 1 );
62
63
img = img0.clone();
64
inpaintMask = Mat::zeros(img.size(), CV_8U);
65
66
imshow("image", img);
67
setMouseCallback( "image", onMouse, 0 );
68
69
for(;;)
70
{
71
char c = (char)waitKey();
72
73
if( c == 27 )
74
break;
75
76
if( c == 'r' )
77
{
78
inpaintMask = Scalar::all(0);
79
img0.copyTo(img);
80
imshow("image", img);
81
}
82
83
if( c == 'i' || c == ' ' )
84
{
85
Mat inpainted;
86
inpaint(img, inpaintMask, inpainted, 3, INPAINT_TELEA);
87
imshow("inpainted image", inpainted);
88
}
89
}
90
91
return 0;
92
}
93
94