forked from spmallick/learnopencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinpaint.cpp
97 lines (85 loc) · 2.62 KB
/
inpaint.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/photo.hpp"
#include <iostream>
using namespace cv;
using namespace std;
// Declare Mat objects for original image and mask for inpainting
Mat img, inpaintMask;
// Mat object for result output
Mat res;
Point prevPt(-1,-1);
// onMouse function for Mouse Handling
// Used to draw regions required to inpaint
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("image", img);
imshow("image: mask", inpaintMask);
}
}
int main( int argc, char** argv )
{
cout << "Usage: ./inpaint <image_path>" << endl;
cout << "Keys: " << endl;
cout << "t - inpaint using FMM" << endl;
cout << "n - inpaint using NS technique" << endl;
cout << "r - reset the inpainting mask" << endl;
cout << "ESC - exit" << endl;
string filename;
if(argc > 1)
filename = argv[1];
else
filename = "sample.jpeg";
// Read image in color mode
img = imread(filename, IMREAD_COLOR);
Mat img_mask;
// Return error if image not read properly
if(img.empty())
{
cout << "Failed to load image: " << filename << endl;
return 0;
}
namedWindow("image", WINDOW_AUTOSIZE);
// Create a copy for the original image
img_mask = img.clone();
// Initialize mask (black image)
inpaintMask = Mat::zeros(img_mask.size(), CV_8U);
// Show the original image
imshow("image", img);
setMouseCallback( "image", onMouse, NULL);
for(;;)
{
char c = (char)waitKey();
if (c == 't') {
// Use Algorithm proposed by Alexendra Telea
inpaint(img, inpaintMask, res, 3, INPAINT_TELEA);
imshow("Inpaint Output using FMM", res);
}
if (c == 'n') {
// Use Algorithm proposed by Bertalmio et. al.
inpaint(img, inpaintMask, res, 3, INPAINT_NS);
imshow("Inpaint Output using NS Technique", res);
}
if (c == 'r') {
inpaintMask = Scalar::all(0);
img_mask.copyTo(img);
imshow("image", inpaintMask);
}
if ( c == 27 )
break;
}
return 0;
}