Added GPU version of wobble suppressor (videostab)
This commit is contained in:
@@ -48,6 +48,10 @@
|
||||
#include "opencv2/videostab/global_motion.hpp"
|
||||
#include "opencv2/videostab/log.hpp"
|
||||
|
||||
#if HAVE_OPENCV_GPU
|
||||
#include "opencv2/gpu/gpu.hpp"
|
||||
#endif
|
||||
|
||||
namespace cv
|
||||
{
|
||||
namespace videostab
|
||||
@@ -94,21 +98,40 @@ public:
|
||||
virtual void suppress(int idx, const Mat &frame, Mat &result);
|
||||
};
|
||||
|
||||
class CV_EXPORTS MoreAccurateMotionWobbleSuppressor : public WobbleSuppressorBase
|
||||
class CV_EXPORTS MoreAccurateMotionWobbleSuppressorBase : public WobbleSuppressorBase
|
||||
{
|
||||
public:
|
||||
MoreAccurateMotionWobbleSuppressor();
|
||||
MoreAccurateMotionWobbleSuppressorBase() { setPeriod(30); }
|
||||
|
||||
void setPeriod(int val) { period_ = val; }
|
||||
int period() const { return period_; }
|
||||
|
||||
protected:
|
||||
int period_;
|
||||
};
|
||||
|
||||
class CV_EXPORTS MoreAccurateMotionWobbleSuppressor : public MoreAccurateMotionWobbleSuppressorBase
|
||||
{
|
||||
public:
|
||||
virtual void suppress(int idx, const Mat &frame, Mat &result);
|
||||
|
||||
private:
|
||||
int period_;
|
||||
Mat_<float> mapx_, mapy_;
|
||||
};
|
||||
|
||||
#if HAVE_OPENCV_GPU
|
||||
class CV_EXPORTS MoreAccurateMotionWobbleSuppressorGpu : public MoreAccurateMotionWobbleSuppressorBase
|
||||
{
|
||||
public:
|
||||
void suppress(int idx, const gpu::GpuMat &frame, gpu::GpuMat &result);
|
||||
virtual void suppress(int idx, const Mat &frame, Mat &result);
|
||||
|
||||
private:
|
||||
gpu::GpuMat frameDevice_, resultDevice_;
|
||||
gpu::GpuMat mapx_, mapy_;
|
||||
};
|
||||
#endif
|
||||
|
||||
} // namespace videostab
|
||||
} // namespace cv
|
||||
|
||||
|
||||
@@ -56,7 +56,6 @@ WobbleSuppressorBase::WobbleSuppressorBase() : motions_(0), stabilizationMotions
|
||||
PyrLkRobustMotionEstimator *est = new PyrLkRobustMotionEstimator();
|
||||
est->setMotionModel(MM_HOMOGRAPHY);
|
||||
est->setRansacParams(RansacParams::default2dMotion(MM_HOMOGRAPHY));
|
||||
setMotionEstimator(est);
|
||||
}
|
||||
|
||||
|
||||
@@ -66,12 +65,6 @@ void NullWobbleSuppressor::suppress(int /*idx*/, const Mat &frame, Mat &result)
|
||||
}
|
||||
|
||||
|
||||
MoreAccurateMotionWobbleSuppressor::MoreAccurateMotionWobbleSuppressor()
|
||||
{
|
||||
setPeriod(30);
|
||||
}
|
||||
|
||||
|
||||
void MoreAccurateMotionWobbleSuppressor::suppress(int idx, const Mat &frame, Mat &result)
|
||||
{
|
||||
CV_Assert(motions_ && stabilizationMotions_);
|
||||
@@ -123,6 +116,43 @@ void MoreAccurateMotionWobbleSuppressor::suppress(int idx, const Mat &frame, Mat
|
||||
remap(frame, result, mapx_, mapy_, INTER_LINEAR, BORDER_REPLICATE);
|
||||
}
|
||||
|
||||
|
||||
#if HAVE_OPENCV_GPU
|
||||
void MoreAccurateMotionWobbleSuppressorGpu::suppress(int idx, const gpu::GpuMat &frame, gpu::GpuMat &result)
|
||||
{
|
||||
CV_Assert(motions_ && stabilizationMotions_);
|
||||
|
||||
if (idx % period_ == 0)
|
||||
{
|
||||
result = frame;
|
||||
return;
|
||||
}
|
||||
|
||||
int k1 = idx / period_ * period_;
|
||||
int k2 = std::min(k1 + period_, frameCount_ - 1);
|
||||
|
||||
Mat S1 = (*stabilizationMotions_)[idx];
|
||||
|
||||
Mat ML = S1 * getMotion(k1, idx, *motions2_) * getMotion(k1, idx, *motions_).inv() * S1.inv();
|
||||
Mat MR = S1 * getMotion(idx, k2, *motions2_).inv() * getMotion(idx, k2, *motions_) * S1.inv();
|
||||
|
||||
gpu::calcWobbleSuppressionMaps(k1, idx, k2, frame.size(), ML, MR, mapx_, mapy_);
|
||||
|
||||
if (result.data == frame.data)
|
||||
result = gpu::GpuMat(frame.size(), frame.type());
|
||||
|
||||
gpu::remap(frame, result, mapx_, mapy_, INTER_LINEAR, BORDER_REPLICATE);
|
||||
}
|
||||
|
||||
|
||||
void MoreAccurateMotionWobbleSuppressorGpu::suppress(int idx, const Mat &frame, Mat &result)
|
||||
{
|
||||
frameDevice_.upload(frame);
|
||||
suppress(idx, frameDevice_, resultDevice_);
|
||||
resultDevice_.download(result);
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace videostab
|
||||
} // namespace cv
|
||||
|
||||
|
||||
Reference in New Issue
Block a user