#include #include #include #include #include "opencv2/core/core.hpp" #include "opencv2/video/video.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/videostab/videostab.hpp" using namespace std; using namespace cv; using namespace cv::videostab; Ptr stabilizer; double outputFps; string outputPath; void run(); void printHelp(); void run() { VideoWriter writer; Mat stabilizedFrame; while (!(stabilizedFrame = stabilizer->nextFrame()).empty()) { if (!outputPath.empty()) { if (!writer.isOpened()) writer.open(outputPath, CV_FOURCC('X','V','I','D'), outputFps, stabilizedFrame.size()); writer << stabilizedFrame; } imshow("stabilizedFrame", stabilizedFrame); char key = static_cast(waitKey(3)); if (key == 27) break; } cout << "\nfinished\n"; } void printHelp() { cout << "OpenCV video stabilizer.\n" "Usage: videostab [arguments]\n\n" "Arguments:\n" " -m, --model=(transl|transl_and_scale|affine)\n" " Set motion model. The default is affine.\n" " --outlier-ratio=\n" " Outliers ratio in motion estimation. The default is 0.5.\n" " --min-inlier-ratio=\n" " Minimum inlier ratio to decide if estimated motion is OK. The default is 0.1,\n" " but you may want to increase it.\n" " -r, --radius=\n" " Set smoothing radius. The default is 15.\n" " --stdev=\n" " Set smoothing weights standard deviation. The default is sqrt(radius).\n" " --deblur=(yes|no)\n" " Do deblurring.\n" " --deblur-sens=\n" " Set deblurring sensitivity (from 0 to +inf). The default is 0.1.\n" " -t, --trim-ratio=\n" " Set trimming ratio (from 0 to 0.5). The default is 0.\n" " --est-trim=(yes|no)\n" " Estimate trim ratio automatically. The default is yes (that leads to two passes,\n" " you can turn it off if you want to use one pass only).\n" " --incl-constr=(yes|no)\n" " Ensure the inclusion constraint is always satisfied. The default is no.\n" " --border-mode=(replicate|const)\n" " Set border extrapolation mode. The default is replicate.\n" " --mosaic=(yes|no)\n" " Do consistent mosaicing. The default is no.\n" " --mosaic-stdev=\n" " Consistent mosaicing stdev threshold. The default is 10.\n" " --motion-inpaint=(yes|no)\n" " Do motion inpainting (requires GPU support). The default is no.\n" " --color-inpaint=(yes|no)\n" " Do color inpainting. The defailt is no.\n" " -o, --output=\n" " Set output file path explicitely. The default is stabilized.avi.\n" " --fps=\n" " Set output video FPS explicitely. By default the source FPS is used.\n" " -h, --help\n" " Print help.\n" "\n"; } int main(int argc, const char **argv) { try { const char *keys = "{ 1 | | | | }" "{ m | model | | }" "{ | min-inlier-ratio | | }" "{ | outlier-ratio | | }" "{ r | radius | | }" "{ | stdev | | }" "{ | deblur | | }" "{ | deblur-sens | | }" "{ | est-trim | | }" "{ t | trim-ratio | | }" "{ | incl-constr | | }" "{ | border-mode | | }" "{ | mosaic | | }" "{ | mosaic-stdev | | }" "{ | motion-inpaint | | }" "{ | color-inpaint | | }" "{ o | output | stabilized.avi | }" "{ | fps | | }" "{ h | help | false | }"; CommandLineParser cmd(argc, argv, keys); // parse command arguments if (cmd.get("help")) { printHelp(); return 0; } stabilizer = new Stabilizer(); string inputPath = cmd.get("1"); if (inputPath.empty()) throw runtime_error("specify video file path"); VideoFileSource *frameSource = new VideoFileSource(inputPath); outputFps = frameSource->fps(); stabilizer->setFrameSource(frameSource); cout << "frame count: " << frameSource->frameCount() << endl; PyrLkRobustMotionEstimator *motionEstimator = new PyrLkRobustMotionEstimator(); if (cmd.get("model") == "transl") motionEstimator->setMotionModel(TRANSLATION); else if (cmd.get("model") == "transl_and_scale") motionEstimator->setMotionModel(TRANSLATION_AND_SCALE); else if (cmd.get("model") == "affine") motionEstimator->setMotionModel(AFFINE); else if (!cmd.get("model").empty()) throw runtime_error("unknow motion mode: " + cmd.get("model")); if (!cmd.get("outlier-ratio").empty()) { RansacParams ransacParams = motionEstimator->ransacParams(); ransacParams.eps = atof(cmd.get("outlier-ratio").c_str()); motionEstimator->setRansacParams(ransacParams); } if (!cmd.get("min-inlier-ratio").empty()) motionEstimator->setMinInlierRatio(atof(cmd.get("min-inlier-ratio").c_str())); stabilizer->setMotionEstimator(motionEstimator); int smoothRadius = -1; float smoothStdev = -1; if (!cmd.get("radius").empty()) smoothRadius = atoi(cmd.get("radius").c_str()); if (!cmd.get("stdev").empty()) smoothStdev = atof(cmd.get("stdev").c_str()); if (smoothRadius > 0 && smoothStdev > 0) stabilizer->setMotionFilter(new GaussianMotionFilter(smoothRadius, smoothStdev)); else if (smoothRadius > 0 && smoothStdev < 0) stabilizer->setMotionFilter(new GaussianMotionFilter(smoothRadius, sqrt(smoothRadius))); if (cmd.get("deblur") == "yes") { WeightingDeblurer *deblurer = new WeightingDeblurer(); if (!cmd.get("deblur-sens").empty()) deblurer->setSensitivity(atof(cmd.get("deblur-sens").c_str())); stabilizer->setDeblurer(deblurer); } if (!cmd.get("est-trim").empty()) stabilizer->setEstimateTrimRatio(cmd.get("est-trim") == "yes"); if (!cmd.get("trim-ratio").empty()) stabilizer->setTrimRatio(atof(cmd.get("trim-ratio").c_str())); stabilizer->setInclusionConstraint(cmd.get("incl-constr") == "yes"); if (cmd.get("border-mode") == "replicate") stabilizer->setBorderMode(BORDER_REPLICATE); else if (cmd.get("border-mode") == "const") stabilizer->setBorderMode(BORDER_CONSTANT); else if (!cmd.get("border-mode").empty()) throw runtime_error("unknown border extrapolation mode: " + cmd.get("border-mode")); InpaintingPipeline *inpainters = new InpaintingPipeline(); if (cmd.get("mosaic") == "yes") { ConsistentMosaicInpainter *inpainter = new ConsistentMosaicInpainter(); if (!cmd.get("mosaic-stdev").empty()) inpainter->setStdevThresh(atof(cmd.get("mosaic-stdev").c_str())); inpainters->pushBack(inpainter); } if (cmd.get("motion-inpaint") == "yes") inpainters->pushBack(new MotionInpainter()); if (cmd.get("color-inpaint") == "yes") inpainters->pushBack(new ColorAverageInpainter()); if (!inpainters->empty()) stabilizer->setInpainter(inpainters); stabilizer->setLog(new LogToStdout()); outputPath = cmd.get("output"); if (!cmd.get("fps").empty()) outputFps = atoi(cmd.get("fps").c_str()); // run video processing run(); } catch (const exception &e) { cout << e.what() << endl; stabilizer.release(); return -1; } stabilizer.release(); return 0; }