dnn_superres.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include <iostream>
  5. #include <opencv2/dnn_superres.hpp>
  6. #include <opencv2/imgproc.hpp>
  7. #include <opencv2/highgui.hpp>
  8. using namespace std;
  9. using namespace cv;
  10. using namespace dnn;
  11. using namespace dnn_superres;
  12. int main(int argc, char *argv[])
  13. {
  14. // Check for valid command line arguments, print usage
  15. // if insufficient arguments were given.
  16. if ( argc < 4 ) {
  17. cout << "usage: Arg 1: image | Path to image" << endl;
  18. cout << "\t Arg 2: algorithm | bilinear, bicubic, edsr, espcn, fsrcnn or lapsrn" << endl;
  19. cout << "\t Arg 3: scale | 2, 3 or 4 \n";
  20. cout << "\t Arg 4: path to model file \n";
  21. return -1;
  22. }
  23. string img_path = string(argv[1]);
  24. string algorithm = string(argv[2]);
  25. int scale = atoi(argv[3]);
  26. string path = "";
  27. if( argc > 4)
  28. path = string(argv[4]);
  29. // Load the image
  30. Mat img = cv::imread(img_path);
  31. Mat original_img(img);
  32. if ( img.empty() )
  33. {
  34. std::cerr << "Couldn't load image: " << img << "\n";
  35. return -2;
  36. }
  37. //Make dnn super resolution instance
  38. DnnSuperResImpl sr;
  39. Mat img_new;
  40. if( algorithm == "bilinear" ){
  41. resize(img, img_new, Size(), scale, scale, 2);
  42. }
  43. else if( algorithm == "bicubic" )
  44. {
  45. resize(img, img_new, Size(), scale, scale, 3);
  46. }
  47. else if( algorithm == "edsr" || algorithm == "espcn" || algorithm == "fsrcnn" || algorithm == "lapsrn" )
  48. {
  49. sr.readModel(path);
  50. sr.setModel(algorithm, scale);
  51. sr.upsample(img, img_new);
  52. }
  53. else{
  54. std::cerr << "Algorithm not recognized. \n";
  55. }
  56. if ( img_new.empty() )
  57. {
  58. std::cerr << "Upsampling failed. \n";
  59. return -3;
  60. }
  61. cout << "Upsampling succeeded. \n";
  62. // Display image
  63. cv::namedWindow("Initial Image", WINDOW_AUTOSIZE);
  64. cv::imshow("Initial Image", img_new);
  65. //cv::imwrite("./saved.jpg", img_new);
  66. cv::waitKey(0);
  67. return 0;
  68. }