2012-03-19 14:39:23 +01:00
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
2012-03-22 10:30:28 +01:00
|
|
|
#include <fstream>
|
2012-03-19 14:39:23 +01:00
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#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;
|
|
|
|
|
2012-03-22 10:30:28 +01:00
|
|
|
|
2012-03-22 08:52:17 +01:00
|
|
|
Ptr<IFrameSource> stabilizedFrames;
|
2012-03-22 10:30:28 +01:00
|
|
|
string saveMotionsPath;
|
2012-03-19 14:39:23 +01:00
|
|
|
double outputFps;
|
|
|
|
string outputPath;
|
2012-03-20 09:12:58 +01:00
|
|
|
bool quietMode;
|
2012-03-19 14:39:23 +01:00
|
|
|
|
|
|
|
void run();
|
2012-03-22 10:30:28 +01:00
|
|
|
void saveMotionsIfNecessary();
|
2012-03-19 14:39:23 +01:00
|
|
|
void printHelp();
|
|
|
|
|
2012-03-22 10:30:28 +01:00
|
|
|
class GlobalMotionReader : public IGlobalMotionEstimator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GlobalMotionReader(string path)
|
|
|
|
{
|
|
|
|
ifstream f(path.c_str());
|
|
|
|
if (!f.is_open())
|
|
|
|
throw runtime_error("can't open motions file: " + path);
|
|
|
|
int size; f >> size;
|
|
|
|
motions_.resize(size);
|
|
|
|
for (int i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
Mat_<float> M(3, 3);
|
|
|
|
for (int l = 0; l < 3; ++l)
|
|
|
|
for (int s = 0; s < 3; ++s)
|
|
|
|
f >> M(l,s);
|
|
|
|
motions_[i] = M;
|
|
|
|
}
|
|
|
|
pos_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Mat estimate(const Mat &/*frame0*/, const Mat &/*frame1*/)
|
|
|
|
{
|
|
|
|
if (pos_ >= motions_.size())
|
|
|
|
{
|
|
|
|
stringstream text;
|
|
|
|
text << "can't load motion between frames " << pos_ << " and " << pos_+1;
|
|
|
|
throw runtime_error(text.str());
|
|
|
|
}
|
|
|
|
return motions_[pos_++];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
vector<Mat> motions_;
|
|
|
|
size_t pos_;
|
|
|
|
};
|
|
|
|
|
2012-03-19 14:39:23 +01:00
|
|
|
void run()
|
|
|
|
{
|
|
|
|
VideoWriter writer;
|
|
|
|
Mat stabilizedFrame;
|
|
|
|
|
2012-03-22 08:52:17 +01:00
|
|
|
while (!(stabilizedFrame = stabilizedFrames->nextFrame()).empty())
|
2012-03-19 14:39:23 +01:00
|
|
|
{
|
2012-03-22 10:30:28 +01:00
|
|
|
if (!saveMotionsPath.empty())
|
|
|
|
saveMotionsIfNecessary();
|
2012-03-19 14:39:23 +01:00
|
|
|
if (!outputPath.empty())
|
|
|
|
{
|
|
|
|
if (!writer.isOpened())
|
|
|
|
writer.open(outputPath, CV_FOURCC('X','V','I','D'), outputFps, stabilizedFrame.size());
|
|
|
|
writer << stabilizedFrame;
|
|
|
|
}
|
2012-03-20 09:12:58 +01:00
|
|
|
if (!quietMode)
|
|
|
|
{
|
|
|
|
imshow("stabilizedFrame", stabilizedFrame);
|
|
|
|
char key = static_cast<char>(waitKey(3));
|
|
|
|
if (key == 27)
|
|
|
|
break;
|
|
|
|
}
|
2012-03-19 14:39:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cout << "\nfinished\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-22 10:30:28 +01:00
|
|
|
void saveMotionsIfNecessary()
|
|
|
|
{
|
|
|
|
static bool areMotionsSaved = false;
|
|
|
|
if (!areMotionsSaved)
|
|
|
|
{
|
|
|
|
IFrameSource *frameSource = static_cast<IFrameSource*>(stabilizedFrames);
|
|
|
|
TwoPassStabilizer *twoPassStabilizer = dynamic_cast<TwoPassStabilizer*>(frameSource);
|
|
|
|
if (twoPassStabilizer)
|
|
|
|
{
|
|
|
|
ofstream f(saveMotionsPath.c_str());
|
|
|
|
const vector<Mat> &motions = twoPassStabilizer->motions();
|
|
|
|
f << motions.size() << endl;
|
|
|
|
for (size_t i = 0; i < motions.size(); ++i)
|
|
|
|
{
|
|
|
|
Mat_<float> M = motions[i];
|
|
|
|
for (int l = 0, k = 0; l < 3; ++l)
|
|
|
|
for (int s = 0; s < 3; ++s, ++k)
|
|
|
|
f << M(l,s) << " ";
|
|
|
|
f << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
areMotionsSaved = true;
|
|
|
|
cout << "motions are saved";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-19 14:39:23 +01:00
|
|
|
void printHelp()
|
|
|
|
{
|
|
|
|
cout << "OpenCV video stabilizer.\n"
|
|
|
|
"Usage: videostab <file_path> [arguments]\n\n"
|
|
|
|
"Arguments:\n"
|
2012-03-30 11:44:32 +02:00
|
|
|
" -m, --model=(transl|transl_and_scale|linear_sim|affine)\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" Set motion model. The default is affine.\n"
|
|
|
|
" --outlier-ratio=<float_number>\n"
|
|
|
|
" Outliers ratio in motion estimation. The default is 0.5.\n"
|
|
|
|
" --min-inlier-ratio=<float_number>\n"
|
2012-03-20 07:52:52 +01:00
|
|
|
" Minimum inlier ratio to decide if estimated motion is OK. The default is 0.1,\n"
|
2012-03-21 09:34:00 +01:00
|
|
|
" but you may want to increase it.\n\n"
|
2012-03-22 10:30:28 +01:00
|
|
|
" --save-motions=<file_path>\n"
|
|
|
|
" Save estimated motions into file.\n"
|
|
|
|
" --load-motions=<file_path>\n"
|
|
|
|
" Load motions from file.\n\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" -r, --radius=<int_number>\n"
|
|
|
|
" Set smoothing radius. The default is 15.\n"
|
|
|
|
" --stdev=<float_number>\n"
|
2012-03-21 09:34:00 +01:00
|
|
|
" Set smoothing weights standard deviation. The default is sqrt(radius).\n\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" --deblur=(yes|no)\n"
|
|
|
|
" Do deblurring.\n"
|
|
|
|
" --deblur-sens=<float_number>\n"
|
2012-03-21 09:34:00 +01:00
|
|
|
" Set deblurring sensitivity (from 0 to +inf). The default is 0.1.\n\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" -t, --trim-ratio=<float_number>\n"
|
2012-03-21 09:34:00 +01:00
|
|
|
" Set trimming ratio (from 0 to 0.5). The default is 0.0.\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" --est-trim=(yes|no)\n"
|
2012-03-20 07:52:52 +01:00
|
|
|
" 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"
|
2012-03-19 14:39:23 +01:00
|
|
|
" --incl-constr=(yes|no)\n"
|
2012-03-21 09:34:00 +01:00
|
|
|
" Ensure the inclusion constraint is always satisfied. The default is no.\n\n"
|
2012-03-20 13:24:51 +01:00
|
|
|
" --border-mode=(replicate|reflect|const)\n"
|
2012-03-21 09:34:00 +01:00
|
|
|
" Set border extrapolation mode. The default is replicate.\n\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" --mosaic=(yes|no)\n"
|
|
|
|
" Do consistent mosaicing. The default is no.\n"
|
|
|
|
" --mosaic-stdev=<float_number>\n"
|
2012-03-21 09:34:00 +01:00
|
|
|
" Consistent mosaicing stdev threshold. The default is 10.0.\n\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" --motion-inpaint=(yes|no)\n"
|
|
|
|
" Do motion inpainting (requires GPU support). The default is no.\n"
|
2012-03-21 09:34:00 +01:00
|
|
|
" --dist-thresh=<float_number>\n"
|
|
|
|
" Estimated flow distance threshold for motion inpainting. The default is 5.0.\n\n"
|
2012-03-20 13:24:51 +01:00
|
|
|
" --color-inpaint=(no|average|ns|telea)\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" Do color inpainting. The defailt is no.\n"
|
2012-03-20 13:24:51 +01:00
|
|
|
" --color-inpaint-radius=<float_number>\n"
|
2012-03-21 09:34:00 +01:00
|
|
|
" Set color inpainting radius (for ns and telea options only).\n\n"
|
2012-03-22 08:52:17 +01:00
|
|
|
" -o, --output=(no|<file_path>)\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" Set output file path explicitely. The default is stabilized.avi.\n"
|
|
|
|
" --fps=<int_number>\n"
|
|
|
|
" Set output video FPS explicitely. By default the source FPS is used.\n"
|
2012-03-20 09:12:58 +01:00
|
|
|
" -q, --quiet\n"
|
2012-03-21 09:34:00 +01:00
|
|
|
" Don't show output video frames.\n\n"
|
2012-03-19 14:39:23 +01:00
|
|
|
" -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 | | }"
|
2012-03-22 10:30:28 +01:00
|
|
|
"{ | save-motions | | }"
|
|
|
|
"{ | load-motions | | }"
|
2012-03-19 14:39:23 +01:00
|
|
|
"{ r | radius | | }"
|
|
|
|
"{ | stdev | | }"
|
|
|
|
"{ | deblur | | }"
|
|
|
|
"{ | deblur-sens | | }"
|
2012-03-30 11:49:41 +02:00
|
|
|
"{ | est-trim | yes | }"
|
2012-03-19 14:39:23 +01:00
|
|
|
"{ t | trim-ratio | | }"
|
|
|
|
"{ | incl-constr | | }"
|
|
|
|
"{ | border-mode | | }"
|
|
|
|
"{ | mosaic | | }"
|
|
|
|
"{ | mosaic-stdev | | }"
|
|
|
|
"{ | motion-inpaint | | }"
|
2012-03-21 09:34:00 +01:00
|
|
|
"{ | dist-thresh | | }"
|
2012-03-20 13:24:51 +01:00
|
|
|
"{ | color-inpaint | no | }"
|
|
|
|
"{ | color-inpaint-radius | | }"
|
2012-03-19 14:39:23 +01:00
|
|
|
"{ o | output | stabilized.avi | }"
|
|
|
|
"{ | fps | | }"
|
2012-03-20 09:12:58 +01:00
|
|
|
"{ q | quiet | false | }"
|
2012-03-19 14:39:23 +01:00
|
|
|
"{ h | help | false | }";
|
|
|
|
CommandLineParser cmd(argc, argv, keys);
|
|
|
|
|
|
|
|
// parse command arguments
|
|
|
|
|
|
|
|
if (cmd.get<bool>("help"))
|
|
|
|
{
|
|
|
|
printHelp();
|
|
|
|
return 0;
|
2012-03-22 08:52:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
StabilizerBase *stabilizer;
|
|
|
|
GaussianMotionFilter *motionFilter = 0;
|
|
|
|
|
|
|
|
if (!cmd.get<string>("stdev").empty())
|
|
|
|
{
|
|
|
|
motionFilter = new GaussianMotionFilter();
|
|
|
|
motionFilter->setStdev(cmd.get<float>("stdev"));
|
2012-03-19 14:39:23 +01:00
|
|
|
}
|
|
|
|
|
2012-03-22 10:30:28 +01:00
|
|
|
if (!cmd.get<string>("save-motions").empty())
|
|
|
|
saveMotionsPath = cmd.get<string>("save-motions");
|
|
|
|
|
|
|
|
bool isTwoPass =
|
|
|
|
cmd.get<string>("est-trim") == "yes" ||
|
|
|
|
!cmd.get<string>("save-motions").empty();
|
2012-03-22 08:52:17 +01:00
|
|
|
|
|
|
|
if (isTwoPass)
|
|
|
|
{
|
|
|
|
TwoPassStabilizer *twoPassStabilizer = new TwoPassStabilizer();
|
|
|
|
if (!cmd.get<string>("est-trim").empty())
|
|
|
|
twoPassStabilizer->setEstimateTrimRatio(cmd.get<string>("est-trim") == "yes");
|
|
|
|
if (motionFilter)
|
|
|
|
twoPassStabilizer->setMotionStabilizer(motionFilter);
|
|
|
|
stabilizer = twoPassStabilizer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OnePassStabilizer *onePassStabilizer= new OnePassStabilizer();
|
|
|
|
if (motionFilter)
|
|
|
|
onePassStabilizer->setMotionFilter(motionFilter);
|
|
|
|
stabilizer = onePassStabilizer;
|
|
|
|
}
|
2012-03-19 14:39:23 +01:00
|
|
|
|
|
|
|
string inputPath = cmd.get<string>("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<string>("model") == "transl")
|
|
|
|
motionEstimator->setMotionModel(TRANSLATION);
|
|
|
|
else if (cmd.get<string>("model") == "transl_and_scale")
|
|
|
|
motionEstimator->setMotionModel(TRANSLATION_AND_SCALE);
|
2012-03-30 11:44:32 +02:00
|
|
|
else if (cmd.get<string>("model") == "linear_sim")
|
|
|
|
motionEstimator->setMotionModel(LINEAR_SIMILARITY);
|
2012-03-19 14:39:23 +01:00
|
|
|
else if (cmd.get<string>("model") == "affine")
|
|
|
|
motionEstimator->setMotionModel(AFFINE);
|
|
|
|
else if (!cmd.get<string>("model").empty())
|
2012-03-22 10:30:28 +01:00
|
|
|
throw runtime_error("unknow motion mode: " + cmd.get<string>("model"));
|
2012-03-19 14:39:23 +01:00
|
|
|
if (!cmd.get<string>("outlier-ratio").empty())
|
|
|
|
{
|
|
|
|
RansacParams ransacParams = motionEstimator->ransacParams();
|
2012-03-20 08:16:10 +01:00
|
|
|
ransacParams.eps = cmd.get<float>("outlier-ratio");
|
2012-03-19 14:39:23 +01:00
|
|
|
motionEstimator->setRansacParams(ransacParams);
|
|
|
|
}
|
|
|
|
if (!cmd.get<string>("min-inlier-ratio").empty())
|
2012-03-20 08:16:10 +01:00
|
|
|
motionEstimator->setMinInlierRatio(cmd.get<float>("min-inlier-ratio"));
|
2012-03-19 14:39:23 +01:00
|
|
|
stabilizer->setMotionEstimator(motionEstimator);
|
2012-03-22 10:30:28 +01:00
|
|
|
if (!cmd.get<string>("load-motions").empty())
|
|
|
|
stabilizer->setMotionEstimator(new GlobalMotionReader(cmd.get<string>("load-motions")));
|
2012-03-19 14:39:23 +01:00
|
|
|
|
|
|
|
if (!cmd.get<string>("radius").empty())
|
2012-03-22 08:52:17 +01:00
|
|
|
stabilizer->setRadius(cmd.get<int>("radius"));
|
2012-03-19 14:39:23 +01:00
|
|
|
|
|
|
|
if (cmd.get<string>("deblur") == "yes")
|
|
|
|
{
|
|
|
|
WeightingDeblurer *deblurer = new WeightingDeblurer();
|
|
|
|
if (!cmd.get<string>("deblur-sens").empty())
|
2012-03-20 08:16:10 +01:00
|
|
|
deblurer->setSensitivity(cmd.get<float>("deblur-sens"));
|
2012-03-19 14:39:23 +01:00
|
|
|
stabilizer->setDeblurer(deblurer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cmd.get<string>("trim-ratio").empty())
|
2012-03-20 08:16:10 +01:00
|
|
|
stabilizer->setTrimRatio(cmd.get<float>("trim-ratio"));
|
2012-03-19 14:39:23 +01:00
|
|
|
|
2012-03-20 08:30:07 +01:00
|
|
|
if (!cmd.get<string>("incl-constr").empty())
|
2012-03-22 08:52:17 +01:00
|
|
|
stabilizer->setCorrectionForInclusion(cmd.get<string>("incl-constr") == "yes");
|
2012-03-19 14:39:23 +01:00
|
|
|
|
2012-03-20 13:24:51 +01:00
|
|
|
if (cmd.get<string>("border-mode") == "reflect")
|
|
|
|
stabilizer->setBorderMode(BORDER_REFLECT);
|
|
|
|
else if (cmd.get<string>("border-mode") == "replicate")
|
2012-03-19 14:39:23 +01:00
|
|
|
stabilizer->setBorderMode(BORDER_REPLICATE);
|
|
|
|
else if (cmd.get<string>("border-mode") == "const")
|
|
|
|
stabilizer->setBorderMode(BORDER_CONSTANT);
|
|
|
|
else if (!cmd.get<string>("border-mode").empty())
|
|
|
|
throw runtime_error("unknown border extrapolation mode: " + cmd.get<string>("border-mode"));
|
|
|
|
|
|
|
|
InpaintingPipeline *inpainters = new InpaintingPipeline();
|
|
|
|
if (cmd.get<string>("mosaic") == "yes")
|
|
|
|
{
|
|
|
|
ConsistentMosaicInpainter *inpainter = new ConsistentMosaicInpainter();
|
|
|
|
if (!cmd.get<string>("mosaic-stdev").empty())
|
2012-03-20 08:16:10 +01:00
|
|
|
inpainter->setStdevThresh(cmd.get<float>("mosaic-stdev"));
|
2012-03-19 14:39:23 +01:00
|
|
|
inpainters->pushBack(inpainter);
|
|
|
|
}
|
|
|
|
if (cmd.get<string>("motion-inpaint") == "yes")
|
2012-03-21 09:34:00 +01:00
|
|
|
{
|
|
|
|
MotionInpainter *inpainter = new MotionInpainter();
|
|
|
|
if (!cmd.get<string>("dist-thresh").empty())
|
|
|
|
inpainter->setDistThreshold(cmd.get<float>("dist-thresh"));
|
|
|
|
inpainters->pushBack(inpainter);
|
|
|
|
}
|
2012-03-20 13:24:51 +01:00
|
|
|
if (!cmd.get<string>("color-inpaint").empty())
|
|
|
|
{
|
|
|
|
if (cmd.get<string>("color-inpaint") == "average")
|
|
|
|
inpainters->pushBack(new ColorAverageInpainter());
|
|
|
|
else if (!cmd.get<string>("color-inpaint-radius").empty())
|
|
|
|
{
|
|
|
|
float radius = cmd.get<float>("color-inpaint-radius");
|
|
|
|
if (cmd.get<string>("color-inpaint") == "ns")
|
|
|
|
inpainters->pushBack(new ColorInpainter(INPAINT_NS, radius));
|
|
|
|
else if (cmd.get<string>("color-inpaint") == "telea")
|
|
|
|
inpainters->pushBack(new ColorInpainter(INPAINT_TELEA, radius));
|
|
|
|
else if (cmd.get<string>("color-inpaint") != "no")
|
|
|
|
throw runtime_error("unknown color inpainting method: " + cmd.get<string>("color-inpaint"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (cmd.get<string>("color-inpaint") == "ns")
|
|
|
|
inpainters->pushBack(new ColorInpainter(INPAINT_NS));
|
|
|
|
else if (cmd.get<string>("color-inpaint") == "telea")
|
|
|
|
inpainters->pushBack(new ColorInpainter(INPAINT_TELEA));
|
|
|
|
else if (cmd.get<string>("color-inpaint") != "no")
|
|
|
|
throw runtime_error("unknown color inpainting method: " + cmd.get<string>("color-inpaint"));
|
|
|
|
}
|
|
|
|
}
|
2012-03-19 14:39:23 +01:00
|
|
|
if (!inpainters->empty())
|
|
|
|
stabilizer->setInpainter(inpainters);
|
|
|
|
|
|
|
|
stabilizer->setLog(new LogToStdout());
|
|
|
|
|
2012-03-22 08:52:17 +01:00
|
|
|
outputPath = cmd.get<string>("output") != "no" ? cmd.get<string>("output") : "";
|
2012-03-19 14:39:23 +01:00
|
|
|
|
|
|
|
if (!cmd.get<string>("fps").empty())
|
2012-03-20 08:16:10 +01:00
|
|
|
outputFps = cmd.get<double>("fps");
|
2012-03-19 14:39:23 +01:00
|
|
|
|
2012-03-20 09:12:58 +01:00
|
|
|
quietMode = cmd.get<bool>("quiet");
|
|
|
|
|
2012-03-22 08:52:17 +01:00
|
|
|
stabilizedFrames = dynamic_cast<IFrameSource*>(stabilizer);
|
|
|
|
|
2012-03-19 14:39:23 +01:00
|
|
|
run();
|
|
|
|
}
|
|
|
|
catch (const exception &e)
|
|
|
|
{
|
2012-03-21 09:34:00 +01:00
|
|
|
cout << "error: " << e.what() << endl;
|
2012-03-22 08:52:17 +01:00
|
|
|
stabilizedFrames.release();
|
2012-03-19 14:39:23 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2012-03-22 08:52:17 +01:00
|
|
|
stabilizedFrames.release();
|
2012-03-19 14:39:23 +01:00
|
|
|
return 0;
|
|
|
|
}
|