opencv/samples/cpp/inpaint.cpp

91 lines
2.3 KiB
C++
Raw Normal View History

2010-11-28 20:41:55 +01:00
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/photo/photo.hpp"
2010-11-28 20:41:55 +01:00
#include <iostream>
using namespace cv;
using namespace std;
2012-06-07 19:21:29 +02:00
static void help()
2010-12-04 09:29:39 +01:00
{
cout << "\nCool inpainging demo. Inpainting repairs damage to images by floodfilling the damage \n"
2012-06-07 19:21:29 +02:00
<< "with surrounding image areas.\n"
"Using OpenCV version %s\n" << CV_VERSION << "\n"
"Usage:\n"
"./inpaint [image_name -- Default fruits.jpg]\n" << endl;
2010-12-04 09:29:39 +01: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-28 20:41:55 +01:00
Mat img, inpaintMask;
Point prevPt(-1,-1);
2012-06-07 19:21:29 +02:00
static void onMouse( int event, int x, int y, int flags, void* )
2010-11-28 20:41:55 +01:00
{
if( event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON) )
prevPt = Point(-1,-1);
else if( event == CV_EVENT_LBUTTONDOWN )
prevPt = Point(x,y);
else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_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);
}
}
int main( int argc, char** argv )
{
char* filename = argc >= 2 ? argv[1] : (char*)"fruits.jpg";
Mat img0 = imread(filename, -1);
if(img0.empty())
{
2010-12-04 09:29:39 +01:00
cout << "Couldn't open the image " << filename << ". Usage: inpaint <image_name>\n" << endl;
2010-11-28 20:41:55 +01:00
return 0;
}
2010-12-04 09:29:39 +01:00
help();
2012-06-07 19:21:29 +02:00
2010-11-28 20:41:55 +01:00
namedWindow( "image", 1 );
img = img0.clone();
inpaintMask = Mat::zeros(img.size(), CV_8U);
imshow("image", img);
setMouseCallback( "image", onMouse, 0 );
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;
inpaint(img, inpaintMask, inpainted, 3, CV_INPAINT_TELEA);
imshow("inpainted image", inpainted);
}
}
return 0;
}