inpainting.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "opencv2/xphoto.hpp"
  2. #include "opencv2/imgproc.hpp"
  3. #include "opencv2/highgui.hpp"
  4. #include <ctime>
  5. #include <iostream>
  6. const char* keys =
  7. {
  8. "{i || input image name}"
  9. "{m || mask image name}"
  10. "{o || output image name}"
  11. };
  12. int main( int argc, const char** argv )
  13. {
  14. bool printHelp = ( argc == 1 );
  15. printHelp = printHelp || ( argc == 2 && std::string(argv[1]) == "--help" );
  16. printHelp = printHelp || ( argc == 2 && std::string(argv[1]) == "-h" );
  17. if ( printHelp )
  18. {
  19. printf("\nThis sample demonstrates shift-map image inpainting\n"
  20. "Call:\n"
  21. " inpainting -i=<string> -m=<string> [-o=<string>]\n\n");
  22. return 0;
  23. }
  24. cv::CommandLineParser parser(argc, argv, keys);
  25. if ( !parser.check() )
  26. {
  27. parser.printErrors();
  28. return -1;
  29. }
  30. std::string inFilename = parser.get<std::string>("i");
  31. std::string maskFilename = parser.get<std::string>("m");
  32. std::string outFilename = parser.get<std::string>("o");
  33. cv::Mat src = cv::imread(inFilename, cv::IMREAD_UNCHANGED);
  34. if ( src.empty() )
  35. {
  36. printf( "Cannot read image file: %s\n", inFilename.c_str() );
  37. return -1;
  38. }
  39. cv::cvtColor(src, src, cv::COLOR_BGR2Lab);
  40. cv::Mat mask = cv::imread(maskFilename, cv::IMREAD_GRAYSCALE);
  41. if ( mask.empty() )
  42. {
  43. printf( "Cannot read image file: %s\n", maskFilename.c_str() );
  44. return -1;
  45. }
  46. cv::threshold(mask, mask, 128, 255, cv::THRESH_BINARY | cv::THRESH_OTSU);
  47. cv::Mat res(src.size(), src.type());
  48. int time = clock();
  49. cv::xphoto::inpaint( src, mask, res, cv::xphoto::INPAINT_SHIFTMAP );
  50. std::cout << "time = " << (clock() - time)
  51. / double(CLOCKS_PER_SEC) << std::endl;
  52. cv::cvtColor(res, res, cv::COLOR_Lab2BGR);
  53. if ( outFilename == "" )
  54. {
  55. cv::namedWindow("inpainting result", 1);
  56. cv::imshow("inpainting result", res);
  57. cv::waitKey(0);
  58. }
  59. else
  60. cv::imwrite(outFilename, res);
  61. return 0;
  62. }