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"
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)
20 using namespace cv::videostab;
22 Ptr<IFrameSource> stabilizedFrames;
23 string saveMotionsPath;
29 void saveMotionsIfNecessary();
32 class GlobalMotionReader : public GlobalMotionEstimatorBase
35 GlobalMotionReader(string path)
37 ifstream f(path.c_str());
39 throw runtime_error("can't open motions file: " + path);
41 motions_.resize(size);
42 for (int i = 0; i < size; ++i)
45 for (int l = 0; l < 3; ++l)
46 for (int s = 0; s < 3; ++s)
53 virtual Mat estimate(const Mat &/*frame0*/, const Mat &/*frame1*/)
55 if (pos_ >= motions_.size())
58 text << "can't load motion between frames " << pos_ << " and " << pos_+1;
59 throw runtime_error(text.str());
61 return motions_[pos_++];
76 while (!(stabilizedFrame = stabilizedFrames->nextFrame()).empty())
79 if (!saveMotionsPath.empty())
80 saveMotionsIfNecessary();
81 if (!outputPath.empty())
83 if (!writer.isOpened())
84 writer.open(outputPath, CV_FOURCC('X','V','I','D'), outputFps, stabilizedFrame.size());
85 writer << stabilizedFrame;
89 imshow("stabilizedFrame", stabilizedFrame);
90 char key = static_cast<char>(waitKey(3));
97 << "processed frames: " << nframes << endl
102 void saveMotionsIfNecessary()
104 static bool areMotionsSaved = false;
105 if (!areMotionsSaved)
107 IFrameSource *frameSource = static_cast<IFrameSource*>(stabilizedFrames);
108 TwoPassStabilizer *twoPassStabilizer = dynamic_cast<TwoPassStabilizer*>(frameSource);
109 if (twoPassStabilizer)
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)
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)
123 areMotionsSaved = true;
124 cout << "motions are saved";
131 cout << "OpenCV video stabilizer.\n"
132 "Usage: videostab <file_path> [arguments]\n\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"
150 " --deblur=(yes|no)\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"
181 " Don't show output video frames.\n\n"
188 int main(int argc, const char **argv)
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 | }"
215 "{ q | quiet | false | }"
216 "{ h | help | false | }";
217 CommandLineParser cmd(argc, argv, keys);
219 // parse command arguments
227 StabilizerBase *stabilizer;
229 bool isTwoPass = arg("est-trim") == "yes" || arg("save-motions") != "no";
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")));
238 twoPassStabilizer->setMotionStabilizer(new GaussianMotionFilter(argi("radius"), argf("stdev")));
242 OnePassStabilizer *onePassStabilizer = new OnePassStabilizer();
243 stabilizer = onePassStabilizer;
244 if (arg("stdev") == "auto")
245 onePassStabilizer->setMotionFilter(new GaussianMotionFilter(argi("radius")));
247 onePassStabilizer->setMotionFilter(new GaussianMotionFilter(argi("radius"), argf("stdev")));
249 stabilizedFrames = dynamic_cast<IFrameSource*>(stabilizer);
251 string inputPath = arg("1");
252 if (inputPath.empty()) throw runtime_error("specify video file path");
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);
259 if (arg("load-motions") == "no")
262 PyrLkRobustMotionEstimator *est = new PyrLkRobustMotionEstimator();
263 Ptr<GlobalMotionEstimatorBase> est_(est);
264 if (arg("model") == "transl")
266 est->setMotionModel(TRANSLATION);
267 ransac = RansacParams::translation2dMotionStd();
269 else if (arg("model") == "transl_and_scale")
271 est->setMotionModel(TRANSLATION_AND_SCALE);
272 ransac = RansacParams::translationAndScale2dMotionStd();
274 else if (arg("model") == "linear_sim")
276 est->setMotionModel(LINEAR_SIMILARITY);
277 ransac = RansacParams::linearSimilarity2dMotionStd();
279 else if (arg("model") == "affine")
281 est->setMotionModel(AFFINE);
282 ransac = RansacParams::affine2dMotionStd();
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_);
292 stabilizer->setMotionEstimator(new GlobalMotionReader(arg("load-motions")));
294 if (arg("save-motions") != "no")
295 saveMotionsPath = arg("save-motions");
297 stabilizer->setRadius(argi("radius"));
298 if (arg("deblur") == "yes")
300 WeightingDeblurer *deblurer = new WeightingDeblurer();
301 deblurer->setRadius(argi("radius"));
302 deblurer->setSensitivity(argf("deblur-sens"));
303 stabilizer->setDeblurer(deblurer);
306 stabilizer->setTrimRatio(argf("trim-ratio"));
307 stabilizer->setCorrectionForInclusion(arg("incl-constr") == "yes");
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);
316 throw runtime_error("unknown border extrapolation mode: "
317 + cmd.get<string>("border-mode"));
319 InpaintingPipeline *inpainters = new InpaintingPipeline();
320 Ptr<InpainterBase> inpainters_(inpainters);
321 if (arg("mosaic") == "yes")
323 ConsistentMosaicInpainter *inp = new ConsistentMosaicInpainter();
324 inp->setStdevThresh(argf("mosaic-stdev"));
325 inpainters->pushBack(inp);
327 if (arg("motion-inpaint") == "yes")
329 MotionInpainter *inp = new MotionInpainter();
330 inp->setDistThreshold(argf("dist-thresh"));
331 inpainters->pushBack(inp);
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())
343 inpainters->setRadius(argi("radius"));
344 stabilizer->setInpainter(inpainters_);
347 stabilizer->setLog(new LogToStdout());
349 if (arg("output") != "no")
350 outputPath = arg("output");
352 quietMode = argb("quite");
356 catch (const exception &e)
358 cout << "error: " << e.what() << endl;
359 stabilizedFrames.release();
362 stabilizedFrames.release();