4 #include "opencv2/core.hpp"
5 #include "opencv2/core/utility.hpp"
6 #include "opencv2/highgui.hpp"
7 #include "opencv2/imgproc.hpp"
8 #include "opencv2/contrib.hpp"
9 #include "opencv2/superres.hpp"
10 #include "opencv2/superres/optical_flow.hpp"
11 #include "opencv2/opencv_modules.hpp"
15 using namespace cv::superres;
17 #define MEASURE_TIME(op) \
23 cout << tm.getTimeSec() << " sec" << endl; \
26 static Ptr<DenseOpticalFlowExt> createOptFlow(const string& name, bool useGpu)
28 if (name == "farneback")
31 return createOptFlow_Farneback_CUDA();
33 return createOptFlow_Farneback();
35 else if (name == "simple")
36 return createOptFlow_Simple();
37 else if (name == "tvl1")
40 return createOptFlow_DualTVL1_CUDA();
42 return createOptFlow_DualTVL1();
44 else if (name == "brox")
45 return createOptFlow_Brox_CUDA();
46 else if (name == "pyrlk")
47 return createOptFlow_PyrLK_CUDA();
49 cerr << "Incorrect Optical Flow algorithm - " << name << endl;
51 return Ptr<DenseOpticalFlowExt>();
54 int main(int argc, const char* argv[])
56 CommandLineParser cmd(argc, argv,
57 "{ v video | | Input video }"
58 "{ o output | | Output video }"
59 "{ s scale | 4 | Scale factor }"
60 "{ i iterations | 180 | Iteration count }"
61 "{ t temporal | 4 | Radius of the temporal search area }"
62 "{ f flow | farneback | Optical flow algorithm (farneback, simple, tvl1, brox, pyrlk) }"
63 "{ g | false | CPU as default device, cuda for CUDA }"
64 "{ h help | false | Print help message }"
67 if (cmd.get<bool>("help"))
69 cout << "This sample demonstrates Super Resolution algorithms for video sequence" << endl;
74 const string inputVideoName = cmd.get<string>("video");
75 const string outputVideoName = cmd.get<string>("output");
76 const int scale = cmd.get<int>("scale");
77 const int iterations = cmd.get<int>("iterations");
78 const int temporalAreaRadius = cmd.get<int>("temporal");
79 const string optFlow = cmd.get<string>("flow");
80 string gpuOption = cmd.get<string>("gpu");
82 std::transform(gpuOption.begin(), gpuOption.end(), gpuOption.begin(), ::tolower);
84 bool useCuda = gpuOption.compare("cuda") == 0;
85 Ptr<SuperResolution> superRes;
88 superRes = createSuperResolution_BTVL1_CUDA();
90 superRes = createSuperResolution_BTVL1();
92 Ptr<DenseOpticalFlowExt> of = createOptFlow(optFlow, useCuda);
96 superRes->set("opticalFlow", of);
98 superRes->set("scale", scale);
99 superRes->set("iterations", iterations);
100 superRes->set("temporalAreaRadius", temporalAreaRadius);
102 Ptr<FrameSource> frameSource;
105 // Try to use gpu Video Decoding
108 frameSource = createFrameSource_Video_CUDA(inputVideoName);
110 frameSource->nextFrame(frame);
112 catch (const cv::Exception&)
114 frameSource.release();
118 frameSource = createFrameSource_Video(inputVideoName);
120 // skip first frame, it is usually corrupted
123 frameSource->nextFrame(frame);
124 cout << "Input : " << inputVideoName << " " << frame.size() << endl;
125 cout << "Scale factor : " << scale << endl;
126 cout << "Iterations : " << iterations << endl;
127 cout << "Temporal radius : " << temporalAreaRadius << endl;
128 cout << "Optical Flow : " << optFlow << endl;
129 cout << "Mode : " << (useCuda ? "CUDA" : "CPU") << endl;
132 superRes->setInput(frameSource);
136 for (int i = 0;; ++i)
138 cout << '[' << setw(3) << i << "] : ";
141 MEASURE_TIME(superRes->nextFrame(result));
146 imshow("Super Resolution", result);
148 if (waitKey(1000) > 0)
151 if (!outputVideoName.empty())
153 if (!writer.isOpened())
154 writer.open(outputVideoName, VideoWriter::fourcc('X', 'V', 'I', 'D'), 25.0, result.size());