Deleted an assignment to a variable that doesn't exist.
[profile/ivi/opencv.git] / samples / gpu / super_resolution.cpp
1 #include <iostream>
2 #include <iomanip>
3 #include <string>
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"
12
13 using namespace std;
14 using namespace cv;
15 using namespace cv::superres;
16
17 #define MEASURE_TIME(op) \
18     { \
19         TickMeter tm; \
20         tm.start(); \
21         op; \
22         tm.stop(); \
23         cout << tm.getTimeSec() << " sec" << endl; \
24     }
25
26 static Ptr<DenseOpticalFlowExt> createOptFlow(const string& name, bool useGpu)
27 {
28     if (name == "farneback")
29     {
30         if (useGpu)
31             return createOptFlow_Farneback_CUDA();
32         else
33             return createOptFlow_Farneback();
34     }
35     else if (name == "simple")
36         return createOptFlow_Simple();
37     else if (name == "tvl1")
38     {
39         if (useGpu)
40             return createOptFlow_DualTVL1_CUDA();
41         else
42             return createOptFlow_DualTVL1();
43     }
44     else if (name == "brox")
45         return createOptFlow_Brox_CUDA();
46     else if (name == "pyrlk")
47         return createOptFlow_PyrLK_CUDA();
48     else
49         cerr << "Incorrect Optical Flow algorithm - " << name << endl;
50
51     return Ptr<DenseOpticalFlowExt>();
52 }
53
54 int main(int argc, const char* argv[])
55 {
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 }"
65     );
66
67     if (cmd.get<bool>("help"))
68     {
69         cout << "This sample demonstrates Super Resolution algorithms for video sequence" << endl;
70         cmd.printMessage();
71         return EXIT_SUCCESS;
72     }
73
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");
81
82     std::transform(gpuOption.begin(), gpuOption.end(), gpuOption.begin(), ::tolower);
83
84     bool useCuda = gpuOption.compare("cuda") == 0;
85     Ptr<SuperResolution> superRes;
86
87     if (useCuda)
88         superRes = createSuperResolution_BTVL1_CUDA();
89     else
90         superRes = createSuperResolution_BTVL1();
91
92     Ptr<DenseOpticalFlowExt> of = createOptFlow(optFlow, useCuda);
93
94     if (of.empty())
95         return EXIT_FAILURE;
96     superRes->set("opticalFlow", of);
97
98     superRes->set("scale", scale);
99     superRes->set("iterations", iterations);
100     superRes->set("temporalAreaRadius", temporalAreaRadius);
101
102     Ptr<FrameSource> frameSource;
103     if (useCuda)
104     {
105         // Try to use gpu Video Decoding
106         try
107         {
108             frameSource = createFrameSource_Video_CUDA(inputVideoName);
109             Mat frame;
110             frameSource->nextFrame(frame);
111         }
112         catch (const cv::Exception&)
113         {
114             frameSource.release();
115         }
116     }
117     if (!frameSource)
118         frameSource = createFrameSource_Video(inputVideoName);
119
120     // skip first frame, it is usually corrupted
121     {
122         Mat frame;
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;
130     }
131
132     superRes->setInput(frameSource);
133
134     VideoWriter writer;
135
136     for (int i = 0;; ++i)
137     {
138         cout << '[' << setw(3) << i << "] : ";
139         Mat result;
140
141         MEASURE_TIME(superRes->nextFrame(result));
142
143         if (result.empty())
144             break;
145
146         imshow("Super Resolution", result);
147
148         if (waitKey(1000) > 0)
149             break;
150
151         if (!outputVideoName.empty())
152         {
153             if (!writer.isOpened())
154                 writer.open(outputVideoName, VideoWriter::fourcc('X', 'V', 'I', 'D'), 25.0, result.size());
155             writer << result;
156         }
157     }
158
159     return 0;
160 }