6 #include "opencv2/core.hpp"
7 #include "opencv2/core/utility.hpp"
8 #include "opencv2/highgui.hpp"
9 #include "opencv2/imgproc.hpp"
10 #include "opencv2/superres.hpp"
11 #include "opencv2/superres/optical_flow.hpp"
12 #include "opencv2/opencv_modules.hpp"
14 #include "tick_meter.hpp"
18 using namespace cv::superres;
20 #define MEASURE_TIME(op) \
26 cout << tm.getTimeSec() << " sec" << endl; \
29 static Ptr<DenseOpticalFlowExt> createOptFlow(const string& name, bool useGpu)
31 if (name == "farneback")
34 return createOptFlow_Farneback_CUDA();
36 return createOptFlow_Farneback();
38 /*else if (name == "simple")
39 return createOptFlow_Simple();*/
40 else if (name == "tvl1")
43 return createOptFlow_DualTVL1_CUDA();
45 return createOptFlow_DualTVL1();
47 else if (name == "brox")
48 return createOptFlow_Brox_CUDA();
49 else if (name == "pyrlk")
50 return createOptFlow_PyrLK_CUDA();
52 cerr << "Incorrect Optical Flow algorithm - " << name << endl;
54 return Ptr<DenseOpticalFlowExt>();
57 int main(int argc, const char* argv[])
59 CommandLineParser cmd(argc, argv,
60 "{ v video | | Input video }"
61 "{ o output | | Output video }"
62 "{ s scale | 4 | Scale factor }"
63 "{ i iterations | 180 | Iteration count }"
64 "{ t temporal | 4 | Radius of the temporal search area }"
65 "{ f flow | farneback | Optical flow algorithm (farneback, simple, tvl1, brox, pyrlk) }"
66 "{ g | false | CPU as default device, cuda for CUDA }"
67 "{ h help | false | Print help message }"
70 if (cmd.get<bool>("help"))
72 cout << "This sample demonstrates Super Resolution algorithms for video sequence" << endl;
77 const string inputVideoName = cmd.get<string>("video");
78 const string outputVideoName = cmd.get<string>("output");
79 const int scale = cmd.get<int>("scale");
80 const int iterations = cmd.get<int>("iterations");
81 const int temporalAreaRadius = cmd.get<int>("temporal");
82 const string optFlow = cmd.get<string>("flow");
83 string gpuOption = cmd.get<string>("gpu");
85 std::transform(gpuOption.begin(), gpuOption.end(), gpuOption.begin(), ::tolower);
87 bool useCuda = gpuOption.compare("cuda") == 0;
88 Ptr<SuperResolution> superRes;
91 superRes = createSuperResolution_BTVL1_CUDA();
93 superRes = createSuperResolution_BTVL1();
95 Ptr<DenseOpticalFlowExt> of = createOptFlow(optFlow, useCuda);
99 superRes->set("opticalFlow", of);
101 superRes->set("scale", scale);
102 superRes->set("iterations", iterations);
103 superRes->set("temporalAreaRadius", temporalAreaRadius);
105 Ptr<FrameSource> frameSource;
108 // Try to use gpu Video Decoding
111 frameSource = createFrameSource_Video_CUDA(inputVideoName);
113 frameSource->nextFrame(frame);
115 catch (const cv::Exception&)
117 frameSource.release();
121 frameSource = createFrameSource_Video(inputVideoName);
123 // skip first frame, it is usually corrupted
126 frameSource->nextFrame(frame);
127 cout << "Input : " << inputVideoName << " " << frame.size() << endl;
128 cout << "Scale factor : " << scale << endl;
129 cout << "Iterations : " << iterations << endl;
130 cout << "Temporal radius : " << temporalAreaRadius << endl;
131 cout << "Optical Flow : " << optFlow << endl;
132 cout << "Mode : " << (useCuda ? "CUDA" : "CPU") << endl;
135 superRes->setInput(frameSource);
139 for (int i = 0;; ++i)
141 cout << '[' << setw(3) << i << "] : ";
144 MEASURE_TIME(superRes->nextFrame(result));
149 imshow("Super Resolution", result);
151 if (waitKey(1000) > 0)
154 if (!outputVideoName.empty())
156 if (!writer.isOpened())
157 writer.open(outputVideoName, VideoWriter::fourcc('X', 'V', 'I', 'D'), 25.0, result.size());