Refactored videostab module, added base class for woobble suppression
[profile/ivi/opencv.git] / samples / cpp / videostab.cpp
1 #include <string>
2 #include <iostream>
3 #include <fstream>
4 #include <sstream>
5 #include <stdexcept>
6 #include "opencv2/core/core.hpp"
7 #include "opencv2/video/video.hpp"
8 #include "opencv2/imgproc/imgproc.hpp"
9 #include "opencv2/highgui/highgui.hpp"
10 #include "opencv2/videostab/videostab.hpp"
11
12 #define arg(name) cmd.get<string>(name)
13 #define argb(name) cmd.get<bool>(name)
14 #define argi(name) cmd.get<int>(name)
15 #define argf(name) cmd.get<float>(name)
16 #define argd(name) cmd.get<double>(name)
17
18 using namespace std;
19 using namespace cv;
20 using namespace cv::videostab;
21
22 Ptr<IFrameSource> stabilizedFrames;
23 string saveMotionsPath;
24 double outputFps;
25 string outputPath;
26 bool quietMode;
27
28 void run();
29 void saveMotionsIfNecessary();
30 void printHelp();
31
32 class GlobalMotionReader : public GlobalMotionEstimatorBase
33 {
34 public:
35     GlobalMotionReader(string path)
36     {
37         ifstream f(path.c_str());
38         if (!f.is_open())
39             throw runtime_error("can't open motions file: " + path);
40         int size; f >> size;
41         motions_.resize(size);
42         for (int i = 0; i < size; ++i)
43         {
44             Mat_<float> M(3, 3);
45             for (int l = 0; l < 3; ++l)
46                 for (int s = 0; s < 3; ++s)
47                     f >> M(l,s);
48             motions_[i] = M;
49         }
50         pos_ = 0;
51     }
52
53     virtual Mat estimate(const Mat &/*frame0*/, const Mat &/*frame1*/)
54     {
55         if (pos_ >= motions_.size())
56         {
57             stringstream text;
58             text << "can't load motion between frames " << pos_ << " and " << pos_+1;
59             throw runtime_error(text.str());
60         }
61         return motions_[pos_++];
62     }
63
64 private:
65     vector<Mat> motions_;
66     size_t pos_;
67 };
68
69
70 void run()
71 {
72     VideoWriter writer;
73     Mat stabilizedFrame;
74     int nframes = 0;
75
76     while (!(stabilizedFrame = stabilizedFrames->nextFrame()).empty())
77     {
78         nframes++;
79         if (!saveMotionsPath.empty())
80             saveMotionsIfNecessary();
81         if (!outputPath.empty())
82         {
83             if (!writer.isOpened())
84                 writer.open(outputPath, CV_FOURCC('X','V','I','D'), outputFps, stabilizedFrame.size());
85             writer << stabilizedFrame;
86         }
87         if (!quietMode)
88         {
89             imshow("stabilizedFrame", stabilizedFrame);
90             char key = static_cast<char>(waitKey(3));
91             if (key == 27)
92                 break;
93         }
94     }
95
96     cout << endl
97          << "processed frames: " << nframes << endl
98          << "finished\n";
99 }
100
101
102 void saveMotionsIfNecessary()
103 {
104     static bool areMotionsSaved = false;
105     if (!areMotionsSaved)
106     {
107         IFrameSource *frameSource = static_cast<IFrameSource*>(stabilizedFrames);
108         TwoPassStabilizer *twoPassStabilizer = dynamic_cast<TwoPassStabilizer*>(frameSource);
109         if (twoPassStabilizer)
110         {
111             ofstream f(saveMotionsPath.c_str());
112             const vector<Mat> &motions = twoPassStabilizer->motions();
113             f << motions.size() << endl;
114             for (size_t i = 0; i < motions.size(); ++i)
115             {
116                 Mat_<float> M = motions[i];
117                 for (int l = 0, k = 0; l < 3; ++l)
118                     for (int s = 0; s < 3; ++s, ++k)
119                         f << M(l,s) << " ";
120                 f << endl;
121             }
122         }
123         areMotionsSaved = true;
124         cout << "motions are saved";
125     }
126 }
127
128
129 void printHelp()
130 {
131     cout << "OpenCV video stabilizer.\n"
132             "Usage: videostab <file_path> [arguments]\n\n"
133             "Arguments:\n"
134             "  -m, --model=(transl|transl_and_scale|linear_sim|affine)\n"
135             "      Set motion model. The default is affine.\n"
136             "  --outlier-ratio=<float_number>\n"
137             "      Outliers ratio in motion estimation. The default is 0.5.\n"
138             "  --min-inlier-ratio=<float_number>\n"
139             "      Minimum inlier ratio to decide if estimated motion is OK. The default is 0.1,\n"
140             "      but you may want to increase it.\n\n"
141             "  --save-motions=(<file_path>|no)\n"
142             "      Save estimated motions into file. The default is no.\n"
143             "  --load-motions=(<file_path>|no)\n"
144             "      Load motions from file. The default is no.\n\n"
145             "  -r, --radius=<int_number>\n"
146             "      Set sliding window radius. The default is 15.\n"
147             "  --stdev=(<float_number>|auto)\n"
148             "      Set smoothing weights standard deviation. The default is sqrt(radius),\n"
149             "      i.e. auto.\n\n"
150             "  --deblur=(yes|no)\n"
151             "      Do deblurring.\n"
152             "  --deblur-sens=<float_number>\n"
153             "      Set deblurring sensitivity (from 0 to +inf). The default is 0.1.\n\n"
154             "  -t, --trim-ratio=<float_number>\n"
155             "      Set trimming ratio (from 0 to 0.5). The default is 0.1.\n"
156             "  --est-trim=(yes|no)\n"
157             "      Estimate trim ratio automatically. The default is yes (that leads to two passes,\n"
158             "      you can turn it off if you want to use one pass only).\n"
159             "  --incl-constr=(yes|no)\n"
160             "      Ensure the inclusion constraint is always satisfied. The default is no.\n\n"
161             "  --border-mode=(replicate|reflect|const)\n"
162             "      Set border extrapolation mode. The default is replicate.\n\n"
163             "  --mosaic=(yes|no)\n"
164             "      Do consistent mosaicing. The default is no.\n"
165             "  --mosaic-stdev=<float_number>\n"
166             "      Consistent mosaicing stdev threshold. The default is 10.0.\n\n"
167             "  --motion-inpaint=(yes|no)\n"
168             "      Do motion inpainting (requires GPU support). The default is no.\n"
169             "  --dist-thresh=<float_number>\n"
170             "      Estimated flow distance threshold for motion inpainting. The default is 5.0.\n\n"
171             "  --color-inpaint=(no|average|ns|telea)\n"
172             "      Do color inpainting. The defailt is no.\n"
173             "  --color-inpaint-radius=<float_number>\n"
174             "      Set color inpainting radius (for ns and telea options only).\n"
175             "      The default is 2.0\n\n"
176             "  -o, --output=(no|<file_path>)\n"
177             "      Set output file path explicitely. The default is stabilized.avi.\n"
178             "  --fps=(<int_number>|auto)\n"
179             "      Set output video FPS explicitely. By default the source FPS is used.\n"
180             "  -q, --quiet\n"
181             "      Don't show output video frames.\n\n"
182             "  -h, --help\n"
183             "      Print help.\n"
184             "\n";
185 }
186
187
188 int main(int argc, const char **argv)
189 {
190     try
191     {
192         const char *keys =
193                 "{ 1 | | | | }"
194                 "{ m | model | affine| }"
195                 "{ | min-inlier-ratio | 0.1 | }"
196                 "{ | outlier-ratio | 0.5 | }"
197                 "{ | save-motions | no | }"
198                 "{ | load-motions | no | }"
199                 "{ r | radius | 15 | }"
200                 "{ | stdev | auto | }"
201                 "{ | deblur | no | }"
202                 "{ | deblur-sens | 0.1 | }"
203                 "{ | est-trim | yes | }"
204                 "{ t | trim-ratio | 0.1 | }"
205                 "{ | incl-constr | no | }"
206                 "{ | border-mode | replicate | }"
207                 "{ | mosaic | no | }"
208                 "{ | mosaic-stdev | 10.0 | }"
209                 "{ | motion-inpaint | no | }"
210                 "{ | dist-thresh | 5.0 | }"
211                 "{ | color-inpaint | no | }"
212                 "{ | color-inpaint-radius | 2 | }"
213                 "{ o | output | stabilized.avi | }"
214                 "{ | fps | auto | }"
215                 "{ q | quiet | false | }"
216                 "{ h | help | false | }";
217         CommandLineParser cmd(argc, argv, keys);
218
219         // parse command arguments
220
221         if (argb("help"))
222         {
223             printHelp();
224             return 0;
225         }               
226
227         StabilizerBase *stabilizer;
228
229         bool isTwoPass = arg("est-trim") == "yes" || arg("save-motions") != "no";
230         if (isTwoPass)
231         {
232             TwoPassStabilizer *twoPassStabilizer = new TwoPassStabilizer();
233             stabilizer = twoPassStabilizer;
234             twoPassStabilizer->setEstimateTrimRatio(arg("est-trim") == "yes");
235             if (arg("stdev") == "auto")
236                 twoPassStabilizer->setMotionStabilizer(new GaussianMotionFilter(argi("radius")));
237             else
238                 twoPassStabilizer->setMotionStabilizer(new GaussianMotionFilter(argi("radius"), argf("stdev")));
239         }
240         else
241         {
242             OnePassStabilizer *onePassStabilizer = new OnePassStabilizer();
243             stabilizer = onePassStabilizer;
244             if (arg("stdev") == "auto")
245                 onePassStabilizer->setMotionFilter(new GaussianMotionFilter(argi("radius")));
246             else
247                 onePassStabilizer->setMotionFilter(new GaussianMotionFilter(argi("radius"), argf("stdev")));
248         }
249         stabilizedFrames = dynamic_cast<IFrameSource*>(stabilizer);
250
251         string inputPath = arg("1");
252         if (inputPath.empty()) throw runtime_error("specify video file path");
253
254         VideoFileSource *source = new VideoFileSource(inputPath);
255         cout << "frame count (rough): " << source->count() << endl;
256         if (arg("fps") == "auto") outputFps = source->fps();  else outputFps = argd("fps");
257         stabilizer->setFrameSource(source);
258
259         if (arg("load-motions") == "no")
260         {
261             RansacParams ransac;
262             PyrLkRobustMotionEstimator *est = new PyrLkRobustMotionEstimator();
263             Ptr<GlobalMotionEstimatorBase> est_(est);
264             if (arg("model") == "transl")
265             {
266                 est->setMotionModel(TRANSLATION);
267                 ransac = RansacParams::translation2dMotionStd();
268             }
269             else if (arg("model") == "transl_and_scale")
270             {
271                 est->setMotionModel(TRANSLATION_AND_SCALE);
272                 ransac = RansacParams::translationAndScale2dMotionStd();
273             }
274             else if (arg("model") == "linear_sim")
275             {
276                 est->setMotionModel(LINEAR_SIMILARITY);
277                 ransac = RansacParams::linearSimilarity2dMotionStd();
278             }
279             else if (arg("model") == "affine")
280             {
281                 est->setMotionModel(AFFINE);
282                 ransac = RansacParams::affine2dMotionStd();
283             }            
284             else
285                 throw runtime_error("unknown motion model: " + arg("model"));
286             ransac.eps = argf("outlier-ratio");
287             est->setRansacParams(ransac);
288             est->setMinInlierRatio(argf("min-inlier-ratio"));
289             stabilizer->setMotionEstimator(est_);
290         }
291         else
292             stabilizer->setMotionEstimator(new GlobalMotionReader(arg("load-motions")));
293
294         if (arg("save-motions") != "no")
295             saveMotionsPath = arg("save-motions");
296
297         stabilizer->setRadius(argi("radius"));
298         if (arg("deblur") == "yes")
299         {
300             WeightingDeblurer *deblurer = new WeightingDeblurer();
301             deblurer->setRadius(argi("radius"));
302             deblurer->setSensitivity(argf("deblur-sens"));
303             stabilizer->setDeblurer(deblurer);
304         }
305
306         stabilizer->setTrimRatio(argf("trim-ratio"));
307         stabilizer->setCorrectionForInclusion(arg("incl-constr") == "yes");
308
309         if (arg("border-mode") == "reflect")
310             stabilizer->setBorderMode(BORDER_REFLECT);
311         else if (arg("border-mode") == "replicate")
312             stabilizer->setBorderMode(BORDER_REPLICATE);
313         else if (arg("border-mode") == "const")
314             stabilizer->setBorderMode(BORDER_CONSTANT);
315         else
316             throw runtime_error("unknown border extrapolation mode: "
317                                  + cmd.get<string>("border-mode"));
318
319         InpaintingPipeline *inpainters = new InpaintingPipeline();
320         Ptr<InpainterBase> inpainters_(inpainters);
321         if (arg("mosaic") == "yes")
322         {
323             ConsistentMosaicInpainter *inp = new ConsistentMosaicInpainter();
324             inp->setStdevThresh(argf("mosaic-stdev"));
325             inpainters->pushBack(inp);
326         }
327         if (arg("motion-inpaint") == "yes")
328         {
329             MotionInpainter *inp = new MotionInpainter();
330             inp->setDistThreshold(argf("dist-thresh"));
331             inpainters->pushBack(inp);
332         }
333         if (arg("color-inpaint") == "average")
334             inpainters->pushBack(new ColorAverageInpainter());
335         else if (arg("color-inpaint") == "ns")
336             inpainters->pushBack(new ColorInpainter(INPAINT_NS, argd("color-inpaint-radius")));
337         else if (arg("color-inpaint") == "telea")
338             inpainters->pushBack(new ColorInpainter(INPAINT_TELEA, argd("color-inpaint-radius")));
339         else if (arg("color-inpaint") != "no")
340             throw runtime_error("unknown color inpainting method: " + arg("color-inpaint"));
341         if (!inpainters->empty())
342         {
343             inpainters->setRadius(argi("radius"));
344             stabilizer->setInpainter(inpainters_);
345         }
346
347         stabilizer->setLog(new LogToStdout());
348
349         if (arg("output") != "no")
350             outputPath = arg("output");
351
352         quietMode = argb("quite");
353
354         run();
355     }
356     catch (const exception &e)
357     {
358         cout << "error: " << e.what() << endl;
359         stabilizedFrames.release();
360         return -1;
361     }
362     stabilizedFrames.release();
363     return 0;
364 }