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"
13 #if defined(HAVE_OPENCV_OCL)
14 #include "opencv2/ocl/ocl.hpp"
19 using namespace cv::superres;
21 #define MEASURE_TIME(op) \
27 cout << tm.getTimeSec() << " sec" << endl; \
30 static Ptr<DenseOpticalFlowExt> createOptFlow(const string& name, bool useGpu)
32 if (name == "farneback")
35 return createOptFlow_Farneback_CUDA();
37 return createOptFlow_Farneback();
39 else if (name == "simple")
40 return createOptFlow_Simple();
41 else if (name == "tvl1")
44 return createOptFlow_DualTVL1_CUDA();
46 return createOptFlow_DualTVL1();
48 else if (name == "brox")
49 return createOptFlow_Brox_CUDA();
50 else if (name == "pyrlk")
51 return createOptFlow_PyrLK_CUDA();
54 cerr << "Incorrect Optical Flow algorithm - " << name << endl;
56 return Ptr<DenseOpticalFlowExt>();
58 #if defined(HAVE_OPENCV_OCL)
59 static Ptr<DenseOpticalFlowExt> createOptFlow(const string& name)
61 if (name == "farneback")
63 return createOptFlow_Farneback_OCL();
65 else if (name == "simple")
68 std::cout<<"simple on OpenCL has not been implemented. Use CPU instead!\n";
69 return createOptFlow_Simple();
71 else if (name == "tvl1")
72 return createOptFlow_DualTVL1_OCL();
73 else if (name == "brox")
75 std::cout<<"brox has not been implemented!\n";
76 return Ptr<DenseOpticalFlowExt>();
78 else if (name == "pyrlk")
79 return createOptFlow_PyrLK_OCL();
82 cerr << "Incorrect Optical Flow algorithm - " << name << endl;
84 return Ptr<DenseOpticalFlowExt>();
87 int main(int argc, const char* argv[])
89 useOclChanged = false;
90 CommandLineParser cmd(argc, argv,
91 "{ v video | | Input video }"
92 "{ o output | | Output video }"
93 "{ s scale | 4 | Scale factor }"
94 "{ i iterations | 180 | Iteration count }"
95 "{ t temporal | 4 | Radius of the temporal search area }"
96 "{ f flow | farneback | Optical flow algorithm (farneback, simple, tvl1, brox, pyrlk) }"
97 "{ g | false | CPU as default device, cuda for CUDA and ocl for OpenCL }"
98 "{ h help | false | Print help message }"
101 if (cmd.get<bool>("help"))
103 cout << "This sample demonstrates Super Resolution algorithms for video sequence" << endl;
108 const string inputVideoName = cmd.get<string>("video");
109 const string outputVideoName = cmd.get<string>("output");
110 const int scale = cmd.get<int>("scale");
111 const int iterations = cmd.get<int>("iterations");
112 const int temporalAreaRadius = cmd.get<int>("temporal");
113 const string optFlow = cmd.get<string>("flow");
114 string gpuOption = cmd.get<string>("gpu");
116 std::transform(gpuOption.begin(), gpuOption.end(), gpuOption.begin(), ::tolower);
118 bool useCuda = false;
121 if(gpuOption.compare("ocl") == 0)
123 else if(gpuOption.compare("cuda") == 0)
126 #ifndef HAVE_OPENCV_OCL
130 cout<<"OPENCL is not compiled\n";
135 #if defined(HAVE_OPENCV_OCL)
141 Ptr<SuperResolution> superRes;
144 #if defined(HAVE_OPENCV_OCL)
147 Ptr<DenseOpticalFlowExt> of = createOptFlow(optFlow);
152 superRes = createSuperResolution_BTVL1();
155 superRes = createSuperResolution_BTVL1_OCL();
156 superRes->set("opticalFlow", of);
162 superRes = createSuperResolution_BTVL1_CUDA();
164 superRes = createSuperResolution_BTVL1();
166 Ptr<DenseOpticalFlowExt> of = createOptFlow(optFlow, useCuda);
170 superRes->set("opticalFlow", of);
173 superRes->set("scale", scale);
174 superRes->set("iterations", iterations);
175 superRes->set("temporalAreaRadius", temporalAreaRadius);
177 Ptr<FrameSource> frameSource;
180 // Try to use gpu Video Decoding
183 frameSource = createFrameSource_Video_CUDA(inputVideoName);
185 frameSource->nextFrame(frame);
187 catch (const cv::Exception&)
189 frameSource.release();
193 frameSource = createFrameSource_Video(inputVideoName);
195 // skip first frame, it is usually corrupted
198 frameSource->nextFrame(frame);
199 cout << "Input : " << inputVideoName << " " << frame.size() << endl;
200 cout << "Scale factor : " << scale << endl;
201 cout << "Iterations : " << iterations << endl;
202 cout << "Temporal radius : " << temporalAreaRadius << endl;
203 cout << "Optical Flow : " << optFlow << endl;
204 #if defined(HAVE_OPENCV_OCL)
205 cout << "Mode : " << (useCuda ? "CUDA" : useOcl? "OpenCL" : "CPU") << endl;
207 cout << "Mode : " << (useCuda ? "CUDA" : "CPU") << endl;
211 superRes->setInput(frameSource);
215 for (int i = 0;; ++i)
217 cout << '[' << setw(3) << i << "] : ";
220 #if defined(HAVE_OPENCV_OCL)
221 cv::ocl::oclMat result_;
227 superRes->nextFrame(result_);
234 MEASURE_TIME(superRes->nextFrame(result));
237 #ifdef HAVE_OPENCV_OCL
242 result_.download(result);
249 imshow("Super Resolution", result);
251 if (waitKey(1000) > 0)
254 if (!outputVideoName.empty())
256 if (!writer.isOpened())
257 writer.open(outputVideoName, VideoWriter::fourcc('X', 'V', 'I', 'D'), 25.0, result.size());