bm3d_image_denoising.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "opencv2/xphoto.hpp"
  2. #include "opencv2/highgui.hpp"
  3. const char* keys =
  4. {
  5. "{i || input image name}"
  6. "{o || output image name}"
  7. "{sigma || expected noise standard deviation}"
  8. "{tw |4| template window size}"
  9. "{sw |16| search window size}"
  10. };
  11. int main(int argc, const char** argv)
  12. {
  13. bool printHelp = (argc == 1);
  14. printHelp = printHelp || (argc == 2 && std::string(argv[1]) == "--help");
  15. printHelp = printHelp || (argc == 2 && std::string(argv[1]) == "-h");
  16. if (printHelp)
  17. {
  18. printf("\nThis sample demonstrates BM3D image denoising\n"
  19. "Call:\n"
  20. " bm3d_image_denoising -i=<string> -sigma=<double> -tw=<int> -sw=<int> [-o=<string>]\n\n");
  21. return 0;
  22. }
  23. cv::CommandLineParser parser(argc, argv, keys);
  24. if (!parser.check())
  25. {
  26. parser.printErrors();
  27. return -1;
  28. }
  29. std::string inFilename = parser.get<std::string>("i");
  30. std::string outFilename = parser.get<std::string>("o");
  31. cv::Mat src = cv::imread(inFilename, cv::IMREAD_GRAYSCALE);
  32. if (src.empty())
  33. {
  34. printf("Cannot read image file: %s\n", inFilename.c_str());
  35. return -1;
  36. }
  37. float sigma = parser.get<float>("sigma");
  38. if (sigma == 0.0)
  39. sigma = 15.0;
  40. int templateWindowSize = parser.get<int>("tw");
  41. if (templateWindowSize == 0)
  42. templateWindowSize = 4;
  43. int searchWindowSize = parser.get<int>("sw");
  44. if (searchWindowSize == 0)
  45. searchWindowSize = 16;
  46. cv::Mat res(src.size(), src.type());
  47. cv::xphoto::bm3dDenoising(src, res, sigma, templateWindowSize, searchWindowSize);
  48. if (outFilename.empty())
  49. {
  50. cv::namedWindow("input image", cv::WINDOW_NORMAL);
  51. cv::imshow("input image", src);
  52. cv::namedWindow("denoising result", cv::WINDOW_NORMAL);
  53. cv::imshow("denoising result", res);
  54. cv::waitKey(0);
  55. }
  56. else
  57. {
  58. cv::imwrite(outFilename, res);
  59. }
  60. return 0;
  61. }