super_resolution.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <ctype.h>
  5. #include "opencv2/core.hpp"
  6. #include "opencv2/core/utility.hpp"
  7. #include "opencv2/highgui.hpp"
  8. #include "opencv2/imgproc.hpp"
  9. #include "opencv2/superres.hpp"
  10. #include "opencv2/superres/optical_flow.hpp"
  11. #include "opencv2/opencv_modules.hpp"
  12. using namespace std;
  13. using namespace cv;
  14. using namespace cv::superres;
  15. #define MEASURE_TIME(op) \
  16. { \
  17. TickMeter tm; \
  18. tm.start(); \
  19. op; \
  20. tm.stop(); \
  21. cout << tm.getTimeSec() << " sec" << endl; \
  22. }
  23. static Ptr<cv::superres::DenseOpticalFlowExt> createOptFlow(const string& name, bool useGpu)
  24. {
  25. if (name == "farneback")
  26. {
  27. if (useGpu)
  28. return cv::superres::createOptFlow_Farneback_CUDA();
  29. else
  30. return cv::superres::createOptFlow_Farneback();
  31. }
  32. /*else if (name == "simple")
  33. return createOptFlow_Simple();*/
  34. else if (name == "tvl1")
  35. {
  36. if (useGpu)
  37. return cv::superres::createOptFlow_DualTVL1_CUDA();
  38. else
  39. return cv::superres::createOptFlow_DualTVL1();
  40. }
  41. else if (name == "brox")
  42. return cv::superres::createOptFlow_Brox_CUDA();
  43. else if (name == "pyrlk")
  44. return cv::superres::createOptFlow_PyrLK_CUDA();
  45. else
  46. cerr << "Incorrect Optical Flow algorithm - " << name << endl;
  47. return Ptr<cv::superres::DenseOpticalFlowExt>();
  48. }
  49. int main(int argc, const char* argv[])
  50. {
  51. CommandLineParser cmd(argc, argv,
  52. "{ v video | | Input video (mandatory)}"
  53. "{ o output | | Output video }"
  54. "{ s scale | 4 | Scale factor }"
  55. "{ i iterations | 180 | Iteration count }"
  56. "{ t temporal | 4 | Radius of the temporal search area }"
  57. "{ f flow | farneback | Optical flow algorithm (farneback, tvl1, brox, pyrlk) }"
  58. "{ g gpu | false | CPU as default device, cuda for CUDA }"
  59. "{ h help | false | Print help message }"
  60. );
  61. const string inputVideoName = cmd.get<string>("video");
  62. if (cmd.get<bool>("help") || inputVideoName.empty())
  63. {
  64. cout << "This sample demonstrates Super Resolution algorithms for video sequence" << endl;
  65. cmd.printMessage();
  66. return EXIT_SUCCESS;
  67. }
  68. const string outputVideoName = cmd.get<string>("output");
  69. const int scale = cmd.get<int>("scale");
  70. const int iterations = cmd.get<int>("iterations");
  71. const int temporalAreaRadius = cmd.get<int>("temporal");
  72. const string optFlow = cmd.get<string>("flow");
  73. string gpuOption = cmd.get<string>("gpu");
  74. std::transform(gpuOption.begin(), gpuOption.end(), gpuOption.begin(), ::tolower);
  75. bool useCuda = gpuOption.compare("cuda") == 0;
  76. Ptr<SuperResolution> superRes;
  77. if (useCuda)
  78. superRes = createSuperResolution_BTVL1_CUDA();
  79. else
  80. superRes = createSuperResolution_BTVL1();
  81. Ptr<cv::superres::DenseOpticalFlowExt> of = createOptFlow(optFlow, useCuda);
  82. if (of.empty())
  83. return EXIT_FAILURE;
  84. superRes->setOpticalFlow(of);
  85. superRes->setScale(scale);
  86. superRes->setIterations(iterations);
  87. superRes->setTemporalAreaRadius(temporalAreaRadius);
  88. Ptr<FrameSource> frameSource;
  89. if (useCuda)
  90. {
  91. // Try to use gpu Video Decoding
  92. try
  93. {
  94. frameSource = createFrameSource_Video_CUDA(inputVideoName);
  95. Mat frame;
  96. frameSource->nextFrame(frame);
  97. }
  98. catch (const cv::Exception&)
  99. {
  100. frameSource.release();
  101. }
  102. }
  103. if (!frameSource)
  104. frameSource = createFrameSource_Video(inputVideoName);
  105. // skip first frame, it is usually corrupted
  106. {
  107. Mat frame;
  108. frameSource->nextFrame(frame);
  109. cout << "Input : " << inputVideoName << " " << frame.size() << endl;
  110. cout << "Scale factor : " << scale << endl;
  111. cout << "Iterations : " << iterations << endl;
  112. cout << "Temporal radius : " << temporalAreaRadius << endl;
  113. cout << "Optical Flow : " << optFlow << endl;
  114. cout << "Mode : " << (useCuda ? "CUDA" : "CPU") << endl;
  115. }
  116. superRes->setInput(frameSource);
  117. VideoWriter writer;
  118. for (int i = 0;; ++i)
  119. {
  120. cout << '[' << setw(3) << i << "] : " << flush;
  121. Mat result;
  122. MEASURE_TIME(superRes->nextFrame(result));
  123. if (result.empty())
  124. break;
  125. imshow("Super Resolution", result);
  126. if (waitKey(1000) > 0)
  127. break;
  128. if (!outputVideoName.empty())
  129. {
  130. if (!writer.isOpened())
  131. writer.open(outputVideoName, VideoWriter::fourcc('X', 'V', 'I', 'D'), 25.0, result.size());
  132. writer << result;
  133. }
  134. }
  135. return 0;
  136. }