Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions modules/videostab/include/opencv2/videostab/global_motion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,13 @@ class CV_EXPORTS ImageMotionEstimatorBase

virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) = 0;

void setFeatureVisualizationCallback(std::function<void(Mat)> featureVisualizationCallback) {
featureVisualizationCallback_ = std::bind(featureVisualizationCallback, std::placeholders::_1);
};

protected:
ImageMotionEstimatorBase(MotionModel model) { setMotionModel(model); }
std::function<void(Mat)> featureVisualizationCallback_;

private:
MotionModel motionModel_;
Expand Down
24 changes: 24 additions & 0 deletions modules/videostab/samples/videostab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ void printHelp()
" Set output file path explicitly. The default is stabilized.avi.\n"
" --fps=(<float_number>|auto)\n"
" Set output video FPS explicitly. By default the source FPS is used (auto).\n"
" --vo=, --vis-output=(no|<file_path>)\n"
" Set visualization output file path. The default is no.\n"
" -q, --quiet\n"
" Don't show output video frames.\n\n"
" -h, --help\n"
Expand Down Expand Up @@ -335,6 +337,7 @@ int main(int argc, const char **argv)
"{ gpu | no | }"
"{ o output | stabilized.avi | }"
"{ fps | auto | }"
"{ vo vis-output | no | }"
"{ q quiet | | }"
"{ h help | | }";
CommandLineParser cmd(argc, argv, keys);
Expand Down Expand Up @@ -464,6 +467,27 @@ int main(int argc, const char **argv)
// cast stabilizer to simple frame source interface to read stabilized frames
stabilizedFrames.reset(dynamic_cast<IFrameSource*>(stabilizer));

// visWriter and visOutputPath must not be defined inside the if statement
VideoWriter visWriter;
string visOutputPath;
if (arg("vis-output") != "no")
{
visOutputPath = arg("vis-output");
std::function<void(Mat)> featureVisualizationCallback = [&visWriter, &visOutputPath](Mat featureVisualization){
// init visWriter (once) and save visualization frame
if (!featureVisualization.empty())
{
if (!visWriter.isOpened())
visWriter.open(visOutputPath, VideoWriter::fourcc('X','V','I','D'),
outputFps, featureVisualization.size());
visWriter << featureVisualization;
}
};
// must be called before
// "stabilizer->setMotionEstimator(makePtr<ToFileMotionWriter>(arg("save-motions"), stabilizer->motionEstimator()));"
stabilizer->motionEstimator()->setFeatureVisualizationCallback(featureVisualizationCallback);
}

MotionModel model = stabilizer->motionEstimator()->motionModel();
if (arg("load-motions") != "no")
{
Expand Down
21 changes: 21 additions & 0 deletions modules/videostab/src/global_motion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,11 @@ Mat KeypointBasedMotionEstimator::estimate(InputArray frame0, InputArray frame1,
// find keypoints
detector_->detect(frame0, keypointsPrev_);
if (keypointsPrev_.empty())
{
if (featureVisualizationCallback_)
featureVisualizationCallback_(Mat());
return Mat::eye(3, 3, CV_32F);
}

// extract points from keypoints
pointsPrev_.resize(keypointsPrev_.size());
Expand Down Expand Up @@ -777,6 +781,23 @@ Mat KeypointBasedMotionEstimator::estimate(InputArray frame0, InputArray frame1,
}
}

if (featureVisualizationCallback_) {
Mat featureVisualizationPrev;
drawKeypoints(frame0, keypointsPrev_, featureVisualizationPrev,
Scalar(0, 0, 255), DrawMatchesFlags::DEFAULT);

std::vector<KeyPoint> keypointsPrevGood;
keypointsPrevGood.reserve(pointsPrevGood_.size());

for( size_t i = 0; i < pointsPrevGood_.size(); ++i) {
keypointsPrevGood.push_back(KeyPoint(pointsPrevGood_[i], 1.f));
}
drawKeypoints(frame0, keypointsPrevGood, featureVisualizationPrev,
Scalar(0, 255, 0), DrawMatchesFlags::DRAW_OVER_OUTIMG);

featureVisualizationCallback_(featureVisualizationPrev);
}

// estimate motion
return motionEstimator_->estimate(pointsPrevGood_, pointsGood_, ok);
}
Expand Down