2012-03-19 22:13:33 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
|
|
|
// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
2012-03-19 21:39:23 +08:00
|
|
|
#include "precomp.hpp"
|
|
|
|
#include "opencv2/videostab/global_motion.hpp"
|
2012-04-04 17:28:47 +08:00
|
|
|
#include "opencv2/videostab/ring_buffer.hpp"
|
2012-04-24 20:23:23 +08:00
|
|
|
#include "opencv2/videostab/outlier_rejection.hpp"
|
2012-04-19 01:00:07 +08:00
|
|
|
#include "opencv2/opencv_modules.hpp"
|
2012-03-19 21:39:23 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace cv
|
|
|
|
{
|
|
|
|
namespace videostab
|
|
|
|
{
|
|
|
|
|
2012-04-11 22:02:10 +08:00
|
|
|
// does isotropic normalization
|
|
|
|
static Mat normalizePoints(int npoints, Point2f *points)
|
|
|
|
{
|
|
|
|
float cx = 0.f, cy = 0.f;
|
|
|
|
for (int i = 0; i < npoints; ++i)
|
|
|
|
{
|
|
|
|
cx += points[i].x;
|
|
|
|
cy += points[i].y;
|
|
|
|
}
|
|
|
|
cx /= npoints;
|
|
|
|
cy /= npoints;
|
|
|
|
|
|
|
|
float d = 0.f;
|
|
|
|
for (int i = 0; i < npoints; ++i)
|
|
|
|
{
|
|
|
|
points[i].x -= cx;
|
|
|
|
points[i].y -= cy;
|
|
|
|
d += sqrt(sqr(points[i].x) + sqr(points[i].y));
|
|
|
|
}
|
|
|
|
d /= npoints;
|
|
|
|
|
|
|
|
float s = sqrt(2.f) / d;
|
|
|
|
for (int i = 0; i < npoints; ++i)
|
|
|
|
{
|
|
|
|
points[i].x *= s;
|
|
|
|
points[i].y *= s;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mat_<float> T = Mat::eye(3, 3, CV_32F);
|
|
|
|
T(0,0) = T(1,1) = s;
|
|
|
|
T(0,2) = -cx*s;
|
|
|
|
T(1,2) = -cy*s;
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-19 21:39:23 +08:00
|
|
|
static Mat estimateGlobMotionLeastSquaresTranslation(
|
2012-04-11 22:02:10 +08:00
|
|
|
int npoints, Point2f *points0, Point2f *points1, float *rmse)
|
2012-03-19 21:39:23 +08:00
|
|
|
{
|
|
|
|
Mat_<float> M = Mat::eye(3, 3, CV_32F);
|
|
|
|
for (int i = 0; i < npoints; ++i)
|
|
|
|
{
|
|
|
|
M(0,2) += points1[i].x - points0[i].x;
|
|
|
|
M(1,2) += points1[i].y - points0[i].y;
|
|
|
|
}
|
|
|
|
M(0,2) /= npoints;
|
|
|
|
M(1,2) /= npoints;
|
2012-04-11 22:02:10 +08:00
|
|
|
|
2012-03-19 21:39:23 +08:00
|
|
|
if (rmse)
|
|
|
|
{
|
|
|
|
*rmse = 0;
|
|
|
|
for (int i = 0; i < npoints; ++i)
|
|
|
|
*rmse += sqr(points1[i].x - points0[i].x - M(0,2)) +
|
|
|
|
sqr(points1[i].y - points0[i].y - M(1,2));
|
|
|
|
*rmse = sqrt(*rmse / npoints);
|
|
|
|
}
|
2012-04-11 22:02:10 +08:00
|
|
|
|
2012-03-19 21:39:23 +08:00
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Mat estimateGlobMotionLeastSquaresTranslationAndScale(
|
2012-04-11 22:02:10 +08:00
|
|
|
int npoints, Point2f *points0, Point2f *points1, float *rmse)
|
2012-03-19 21:39:23 +08:00
|
|
|
{
|
2012-04-11 22:02:10 +08:00
|
|
|
Mat_<float> T0 = normalizePoints(npoints, points0);
|
|
|
|
Mat_<float> T1 = normalizePoints(npoints, points1);
|
|
|
|
|
2012-03-19 21:39:23 +08:00
|
|
|
Mat_<float> A(2*npoints, 3), b(2*npoints, 1);
|
|
|
|
float *a0, *a1;
|
|
|
|
Point2f p0, p1;
|
|
|
|
|
|
|
|
for (int i = 0; i < npoints; ++i)
|
|
|
|
{
|
|
|
|
a0 = A[2*i];
|
|
|
|
a1 = A[2*i+1];
|
|
|
|
p0 = points0[i];
|
|
|
|
p1 = points1[i];
|
|
|
|
a0[0] = p0.x; a0[1] = 1; a0[2] = 0;
|
|
|
|
a1[0] = p0.y; a1[1] = 0; a1[2] = 1;
|
|
|
|
b(2*i,0) = p1.x;
|
|
|
|
b(2*i+1,0) = p1.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mat_<float> sol;
|
2012-04-20 17:02:39 +08:00
|
|
|
solve(A, b, sol, DECOMP_NORMAL | DECOMP_LU);
|
2012-03-19 21:39:23 +08:00
|
|
|
|
|
|
|
if (rmse)
|
2012-03-21 15:53:36 +08:00
|
|
|
*rmse = static_cast<float>(norm(A*sol, b, NORM_L2) / sqrt(static_cast<double>(npoints)));
|
2012-03-19 21:39:23 +08:00
|
|
|
|
|
|
|
Mat_<float> M = Mat::eye(3, 3, CV_32F);
|
|
|
|
M(0,0) = M(1,1) = sol(0,0);
|
|
|
|
M(0,2) = sol(1,0);
|
|
|
|
M(1,2) = sol(2,0);
|
2012-04-11 22:02:10 +08:00
|
|
|
|
|
|
|
return T1.inv() * M * T0;
|
2012-03-19 21:39:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-24 20:23:23 +08:00
|
|
|
static Mat estimateGlobMotionLeastSquaresRigid(
|
|
|
|
int npoints, Point2f *points0, Point2f *points1, float *rmse)
|
|
|
|
{
|
|
|
|
Point2f mean0(0.f, 0.f);
|
|
|
|
Point2f mean1(0.f, 0.f);
|
|
|
|
|
|
|
|
for (int i = 0; i < npoints; ++i)
|
|
|
|
{
|
|
|
|
mean0 += points0[i];
|
|
|
|
mean1 += points1[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
mean0 *= 1.f / npoints;
|
|
|
|
mean1 *= 1.f / npoints;
|
|
|
|
|
|
|
|
Mat_<float> A = Mat::zeros(2, 2, CV_32F);
|
|
|
|
Point2f pt0, pt1;
|
|
|
|
|
|
|
|
for (int i = 0; i < npoints; ++i)
|
|
|
|
{
|
|
|
|
pt0 = points0[i] - mean0;
|
|
|
|
pt1 = points1[i] - mean1;
|
|
|
|
A(0,0) += pt1.x * pt0.x;
|
|
|
|
A(0,1) += pt1.x * pt0.y;
|
|
|
|
A(1,0) += pt1.y * pt0.x;
|
|
|
|
A(1,1) += pt1.y * pt0.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mat_<float> M = Mat::eye(3, 3, CV_32F);
|
|
|
|
|
|
|
|
SVD svd(A);
|
|
|
|
Mat_<float> R = svd.u * svd.vt;
|
|
|
|
Mat tmp(M(Rect(0,0,2,2)));
|
|
|
|
R.copyTo(tmp);
|
|
|
|
|
|
|
|
M(0,2) = mean1.x - R(0,0)*mean0.x - R(0,1)*mean0.y;
|
|
|
|
M(1,2) = mean1.y - R(1,0)*mean0.x - R(1,1)*mean0.y;
|
|
|
|
|
|
|
|
if (rmse)
|
|
|
|
{
|
|
|
|
*rmse = 0;
|
|
|
|
for (int i = 0; i < npoints; ++i)
|
|
|
|
{
|
|
|
|
pt0 = points0[i];
|
|
|
|
pt1 = points1[i];
|
|
|
|
*rmse += sqr(pt1.x - M(0,0)*pt0.x - M(0,1)*pt0.y - M(0,2)) +
|
|
|
|
sqr(pt1.y - M(1,0)*pt0.x - M(1,1)*pt0.y - M(1,2));
|
|
|
|
}
|
|
|
|
*rmse = sqrt(*rmse / npoints);
|
|
|
|
}
|
|
|
|
|
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-19 01:00:07 +08:00
|
|
|
static Mat estimateGlobMotionLeastSquaresSimilarity(
|
2012-04-11 22:02:10 +08:00
|
|
|
int npoints, Point2f *points0, Point2f *points1, float *rmse)
|
2012-03-30 17:44:32 +08:00
|
|
|
{
|
2012-04-11 22:02:10 +08:00
|
|
|
Mat_<float> T0 = normalizePoints(npoints, points0);
|
|
|
|
Mat_<float> T1 = normalizePoints(npoints, points1);
|
|
|
|
|
2012-03-30 17:44:32 +08:00
|
|
|
Mat_<float> A(2*npoints, 4), b(2*npoints, 1);
|
|
|
|
float *a0, *a1;
|
|
|
|
Point2f p0, p1;
|
|
|
|
|
|
|
|
for (int i = 0; i < npoints; ++i)
|
|
|
|
{
|
|
|
|
a0 = A[2*i];
|
|
|
|
a1 = A[2*i+1];
|
|
|
|
p0 = points0[i];
|
|
|
|
p1 = points1[i];
|
2012-04-19 01:00:07 +08:00
|
|
|
a0[0] = p0.x; a0[1] = p0.y; a0[2] = 1; a0[3] = 0;
|
2012-03-30 17:44:32 +08:00
|
|
|
a1[0] = p0.y; a1[1] = -p0.x; a1[2] = 0; a1[3] = 1;
|
|
|
|
b(2*i,0) = p1.x;
|
|
|
|
b(2*i+1,0) = p1.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mat_<float> sol;
|
2012-04-20 17:02:39 +08:00
|
|
|
solve(A, b, sol, DECOMP_NORMAL | DECOMP_LU);
|
2012-03-30 17:44:32 +08:00
|
|
|
|
|
|
|
if (rmse)
|
|
|
|
*rmse = static_cast<float>(norm(A*sol, b, NORM_L2) / sqrt(static_cast<double>(npoints)));
|
|
|
|
|
|
|
|
Mat_<float> M = Mat::eye(3, 3, CV_32F);
|
|
|
|
M(0,0) = M(1,1) = sol(0,0);
|
|
|
|
M(0,1) = sol(1,0);
|
|
|
|
M(1,0) = -sol(1,0);
|
|
|
|
M(0,2) = sol(2,0);
|
|
|
|
M(1,2) = sol(3,0);
|
2012-04-11 22:02:10 +08:00
|
|
|
|
|
|
|
return T1.inv() * M * T0;
|
2012-03-30 17:44:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-19 21:39:23 +08:00
|
|
|
static Mat estimateGlobMotionLeastSquaresAffine(
|
2012-04-11 22:02:10 +08:00
|
|
|
int npoints, Point2f *points0, Point2f *points1, float *rmse)
|
2012-03-19 21:39:23 +08:00
|
|
|
{
|
2012-04-11 22:02:10 +08:00
|
|
|
Mat_<float> T0 = normalizePoints(npoints, points0);
|
|
|
|
Mat_<float> T1 = normalizePoints(npoints, points1);
|
|
|
|
|
2012-03-19 21:39:23 +08:00
|
|
|
Mat_<float> A(2*npoints, 6), b(2*npoints, 1);
|
|
|
|
float *a0, *a1;
|
|
|
|
Point2f p0, p1;
|
|
|
|
|
|
|
|
for (int i = 0; i < npoints; ++i)
|
|
|
|
{
|
|
|
|
a0 = A[2*i];
|
|
|
|
a1 = A[2*i+1];
|
|
|
|
p0 = points0[i];
|
|
|
|
p1 = points1[i];
|
|
|
|
a0[0] = p0.x; a0[1] = p0.y; a0[2] = 1; a0[3] = a0[4] = a0[5] = 0;
|
|
|
|
a1[0] = a1[1] = a1[2] = 0; a1[3] = p0.x; a1[4] = p0.y; a1[5] = 1;
|
|
|
|
b(2*i,0) = p1.x;
|
|
|
|
b(2*i+1,0) = p1.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mat_<float> sol;
|
2012-04-20 17:02:39 +08:00
|
|
|
solve(A, b, sol, DECOMP_NORMAL | DECOMP_LU);
|
2012-03-19 21:39:23 +08:00
|
|
|
|
|
|
|
if (rmse)
|
2012-03-21 15:53:36 +08:00
|
|
|
*rmse = static_cast<float>(norm(A*sol, b, NORM_L2) / sqrt(static_cast<double>(npoints)));
|
2012-03-19 21:39:23 +08:00
|
|
|
|
|
|
|
Mat_<float> M = Mat::eye(3, 3, CV_32F);
|
|
|
|
for (int i = 0, k = 0; i < 2; ++i)
|
|
|
|
for (int j = 0; j < 3; ++j, ++k)
|
|
|
|
M(i,j) = sol(k,0);
|
|
|
|
|
2012-04-11 22:02:10 +08:00
|
|
|
return T1.inv() * M * T0;
|
2012-03-19 21:39:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Mat estimateGlobalMotionLeastSquares(
|
2012-04-11 22:02:10 +08:00
|
|
|
int npoints, Point2f *points0, Point2f *points1, int model, float *rmse)
|
2012-03-19 21:39:23 +08:00
|
|
|
{
|
2012-04-16 14:41:06 +08:00
|
|
|
CV_Assert(model <= MM_AFFINE);
|
2012-03-19 21:39:23 +08:00
|
|
|
|
2012-04-11 22:02:10 +08:00
|
|
|
typedef Mat (*Impl)(int, Point2f*, Point2f*, float*);
|
2012-03-19 21:39:23 +08:00
|
|
|
static Impl impls[] = { estimateGlobMotionLeastSquaresTranslation,
|
|
|
|
estimateGlobMotionLeastSquaresTranslationAndScale,
|
2012-04-24 20:23:23 +08:00
|
|
|
estimateGlobMotionLeastSquaresRigid,
|
2012-04-19 01:00:07 +08:00
|
|
|
estimateGlobMotionLeastSquaresSimilarity,
|
2012-03-19 21:39:23 +08:00
|
|
|
estimateGlobMotionLeastSquaresAffine };
|
|
|
|
|
2012-04-11 22:02:10 +08:00
|
|
|
return impls[model](npoints, points0, points1, rmse);
|
2012-03-19 21:39:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Mat estimateGlobalMotionRobust(
|
2012-04-18 21:23:41 +08:00
|
|
|
int npoints, const Point2f *points0, const Point2f *points1, int model,
|
2012-03-21 15:53:36 +08:00
|
|
|
const RansacParams ¶ms, float *rmse, int *ninliers)
|
2012-03-19 21:39:23 +08:00
|
|
|
{
|
2012-04-16 14:41:06 +08:00
|
|
|
CV_Assert(model <= MM_AFFINE);
|
2012-03-19 21:39:23 +08:00
|
|
|
|
2012-04-24 20:23:23 +08:00
|
|
|
const int niters = params.niters();
|
2012-03-19 21:39:23 +08:00
|
|
|
|
2012-04-11 22:02:10 +08:00
|
|
|
// current hypothesis
|
2012-03-19 21:39:23 +08:00
|
|
|
vector<int> indices(params.size);
|
2012-04-11 22:02:10 +08:00
|
|
|
vector<Point2f> subset0(params.size);
|
|
|
|
vector<Point2f> subset1(params.size);
|
|
|
|
|
|
|
|
// best hypothesis
|
|
|
|
vector<Point2f> subset0best(params.size);
|
|
|
|
vector<Point2f> subset1best(params.size);
|
2012-03-19 21:39:23 +08:00
|
|
|
Mat_<float> bestM;
|
|
|
|
int ninliersMax = -1;
|
2012-04-11 22:02:10 +08:00
|
|
|
|
|
|
|
RNG rng(0);
|
2012-03-19 21:39:23 +08:00
|
|
|
Point2f p0, p1;
|
|
|
|
float x, y;
|
|
|
|
|
|
|
|
for (int iter = 0; iter < niters; ++iter)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < params.size; ++i)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
while (!ok)
|
|
|
|
{
|
|
|
|
ok = true;
|
|
|
|
indices[i] = static_cast<unsigned>(rng) % npoints;
|
|
|
|
for (int j = 0; j < i; ++j)
|
|
|
|
if (indices[i] == indices[j])
|
|
|
|
{ ok = false; break; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < params.size; ++i)
|
|
|
|
{
|
|
|
|
subset0[i] = points0[indices[i]];
|
|
|
|
subset1[i] = points1[indices[i]];
|
|
|
|
}
|
|
|
|
|
2012-04-11 22:02:10 +08:00
|
|
|
Mat_<float> M = estimateGlobalMotionLeastSquares(
|
|
|
|
params.size, &subset0[0], &subset1[0], model, 0);
|
2012-03-19 21:39:23 +08:00
|
|
|
|
|
|
|
int ninliers = 0;
|
|
|
|
for (int i = 0; i < npoints; ++i)
|
|
|
|
{
|
|
|
|
p0 = points0[i]; p1 = points1[i];
|
|
|
|
x = M(0,0)*p0.x + M(0,1)*p0.y + M(0,2);
|
|
|
|
y = M(1,0)*p0.x + M(1,1)*p0.y + M(1,2);
|
|
|
|
if (sqr(x - p1.x) + sqr(y - p1.y) < params.thresh * params.thresh)
|
|
|
|
ninliers++;
|
|
|
|
}
|
|
|
|
if (ninliers >= ninliersMax)
|
|
|
|
{
|
|
|
|
bestM = M;
|
|
|
|
ninliersMax = ninliers;
|
|
|
|
subset0best.swap(subset0);
|
|
|
|
subset1best.swap(subset1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ninliersMax < params.size)
|
2012-04-11 22:02:10 +08:00
|
|
|
// compute RMSE
|
|
|
|
bestM = estimateGlobalMotionLeastSquares(
|
|
|
|
params.size, &subset0best[0], &subset1best[0], model, rmse);
|
2012-03-19 21:39:23 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
subset0.resize(ninliersMax);
|
|
|
|
subset1.resize(ninliersMax);
|
|
|
|
for (int i = 0, j = 0; i < npoints; ++i)
|
|
|
|
{
|
|
|
|
p0 = points0[i]; p1 = points1[i];
|
|
|
|
x = bestM(0,0)*p0.x + bestM(0,1)*p0.y + bestM(0,2);
|
|
|
|
y = bestM(1,0)*p0.x + bestM(1,1)*p0.y + bestM(1,2);
|
|
|
|
if (sqr(x - p1.x) + sqr(y - p1.y) < params.thresh * params.thresh)
|
|
|
|
{
|
|
|
|
subset0[j] = p0;
|
|
|
|
subset1[j] = p1;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
2012-04-11 22:02:10 +08:00
|
|
|
bestM = estimateGlobalMotionLeastSquares(
|
|
|
|
ninliersMax, &subset0[0], &subset1[0], model, rmse);
|
2012-03-19 21:39:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ninliers)
|
|
|
|
*ninliers = ninliersMax;
|
|
|
|
|
|
|
|
return bestM;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-05 21:23:42 +08:00
|
|
|
FromFileMotionReader::FromFileMotionReader(const string &path)
|
2012-04-24 20:23:23 +08:00
|
|
|
: GlobalMotionEstimatorBase(MM_UNKNOWN)
|
2012-04-05 21:23:42 +08:00
|
|
|
{
|
|
|
|
file_.open(path.c_str());
|
|
|
|
CV_Assert(file_.is_open());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-10 18:33:19 +08:00
|
|
|
Mat FromFileMotionReader::estimate(const Mat &/*frame0*/, const Mat &/*frame1*/, bool *ok)
|
2012-04-05 21:23:42 +08:00
|
|
|
{
|
|
|
|
Mat_<float> M(3, 3);
|
2012-04-10 18:33:19 +08:00
|
|
|
bool ok_;
|
2012-04-05 21:23:42 +08:00
|
|
|
file_ >> M(0,0) >> M(0,1) >> M(0,2)
|
|
|
|
>> M(1,0) >> M(1,1) >> M(1,2)
|
2012-04-10 18:33:19 +08:00
|
|
|
>> M(2,0) >> M(2,1) >> M(2,2) >> ok_;
|
|
|
|
if (ok) *ok = ok_;
|
2012-04-05 21:23:42 +08:00
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ToFileMotionWriter::ToFileMotionWriter(const string &path, Ptr<GlobalMotionEstimatorBase> estimator)
|
2012-04-24 20:23:23 +08:00
|
|
|
: GlobalMotionEstimatorBase(estimator->motionModel())
|
2012-04-05 21:23:42 +08:00
|
|
|
{
|
|
|
|
file_.open(path.c_str());
|
|
|
|
CV_Assert(file_.is_open());
|
|
|
|
estimator_ = estimator;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-10 18:33:19 +08:00
|
|
|
Mat ToFileMotionWriter::estimate(const Mat &frame0, const Mat &frame1, bool *ok)
|
2012-04-05 21:23:42 +08:00
|
|
|
{
|
2012-04-10 18:33:19 +08:00
|
|
|
bool ok_;
|
|
|
|
Mat_<float> M = estimator_->estimate(frame0, frame1, &ok_);
|
2012-04-05 21:23:42 +08:00
|
|
|
file_ << M(0,0) << " " << M(0,1) << " " << M(0,2) << " "
|
|
|
|
<< M(1,0) << " " << M(1,1) << " " << M(1,2) << " "
|
2012-04-10 18:33:19 +08:00
|
|
|
<< M(2,0) << " " << M(2,1) << " " << M(2,2) << " " << ok_ << endl;
|
|
|
|
if (ok) *ok = ok_;
|
2012-04-05 21:23:42 +08:00
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-24 20:23:23 +08:00
|
|
|
PyrLkRobustMotionEstimatorBase::PyrLkRobustMotionEstimatorBase(MotionModel model)
|
|
|
|
: GlobalMotionEstimatorBase(model)
|
|
|
|
{
|
|
|
|
setRansacParams(RansacParams::default2dMotion(model));
|
|
|
|
setOutlierRejector(new NullOutlierRejector());
|
|
|
|
setMinInlierRatio(0.1f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-06 16:52:31 +08:00
|
|
|
PyrLkRobustMotionEstimator::PyrLkRobustMotionEstimator(MotionModel model)
|
2012-04-24 20:23:23 +08:00
|
|
|
: PyrLkRobustMotionEstimatorBase(model)
|
2012-03-19 21:39:23 +08:00
|
|
|
{
|
|
|
|
setDetector(new GoodFeaturesToTrackDetector());
|
|
|
|
setOptFlowEstimator(new SparsePyrLkOptFlowEstimator());
|
2012-04-11 18:17:35 +08:00
|
|
|
setGridSize(Size(0,0));
|
2012-03-19 21:39:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-10 18:33:19 +08:00
|
|
|
Mat PyrLkRobustMotionEstimator::estimate(const Mat &frame0, const Mat &frame1, bool *ok)
|
2012-03-19 21:39:23 +08:00
|
|
|
{
|
2012-04-18 21:23:41 +08:00
|
|
|
// find keypoints
|
|
|
|
|
2012-03-19 21:39:23 +08:00
|
|
|
detector_->detect(frame0, keypointsPrev_);
|
|
|
|
|
2012-04-11 18:17:35 +08:00
|
|
|
// add extra keypoints
|
2012-04-18 21:23:41 +08:00
|
|
|
|
2012-04-11 18:17:35 +08:00
|
|
|
if (gridSize_.width > 0 && gridSize_.height > 0)
|
|
|
|
{
|
2012-04-18 21:23:41 +08:00
|
|
|
float dx = static_cast<float>(frame0.cols) / (gridSize_.width + 1);
|
|
|
|
float dy = static_cast<float>(frame0.rows) / (gridSize_.height + 1);
|
2012-04-11 18:17:35 +08:00
|
|
|
for (int x = 0; x < gridSize_.width; ++x)
|
|
|
|
for (int y = 0; y < gridSize_.height; ++y)
|
|
|
|
keypointsPrev_.push_back(KeyPoint((x+1)*dx, (y+1)*dy, 0.f));
|
|
|
|
}
|
|
|
|
|
2012-04-18 21:23:41 +08:00
|
|
|
// extract points from keypoints
|
2012-04-11 18:17:35 +08:00
|
|
|
|
2012-03-19 21:39:23 +08:00
|
|
|
pointsPrev_.resize(keypointsPrev_.size());
|
|
|
|
for (size_t i = 0; i < keypointsPrev_.size(); ++i)
|
|
|
|
pointsPrev_[i] = keypointsPrev_[i].pt;
|
|
|
|
|
2012-04-18 21:23:41 +08:00
|
|
|
// find correspondences
|
|
|
|
|
2012-03-19 21:39:23 +08:00
|
|
|
optFlowEstimator_->run(frame0, frame1, pointsPrev_, points_, status_, noArray());
|
|
|
|
|
2012-04-18 21:23:41 +08:00
|
|
|
// leave good correspondences only
|
|
|
|
|
|
|
|
pointsPrevGood_.clear(); pointsPrevGood_.reserve(points_.size());
|
|
|
|
pointsGood_.clear(); pointsGood_.reserve(points_.size());
|
2012-04-11 22:02:10 +08:00
|
|
|
|
2012-04-18 21:23:41 +08:00
|
|
|
for (size_t i = 0; i < points_.size(); ++i)
|
2012-03-19 21:39:23 +08:00
|
|
|
{
|
|
|
|
if (status_[i])
|
|
|
|
{
|
|
|
|
pointsPrevGood_.push_back(pointsPrev_[i]);
|
|
|
|
pointsGood_.push_back(points_[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-24 20:23:23 +08:00
|
|
|
// perfrom outlier rejection
|
|
|
|
|
|
|
|
IOutlierRejector *outlierRejector = static_cast<IOutlierRejector*>(outlierRejector_);
|
|
|
|
if (!dynamic_cast<NullOutlierRejector*>(outlierRejector))
|
|
|
|
{
|
|
|
|
pointsPrev_.swap(pointsPrevGood_);
|
|
|
|
points_.swap(pointsGood_);
|
|
|
|
|
|
|
|
outlierRejector_->process(frame0.size(), pointsPrev_, points_, status_);
|
|
|
|
|
|
|
|
pointsPrevGood_.clear(); pointsPrevGood_.reserve(points_.size());
|
|
|
|
pointsGood_.clear(); pointsGood_.reserve(points_.size());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < points_.size(); ++i)
|
|
|
|
{
|
|
|
|
if (status_[i])
|
|
|
|
{
|
|
|
|
pointsPrevGood_.push_back(pointsPrev_[i]);
|
|
|
|
pointsGood_.push_back(points_[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-18 21:23:41 +08:00
|
|
|
size_t npoints = pointsGood_.size();
|
|
|
|
|
|
|
|
// find motion
|
|
|
|
|
|
|
|
int ninliers = 0;
|
2012-04-04 19:45:16 +08:00
|
|
|
Mat_<float> M;
|
|
|
|
|
2012-04-16 14:41:06 +08:00
|
|
|
if (motionModel_ != MM_HOMOGRAPHY)
|
2012-04-04 19:45:16 +08:00
|
|
|
M = estimateGlobalMotionRobust(
|
2012-04-18 21:23:41 +08:00
|
|
|
npoints, &pointsPrevGood_[0], &pointsGood_[0], motionModel_,
|
|
|
|
ransacParams_, 0, &ninliers);
|
2012-04-04 19:45:16 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
vector<uchar> mask;
|
2012-04-10 17:07:30 +08:00
|
|
|
M = findHomography(pointsPrevGood_, pointsGood_, mask, CV_RANSAC, ransacParams_.thresh);
|
2012-04-18 21:23:41 +08:00
|
|
|
for (size_t i = 0; i < npoints; ++i)
|
|
|
|
if (mask[i]) ninliers++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we're confident enough in estimated motion
|
|
|
|
|
|
|
|
if (ok) *ok = true;
|
|
|
|
if (static_cast<float>(ninliers) / npoints < minInlierRatio_)
|
|
|
|
{
|
|
|
|
M = Mat::eye(3, 3, CV_32F);
|
|
|
|
if (ok) *ok = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if HAVE_OPENCV_GPU
|
|
|
|
PyrLkRobustMotionEstimatorGpu::PyrLkRobustMotionEstimatorGpu(MotionModel model)
|
2012-04-24 20:23:23 +08:00
|
|
|
: PyrLkRobustMotionEstimatorBase(model)
|
2012-04-18 21:23:41 +08:00
|
|
|
{
|
|
|
|
CV_Assert(gpu::getCudaEnabledDeviceCount() > 0);
|
|
|
|
}
|
|
|
|
|
2012-04-04 19:45:16 +08:00
|
|
|
|
2012-04-18 21:23:41 +08:00
|
|
|
Mat PyrLkRobustMotionEstimatorGpu::estimate(const Mat &frame0, const Mat &frame1, bool *ok)
|
|
|
|
{
|
|
|
|
frame0_.upload(frame0);
|
|
|
|
frame1_.upload(frame1);
|
|
|
|
return estimate(frame0_, frame1_, ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Mat PyrLkRobustMotionEstimatorGpu::estimate(const gpu::GpuMat &frame0, const gpu::GpuMat &frame1, bool *ok)
|
|
|
|
{
|
|
|
|
// convert frame to gray if it's color
|
|
|
|
|
|
|
|
gpu::GpuMat grayFrame0;
|
|
|
|
if (frame0.channels() == 1)
|
|
|
|
grayFrame0 = frame0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gpu::cvtColor(frame0_, grayFrame0_, CV_BGR2GRAY);
|
|
|
|
grayFrame0 = grayFrame0_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find keypoints
|
|
|
|
|
|
|
|
detector_(grayFrame0, pointsPrev_);
|
|
|
|
|
|
|
|
// find correspondences
|
|
|
|
|
|
|
|
optFlowEstimator_.run(frame0, frame1, pointsPrev_, points_, status_);
|
|
|
|
|
|
|
|
// leave good correspondences only
|
|
|
|
|
|
|
|
gpu::compactPoints(pointsPrev_, points_, status_);
|
|
|
|
|
|
|
|
pointsPrev_.download(hostPointsPrev_);
|
|
|
|
points_.download(hostPoints_);
|
|
|
|
|
2012-04-24 20:23:23 +08:00
|
|
|
Point2f *points0 = hostPointsPrev_.ptr<Point2f>();
|
|
|
|
Point2f *points1 = hostPoints_.ptr<Point2f>();
|
2012-04-18 21:23:41 +08:00
|
|
|
int npoints = hostPointsPrev_.cols;
|
|
|
|
|
2012-04-24 20:23:23 +08:00
|
|
|
// perfrom outlier rejection
|
|
|
|
|
|
|
|
IOutlierRejector *outlierRejector = static_cast<IOutlierRejector*>(outlierRejector_);
|
|
|
|
if (!dynamic_cast<NullOutlierRejector*>(outlierRejector))
|
|
|
|
{
|
|
|
|
outlierRejector_->process(frame0.size(), hostPointsPrev_, hostPoints_, rejectionStatus_);
|
|
|
|
|
|
|
|
hostPointsPrevGood_.clear(); hostPointsPrevGood_.reserve(hostPoints_.cols);
|
|
|
|
hostPointsGood_.clear(); hostPointsGood_.reserve(hostPoints_.cols);
|
|
|
|
|
|
|
|
for (int i = 0; i < hostPoints_.cols; ++i)
|
|
|
|
{
|
|
|
|
if (rejectionStatus_[i])
|
|
|
|
{
|
|
|
|
hostPointsPrevGood_.push_back(hostPointsPrev_.at<Point2f>(0,i));
|
|
|
|
hostPointsGood_.push_back(hostPoints_.at<Point2f>(0,i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
points0 = &hostPointsPrevGood_[0];
|
|
|
|
points1 = &hostPointsGood_[0];
|
|
|
|
npoints = static_cast<int>(hostPointsGood_.size());
|
|
|
|
}
|
|
|
|
|
2012-04-18 21:23:41 +08:00
|
|
|
// find motion
|
|
|
|
|
|
|
|
int ninliers = 0;
|
|
|
|
Mat_<float> M;
|
|
|
|
|
|
|
|
if (motionModel_ != MM_HOMOGRAPHY)
|
|
|
|
M = estimateGlobalMotionRobust(
|
2012-04-24 20:23:23 +08:00
|
|
|
npoints, points0, points1, motionModel_, ransacParams_, 0, &ninliers);
|
2012-04-18 21:23:41 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
vector<uchar> mask;
|
2012-04-24 20:23:23 +08:00
|
|
|
M = findHomography(
|
|
|
|
Mat(1, npoints, CV_32FC2, points0), Mat(1, npoints, CV_32FC2, points1),
|
|
|
|
mask, CV_RANSAC, ransacParams_.thresh);
|
2012-04-18 21:23:41 +08:00
|
|
|
for (int i = 0; i < npoints; ++i)
|
2012-04-17 17:12:14 +08:00
|
|
|
if (mask[i]) ninliers++;
|
2012-04-04 19:45:16 +08:00
|
|
|
}
|
2012-03-19 21:39:23 +08:00
|
|
|
|
2012-04-18 21:23:41 +08:00
|
|
|
// check if we're confident enough in estimated motion
|
|
|
|
|
2012-04-10 18:33:19 +08:00
|
|
|
if (ok) *ok = true;
|
2012-04-18 21:23:41 +08:00
|
|
|
if (static_cast<float>(ninliers) / npoints < minInlierRatio_)
|
2012-04-10 18:33:19 +08:00
|
|
|
{
|
2012-03-19 21:39:23 +08:00
|
|
|
M = Mat::eye(3, 3, CV_32F);
|
2012-04-10 18:33:19 +08:00
|
|
|
if (ok) *ok = false;
|
|
|
|
}
|
2012-03-19 21:39:23 +08:00
|
|
|
|
|
|
|
return M;
|
|
|
|
}
|
2012-04-18 21:23:41 +08:00
|
|
|
#endif // #if HAVE_OPENCV_GPU
|
2012-03-19 21:39:23 +08:00
|
|
|
|
|
|
|
|
2012-04-04 17:28:47 +08:00
|
|
|
Mat getMotion(int from, int to, const vector<Mat> &motions)
|
2012-03-19 21:39:23 +08:00
|
|
|
{
|
|
|
|
Mat M = Mat::eye(3, 3, CV_32F);
|
|
|
|
if (to > from)
|
|
|
|
{
|
|
|
|
for (int i = from; i < to; ++i)
|
2012-04-04 17:28:47 +08:00
|
|
|
M = at(i, motions) * M;
|
2012-03-19 21:39:23 +08:00
|
|
|
}
|
|
|
|
else if (from > to)
|
|
|
|
{
|
|
|
|
for (int i = to; i < from; ++i)
|
2012-04-04 17:28:47 +08:00
|
|
|
M = at(i, motions) * M;
|
2012-03-19 21:39:23 +08:00
|
|
|
M = M.inv();
|
|
|
|
}
|
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace videostab
|
|
|
|
} // namespace cv
|
2012-04-24 20:23:23 +08:00
|
|
|
|