This source file includes following definitions.
- setFullAffine
- getFullAffine
- write
- read
- warpImage
- _localAffineEstimate
- estimateTransformation
- applyTransformation
- createAffineTransformer
#include "precomp.hpp"
namespace cv
{
class AffineTransformerImpl : public AffineTransformer
{
public:
AffineTransformerImpl()
{
fullAffine = true;
name_ = "ShapeTransformer.AFF";
}
AffineTransformerImpl(bool _fullAffine)
{
fullAffine = _fullAffine;
name_ = "ShapeTransformer.AFF";
}
~AffineTransformerImpl()
{
}
virtual void estimateTransformation(InputArray transformingShape, InputArray targetShape, std::vector<DMatch> &matches);
virtual float applyTransformation(InputArray input, OutputArray output=noArray());
virtual void warpImage(InputArray transformingImage, OutputArray output,
int flags, int borderMode, const Scalar& borderValue) const;
virtual void setFullAffine(bool _fullAffine) {fullAffine=_fullAffine;}
virtual bool getFullAffine() const {return fullAffine;}
virtual void write(FileStorage& fs) const
{
fs << "name" << name_
<< "affine_type" << int(fullAffine);
}
virtual void read(const FileNode& fn)
{
CV_Assert( (String)fn["name"] == name_ );
fullAffine = int(fn["affine_type"])?true:false;
}
private:
bool fullAffine;
Mat affineMat;
float transformCost;
protected:
String name_;
};
void AffineTransformerImpl::warpImage(InputArray transformingImage, OutputArray output,
int flags, int borderMode, const Scalar& borderValue) const
{
CV_Assert(!affineMat.empty());
warpAffine(transformingImage, output, affineMat, transformingImage.getMat().size(), flags, borderMode, borderValue);
}
static Mat _localAffineEstimate(const std::vector<Point2f>& shape1, const std::vector<Point2f>& shape2,
bool fullAfine)
{
Mat out(2,3,CV_32F);
int siz=2*(int)shape1.size();
if (fullAfine)
{
Mat matM(siz, 6, CV_32F);
Mat matP(siz,1,CV_32F);
int contPt=0;
for (int ii=0; ii<siz; ii++)
{
Mat therow = Mat::zeros(1,6,CV_32F);
if (ii%2==0)
{
therow.at<float>(0,0)=shape1[contPt].x;
therow.at<float>(0,1)=shape1[contPt].y;
therow.at<float>(0,2)=1;
therow.row(0).copyTo(matM.row(ii));
matP.at<float>(ii,0) = shape2[contPt].x;
}
else
{
therow.at<float>(0,3)=shape1[contPt].x;
therow.at<float>(0,4)=shape1[contPt].y;
therow.at<float>(0,5)=1;
therow.row(0).copyTo(matM.row(ii));
matP.at<float>(ii,0) = shape2[contPt].y;
contPt++;
}
}
Mat sol;
solve(matM, matP, sol, DECOMP_SVD);
out = sol.reshape(0,2);
}
else
{
Mat matM(siz, 4, CV_32F);
Mat matP(siz,1,CV_32F);
int contPt=0;
for (int ii=0; ii<siz; ii++)
{
Mat therow = Mat::zeros(1,4,CV_32F);
if (ii%2==0)
{
therow.at<float>(0,0)=shape1[contPt].x;
therow.at<float>(0,1)=shape1[contPt].y;
therow.at<float>(0,2)=1;
therow.row(0).copyTo(matM.row(ii));
matP.at<float>(ii,0) = shape2[contPt].x;
}
else
{
therow.at<float>(0,0)=-shape1[contPt].y;
therow.at<float>(0,1)=shape1[contPt].x;
therow.at<float>(0,3)=1;
therow.row(0).copyTo(matM.row(ii));
matP.at<float>(ii,0) = shape2[contPt].y;
contPt++;
}
}
Mat sol;
solve(matM, matP, sol, DECOMP_SVD);
out.at<float>(0,0)=sol.at<float>(0,0);
out.at<float>(0,1)=sol.at<float>(1,0);
out.at<float>(0,2)=sol.at<float>(2,0);
out.at<float>(1,0)=-sol.at<float>(1,0);
out.at<float>(1,1)=sol.at<float>(0,0);
out.at<float>(1,2)=sol.at<float>(3,0);
}
return out;
}
void AffineTransformerImpl::estimateTransformation(InputArray _pts1, InputArray _pts2, std::vector<DMatch>& _matches)
{
Mat pts1 = _pts1.getMat();
Mat pts2 = _pts2.getMat();
CV_Assert((pts1.channels()==2) && (pts1.cols>0) && (pts2.channels()==2) && (pts2.cols>0));
CV_Assert(_matches.size()>1);
if (pts1.type() != CV_32F)
pts1.convertTo(pts1, CV_32F);
if (pts2.type() != CV_32F)
pts2.convertTo(pts2, CV_32F);
std::vector<DMatch> matches;
for (size_t i=0; i<_matches.size(); i++)
{
if (_matches[i].queryIdx<pts1.cols &&
_matches[i].trainIdx<pts2.cols)
{
matches.push_back(_matches[i]);
}
}
std::vector<Point2f> shape1;
std::vector<Point2f> shape2;
for (size_t i=0; i<matches.size(); i++)
{
Point2f pt1=pts1.at<Point2f>(0,matches[i].queryIdx);
shape1.push_back(pt1);
Point2f pt2=pts2.at<Point2f>(0,matches[i].trainIdx);
shape2.push_back(pt2);
}
Mat affine;
estimateRigidTransform(shape1, shape2, fullAffine).convertTo(affine, CV_32F);
if (affine.empty())
affine=_localAffineEstimate(shape1, shape2, fullAffine);
affineMat = affine;
}
float AffineTransformerImpl::applyTransformation(InputArray inPts, OutputArray outPts)
{
Mat pts1 = inPts.getMat();
CV_Assert((pts1.channels()==2) && (pts1.cols>0));
Mat fAffine;
transform(pts1, fAffine, affineMat);
if (outPts.needed())
{
outPts.create(1,fAffine.cols, CV_32FC2);
Mat outMat = outPts.getMat();
for (int i=0; i<fAffine.cols; i++)
outMat.at<Point2f>(0,i)=fAffine.at<Point2f>(0,i);
}
Mat Af(2, 2, CV_32F);
Af.at<float>(0,0)=affineMat.at<float>(0,0);
Af.at<float>(0,1)=affineMat.at<float>(1,0);
Af.at<float>(1,0)=affineMat.at<float>(0,1);
Af.at<float>(1,1)=affineMat.at<float>(1,1);
SVD mysvd(Af, SVD::NO_UV);
Mat singVals=mysvd.w;
transformCost=std::log((singVals.at<float>(0,0)+FLT_MIN)/(singVals.at<float>(1,0)+FLT_MIN));
return transformCost;
}
Ptr <AffineTransformer> createAffineTransformer(bool fullAffine)
{
return Ptr<AffineTransformer>( new AffineTransformerImpl(fullAffine) );
}
}