opencv/samples/cpp/inpaint.cpp

92 lines
2.4 KiB
C++
Raw Normal View History

2014-07-04 22:48:15 +08:00
#include "opencv2/imgcodecs.hpp"
2016-02-15 21:37:29 +08:00
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/photo.hpp"
2010-11-29 03:41:55 +08:00
#include <iostream>
using namespace cv;
using namespace std;
2019-07-28 17:09:17 +08:00
static void help( char** argv )
2010-12-04 16:29:39 +08:00
{
cout << "\nCool inpainging demo. Inpainting repairs damage to images by floodfilling the damage \n"
2012-06-08 01:21:29 +08:00
<< "with surrounding image areas.\n"
"Using OpenCV version %s\n" << CV_VERSION << "\n"
2019-07-28 17:09:17 +08:00
"Usage:\n" << argv[0] <<" [image_name -- Default fruits.jpg]\n" << endl;
2010-12-04 16:29:39 +08:00
cout << "Hot keys: \n"
"\tESC - quit the program\n"
"\tr - restore the original image\n"
"\ti or SPACE - run inpainting algorithm\n"
"\t\t(before running it, paint something on the image)\n" << endl;
}
2010-11-29 03:41:55 +08:00
Mat img, inpaintMask;
Point prevPt(-1,-1);
2012-06-08 01:21:29 +08:00
static void onMouse( int event, int x, int y, int flags, void* )
2010-11-29 03:41:55 +08:00
{
if( event == EVENT_LBUTTONUP || !(flags & EVENT_FLAG_LBUTTON) )
2010-11-29 03:41:55 +08:00
prevPt = Point(-1,-1);
else if( event == EVENT_LBUTTONDOWN )
2010-11-29 03:41:55 +08:00
prevPt = Point(x,y);
else if( event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON) )
2010-11-29 03:41:55 +08:00
{
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);
}
}
int main( int argc, char** argv )
{
2018-10-31 20:48:56 +08:00
cv::CommandLineParser parser(argc, argv, "{@image|fruits.jpg|}");
2019-07-28 17:09:17 +08:00
help(argv);
2017-07-26 13:39:53 +08:00
2018-10-31 20:48:56 +08:00
string filename = samples::findFile(parser.get<string>("@image"));
Mat img0 = imread(filename, IMREAD_COLOR);
2010-11-29 03:41:55 +08:00
if(img0.empty())
{
2010-12-04 16:29:39 +08:00
cout << "Couldn't open the image " << filename << ". Usage: inpaint <image_name>\n" << endl;
2010-11-29 03:41:55 +08:00
return 0;
}
2018-10-31 20:48:56 +08:00
namedWindow("image", WINDOW_AUTOSIZE);
2010-11-29 03:41:55 +08:00
img = img0.clone();
inpaintMask = Mat::zeros(img.size(), CV_8U);
imshow("image", img);
2018-10-31 20:48:56 +08:00
setMouseCallback( "image", onMouse, NULL);
2010-11-29 03:41:55 +08:00
for(;;)
{
char c = (char)waitKey();
if( c == 27 )
break;
if( c == 'r' )
{
inpaintMask = Scalar::all(0);
img0.copyTo(img);
imshow("image", img);
}
if( c == 'i' || c == ' ' )
{
Mat inpainted;
2013-04-08 03:23:53 +08:00
inpaint(img, inpaintMask, inpainted, 3, INPAINT_TELEA);
2010-11-29 03:41:55 +08:00
imshow("inpainted image", inpainted);
}
}
return 0;
}