inpaint.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // Declare Mat objects for original image and mask for inpainting
  9. Mat img, inpaintMask;
  10. // Mat object for result output
  11. Mat res;
  12. Point prevPt(-1,-1);
  13. // onMouse function for Mouse Handling
  14. // Used to draw regions required to inpaint
  15. static void onMouse( int event, int x, int y, int flags, void* )
  16. {
  17. if( event == EVENT_LBUTTONUP || !(flags & EVENT_FLAG_LBUTTON) )
  18. prevPt = Point(-1,-1);
  19. else if( event == EVENT_LBUTTONDOWN )
  20. prevPt = Point(x,y);
  21. else if( event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON) )
  22. {
  23. Point pt(x,y);
  24. if( prevPt.x < 0 )
  25. prevPt = pt;
  26. line( inpaintMask, prevPt, pt, Scalar::all(255), 5, 8, 0 );
  27. line( img, prevPt, pt, Scalar::all(255), 5, 8, 0 );
  28. prevPt = pt;
  29. imshow("image", img);
  30. imshow("image: mask", inpaintMask);
  31. }
  32. }
  33. int main( int argc, char** argv )
  34. {
  35. cout << "Usage: ./inpaint <image_path>" << endl;
  36. cout << "Keys: " << endl;
  37. cout << "t - inpaint using FMM" << endl;
  38. cout << "n - inpaint using NS technique" << endl;
  39. cout << "r - reset the inpainting mask" << endl;
  40. cout << "ESC - exit" << endl;
  41. string filename;
  42. if(argc > 1)
  43. filename = argv[1];
  44. else
  45. filename = "sample.jpeg";
  46. // Read image in color mode
  47. img = imread(filename, IMREAD_COLOR);
  48. Mat img_mask;
  49. // Return error if image not read properly
  50. if(img.empty())
  51. {
  52. cout << "Failed to load image: " << filename << endl;
  53. return 0;
  54. }
  55. namedWindow("image", WINDOW_AUTOSIZE);
  56. // Create a copy for the original image
  57. img_mask = img.clone();
  58. // Initialize mask (black image)
  59. inpaintMask = Mat::zeros(img_mask.size(), CV_8U);
  60. // Show the original image
  61. imshow("image", img);
  62. setMouseCallback( "image", onMouse, NULL);
  63. for(;;)
  64. {
  65. char c = (char)waitKey();
  66. if (c == 't') {
  67. // Use Algorithm proposed by Alexendra Telea
  68. inpaint(img, inpaintMask, res, 3, INPAINT_TELEA);
  69. imshow("Inpaint Output using FMM", res);
  70. }
  71. if (c == 'n') {
  72. // Use Algorithm proposed by Bertalmio et. al.
  73. inpaint(img, inpaintMask, res, 3, INPAINT_NS);
  74. imshow("Inpaint Output using NS Technique", res);
  75. }
  76. if (c == 'r') {
  77. inpaintMask = Scalar::all(0);
  78. img_mask.copyTo(img);
  79. imshow("image", inpaintMask);
  80. }
  81. if ( c == 27 )
  82. break;
  83. }
  84. return 0;
  85. }