Blame view

3rdparty/opencv-4.5.4/samples/cpp/inpaint.cpp 2.38 KB
f4334277   Hu Chunming   提交3rdparty
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
  #include "opencv2/imgcodecs.hpp"
  #include "opencv2/highgui.hpp"
  #include "opencv2/imgproc.hpp"
  #include "opencv2/photo.hpp"
  
  #include <iostream>
  
  using namespace cv;
  using namespace std;
  
  static void help( char** argv )
  {
      cout << "\nCool inpainging demo. Inpainting repairs damage to images by floodfilling the damage \n"
              << "with surrounding image areas.\n"
              "Using OpenCV version %s\n" << CV_VERSION << "\n"
              "Usage:\n" << argv[0] <<" [image_name -- Default fruits.jpg]\n" << endl;
  
      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;
  }
  
  Mat img, inpaintMask;
  Point prevPt(-1,-1);
  
  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);
      }
  }
  
  
  int main( int argc, char** argv )
  {
      cv::CommandLineParser parser(argc, argv, "{@image|fruits.jpg|}");
      help(argv);
  
      string filename = samples::findFile(parser.get<string>("@image"));
      Mat img0 = imread(filename, IMREAD_COLOR);
      if(img0.empty())
      {
          cout << "Couldn't open the image " << filename << ". Usage: inpaint <image_name>\n" << endl;
          return 0;
      }
  
      namedWindow("image", WINDOW_AUTOSIZE);
  
      img = img0.clone();
      inpaintMask = Mat::zeros(img.size(), CV_8U);
  
      imshow("image", img);
      setMouseCallback( "image", onMouse, NULL);
  
      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, INPAINT_TELEA);
              imshow("inpainted image", inpainted);
          }
      }
  
      return 0;
  }