inpaint.cpp 2.4 KB

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