dct_image_denoising.cpp 1.7 KB

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