From 3323ee9d18f6b24e46691c59209c12da4118eeb4 Mon Sep 17 00:00:00 2001 From: Alexey Spizhevoy Date: Wed, 21 Mar 2012 08:34:00 +0000 Subject: [PATCH] Added distance threshold argument into videostab sample --- .../include/opencv2/videostab/inpainting.hpp | 6 ++++- modules/videostab/src/inpainting.cpp | 16 ++++++++---- samples/cpp/videostab.cpp | 30 ++++++++++++++-------- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/modules/videostab/include/opencv2/videostab/inpainting.hpp b/modules/videostab/include/opencv2/videostab/inpainting.hpp index b45eeb0..b973ba1 100644 --- a/modules/videostab/include/opencv2/videostab/inpainting.hpp +++ b/modules/videostab/include/opencv2/videostab/inpainting.hpp @@ -137,6 +137,9 @@ public: void setFlowErrorThreshold(float val) { flowErrorThreshold_ = val; } float flowErrorThreshold() const { return flowErrorThreshold_; } + void setDistThreshold(float val) { distThresh_ = val; } + float distThresh() const { return distThresh_; } + void setBorderMode(int val) { borderMode_ = val; } int borderMode() const { return borderMode_; } @@ -146,6 +149,7 @@ private: FastMarchingMethod fmm_; Ptr optFlowEstimator_; float flowErrorThreshold_; + float distThresh_; int borderMode_; Mat frame1_, transformedFrame1_; @@ -184,7 +188,7 @@ CV_EXPORTS void calcFlowMask( CV_EXPORTS void completeFrameAccordingToFlow( const Mat &flowMask, const Mat &flowX, const Mat &flowY, const Mat &frame1, const Mat &mask1, - Mat& frame0, Mat &mask0); + float distThresh, Mat& frame0, Mat &mask0); } // namespace videostab } // namespace cv diff --git a/modules/videostab/src/inpainting.cpp b/modules/videostab/src/inpainting.cpp index 76dcf79..7e4a5f3 100644 --- a/modules/videostab/src/inpainting.cpp +++ b/modules/videostab/src/inpainting.cpp @@ -110,7 +110,7 @@ struct Pixel3 ConsistentMosaicInpainter::ConsistentMosaicInpainter() { - setStdevThresh(20); + setStdevThresh(20.f); } @@ -288,6 +288,7 @@ MotionInpainter::MotionInpainter() CV_Error(CV_StsNotImplemented, "Current implementation of MotionInpainter requires GPU"); #endif setFlowErrorThreshold(1e-4f); + setDistThreshold(5.f); setBorderMode(BORDER_REPLICATE); } @@ -353,7 +354,8 @@ void MotionInpainter::inpaint(int idx, Mat &frame, Mat &mask) fmm_.run(flowMask_, body); completeFrameAccordingToFlow( - flowMask_, flowX_, flowY_, transformedFrame1_, transformedMask1_, frame, mask); + flowMask_, flowX_, flowY_, transformedFrame1_, transformedMask1_, distThresh_, + frame, mask); } } @@ -446,7 +448,7 @@ void calcFlowMask( void completeFrameAccordingToFlow( const Mat &flowMask, const Mat &flowX, const Mat &flowY, const Mat &frame1, const Mat &mask1, - Mat &frame0, Mat &mask0) + float distThresh, Mat &frame0, Mat &mask0) { CV_Assert(flowMask.type() == CV_8U); CV_Assert(flowX.type() == CV_32F && flowX.size() == flowMask.size()); @@ -459,6 +461,7 @@ void completeFrameAccordingToFlow( Mat_ flowMask_(flowMask), mask1_(mask1), mask0_(mask0); Mat_ flowX_(flowX), flowY_(flowY); + //int count = 0; for (int y0 = 0; y0 < frame0.rows; ++y0) { for (int x0 = 0; x0 < frame0.cols; ++x0) @@ -468,14 +471,17 @@ void completeFrameAccordingToFlow( int x1 = cvRound(x0 + flowX_(y0,x0)); int y1 = cvRound(y0 + flowY_(y0,x0)); - if (x1 >= 0 && x1 < frame1.cols && y1 >= 0 && y1 < frame1.rows && mask1_(y1,x1)) - { + if (x1 >= 0 && x1 < frame1.cols && y1 >= 0 && y1 < frame1.rows && mask1_(y1,x1) + && sqr(flowX_(y0,x0)) + sqr(flowY_(y0,x0)) < sqr(distThresh)) + { frame0.at >(y0,x0) = frame1.at >(y1,x1); mask0_(y0,x0) = 255; + //count++; } } } } + //cout << count << endl; } } // namespace videostab diff --git a/samples/cpp/videostab.cpp b/samples/cpp/videostab.cpp index 68b6219..807438f 100644 --- a/samples/cpp/videostab.cpp +++ b/samples/cpp/videostab.cpp @@ -57,40 +57,42 @@ void printHelp() " 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" + " but you may want to increase it.\n\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" + " Set smoothing weights standard deviation. The default is sqrt(radius).\n\n" " --deblur=(yes|no)\n" " Do deblurring.\n" " --deblur-sens=\n" - " Set deblurring sensitivity (from 0 to +inf). The default is 0.1.\n" + " Set deblurring sensitivity (from 0 to +inf). The default is 0.1.\n\n" " -t, --trim-ratio=\n" - " Set trimming ratio (from 0 to 0.5). The default is 0.\n" + " Set trimming ratio (from 0 to 0.5). The default is 0.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" + " Ensure the inclusion constraint is always satisfied. The default is no.\n\n" " --border-mode=(replicate|reflect|const)\n" - " Set border extrapolation mode. The default is replicate.\n" + " Set border extrapolation mode. The default is replicate.\n\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" + " Consistent mosaicing stdev threshold. The default is 10.0.\n\n" " --motion-inpaint=(yes|no)\n" " Do motion inpainting (requires GPU support). The default is no.\n" + " --dist-thresh=\n" + " Estimated flow distance threshold for motion inpainting. The default is 5.0.\n\n" " --color-inpaint=(no|average|ns|telea)\n" " Do color inpainting. The defailt is no.\n" " --color-inpaint-radius=\n" - " Set color inpainting radius (for ns and telea options only).\n" + " Set color inpainting radius (for ns and telea options only).\n\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" " -q, --quiet\n" - " Don't show output video frames.\n" + " Don't show output video frames.\n\n" " -h, --help\n" " Print help.\n" "\n"; @@ -117,6 +119,7 @@ int main(int argc, const char **argv) "{ | mosaic | | }" "{ | mosaic-stdev | | }" "{ | motion-inpaint | | }" + "{ | dist-thresh | | }" "{ | color-inpaint | no | }" "{ | color-inpaint-radius | | }" "{ o | output | stabilized.avi | }" @@ -212,7 +215,12 @@ int main(int argc, const char **argv) inpainters->pushBack(inpainter); } if (cmd.get("motion-inpaint") == "yes") - inpainters->pushBack(new MotionInpainter()); + { + MotionInpainter *inpainter = new MotionInpainter(); + if (!cmd.get("dist-thresh").empty()) + inpainter->setDistThreshold(cmd.get("dist-thresh")); + inpainters->pushBack(inpainter); + } if (!cmd.get("color-inpaint").empty()) { if (cmd.get("color-inpaint") == "average") @@ -254,7 +262,7 @@ int main(int argc, const char **argv) } catch (const exception &e) { - cout << e.what() << endl; + cout << "error: " << e.what() << endl; stabilizer.release(); return -1; } -- 2.7.4