1
/*M///////////////////////////////////////////////////////////////////////////////////////
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
11
// For Open Source Computer Vision Library
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
43
#include "precomp.hpp"
48
class ThinPlateSplineShapeTransformerImpl : public ThinPlateSplineShapeTransformer
52
ThinPlateSplineShapeTransformerImpl()
54
regularizationParameter=0;
55
name_ = "ShapeTransformer.TPS";
59
ThinPlateSplineShapeTransformerImpl(double _regularizationParameter)
61
regularizationParameter=_regularizationParameter;
62
name_ = "ShapeTransformer.TPS";
67
~ThinPlateSplineShapeTransformerImpl()
71
//! the main operators
72
virtual void estimateTransformation(InputArray transformingShape, InputArray targetShape, std::vector<DMatch> &matches);
73
virtual float applyTransformation(InputArray inPts, OutputArray output=noArray());
74
virtual void warpImage(InputArray transformingImage, OutputArray output,
75
int flags, int borderMode, const Scalar& borderValue) const;
78
virtual void setRegularizationParameter(double _regularizationParameter) {regularizationParameter=_regularizationParameter;}
79
virtual double getRegularizationParameter() const {return regularizationParameter;}
82
virtual void write(FileStorage& fs) const
85
<< "regularization" << regularizationParameter;
88
virtual void read(const FileNode& fn)
90
CV_Assert( (String)fn["name"] == name_ );
91
regularizationParameter = (int)fn["regularization"];
96
double regularizationParameter;
105
static float distance(Point2f p, Point2f q)
107
Point2f diff = p - q;
108
float norma = diff.x*diff.x + diff.y*diff.y;// - 2*diff.x*diff.y;
109
if (norma<0) norma=0;
110
//else norma = std::sqrt(norma);
111
norma = norma*std::log(norma+FLT_EPSILON);
115
static Point2f _applyTransformation(const Mat &shapeRef, const Point2f point, const Mat &tpsParameters)
118
for (int i=0; i<2; i++)
120
float a1=tpsParameters.at<float>(tpsParameters.rows-3,i);
121
float ax=tpsParameters.at<float>(tpsParameters.rows-2,i);
122
float ay=tpsParameters.at<float>(tpsParameters.rows-1,i);
124
float affine=a1+ax*point.x+ay*point.y;
126
for (int j=0; j<shapeRef.rows; j++)
128
nonrigid+=tpsParameters.at<float>(j,i)*
129
distance(Point2f(shapeRef.at<float>(j,0),shapeRef.at<float>(j,1)),
134
out.x=affine+nonrigid;
138
out.y=affine+nonrigid;
145
void ThinPlateSplineShapeTransformerImpl::warpImage(InputArray transformingImage, OutputArray output,
146
int flags, int borderMode, const Scalar& borderValue) const
148
CV_Assert(tpsComputed==true);
150
Mat theinput = transformingImage.getMat();
151
Mat mapX(theinput.rows, theinput.cols, CV_32FC1);
152
Mat mapY(theinput.rows, theinput.cols, CV_32FC1);
154
for (int row = 0; row < theinput.rows; row++)
156
for (int col = 0; col < theinput.cols; col++)
158
Point2f pt = _applyTransformation(shapeReference, Point2f(float(col), float(row)), tpsParameters);
159
mapX.at<float>(row, col) = pt.x;
160
mapY.at<float>(row, col) = pt.y;
163
remap(transformingImage, output, mapX, mapY, flags, borderMode, borderValue);
166
float ThinPlateSplineShapeTransformerImpl::applyTransformation(InputArray inPts, OutputArray outPts)
168
CV_Assert(tpsComputed);
169
Mat pts1 = inPts.getMat();
170
CV_Assert((pts1.channels()==2) && (pts1.cols>0));
172
//Apply transformation in the complete set of points
173
// Ensambling output //
176
outPts.create(1,pts1.cols, CV_32FC2);
177
Mat outMat = outPts.getMat();
178
for (int i=0; i<pts1.cols; i++)
180
Point2f pt=pts1.at<Point2f>(0,i);
181
outMat.at<Point2f>(0,i)=_applyTransformation(shapeReference, pt, tpsParameters);
185
return transformCost;
188
void ThinPlateSplineShapeTransformerImpl::estimateTransformation(InputArray _pts1, InputArray _pts2,
189
std::vector<DMatch>& _matches )
191
Mat pts1 = _pts1.getMat();
192
Mat pts2 = _pts2.getMat();
193
CV_Assert((pts1.channels()==2) && (pts1.cols>0) && (pts2.channels()==2) && (pts2.cols>0));
194
CV_Assert(_matches.size()>1);
196
if (pts1.type() != CV_32F)
197
pts1.convertTo(pts1, CV_32F);
198
if (pts2.type() != CV_32F)
199
pts2.convertTo(pts2, CV_32F);
201
// Use only valid matchings //
202
std::vector<DMatch> matches;
203
for (size_t i=0; i<_matches.size(); i++)
205
if (_matches[i].queryIdx<pts1.cols &&
206
_matches[i].trainIdx<pts2.cols)
208
matches.push_back(_matches[i]);
212
// Organizing the correspondent points in matrix style //
213
Mat shape1((int)matches.size(),2,CV_32F); // transforming shape
214
Mat shape2((int)matches.size(),2,CV_32F); // target shape
215
for (int i=0, end = (int)matches.size(); i<end; i++)
217
Point2f pt1=pts1.at<Point2f>(0,matches[i].queryIdx);
218
shape1.at<float>(i,0) = pt1.x;
219
shape1.at<float>(i,1) = pt1.y;
221
Point2f pt2=pts2.at<Point2f>(0,matches[i].trainIdx);
222
shape2.at<float>(i,0) = pt2.x;
223
shape2.at<float>(i,1) = pt2.y;
225
shape1.copyTo(shapeReference);
227
// Building the matrices for solving the L*(w|a)=(v|0) problem with L={[K|P];[P'|0]}
229
//Building K and P (Neede to buil L)
230
Mat matK((int)matches.size(),(int)matches.size(),CV_32F);
231
Mat matP((int)matches.size(),3,CV_32F);
232
for (int i=0, end=(int)matches.size(); i<end; i++)
234
for (int j=0; j<end; j++)
238
matK.at<float>(i,j)=float(regularizationParameter);
242
matK.at<float>(i,j) = distance(Point2f(shape1.at<float>(i,0),shape1.at<float>(i,1)),
243
Point2f(shape1.at<float>(j,0),shape1.at<float>(j,1)));
246
matP.at<float>(i,0) = 1;
247
matP.at<float>(i,1) = shape1.at<float>(i,0);
248
matP.at<float>(i,2) = shape1.at<float>(i,1);
252
Mat matL=Mat::zeros((int)matches.size()+3,(int)matches.size()+3,CV_32F);
253
Mat matLroi(matL, Rect(0,0,(int)matches.size(),(int)matches.size())); //roi for K
254
matK.copyTo(matLroi);
255
matLroi = Mat(matL,Rect((int)matches.size(),0,3,(int)matches.size())); //roi for P
256
matP.copyTo(matLroi);
258
transpose(matP,matPt);
259
matLroi = Mat(matL,Rect(0,(int)matches.size(),(int)matches.size(),3)); //roi for P'
260
matPt.copyTo(matLroi);
263
Mat matB = Mat::zeros((int)matches.size()+3,2,CV_32F);
264
for (int i=0, end = (int)matches.size(); i<end; i++)
266
matB.at<float>(i,0) = shape2.at<float>(i,0); //x's
267
matB.at<float>(i,1) = shape2.at<float>(i,1); //y's
270
//Obtaining transformation params (w|a)
271
solve(matL, matB, tpsParameters, DECOMP_LU);
272
//tpsParameters = matL.inv()*matB;
274
//Setting transform Cost and Shape reference
275
Mat w(tpsParameters, Rect(0,0,2,tpsParameters.rows-3));
277
transformCost=fabs(Q.at<float>(0,0)*Q.at<float>(1,1));//fabs(mean(Q.diag(0))[0]);//std::max(Q.at<float>(0,0),Q.at<float>(1,1));
281
Ptr <ThinPlateSplineShapeTransformer> createThinPlateSplineShapeTransformer(double regularizationParameter)
283
return Ptr<ThinPlateSplineShapeTransformer>( new ThinPlateSplineShapeTransformerImpl(regularizationParameter) );