34 #include <Base/Image/Image.hh>
35 #include <Base/ImageUtils/ImageDraw.hh>
36 #include <Base/Image/ImageIO.hh>
37 #include <Base/Image/ImageConvert.hh>
38 #include <Base/Geometry/HomgPoint2D.hh>
39 #include <Base/Debug/TimeMeasure.hh>
40 #include <Image/TextureTransformHomography.hh>
41 #include <Image/TextureTransformAffine.hh>
42 #include <Base/Math/Random.hh>
43 #include "MathAlgo/Lapack.hh"
44 #include <Matcher2D/ImageAlignment.hh>
45 #include <Image/HomographyMapping.hh>
46 #include <Geometry/RMatrix.hh>
49 #include "Base/Common/FileHandling.hh"
50 #include "Utils/ThreeDOut.hh"
62 BIASERR(
"error loading image "<<name);
67 BIASERR(
"error converting image "<<name);
73 BIASERR(
"error converting image to grey "<<name);
84 int main(
int argc,
char*argv[])
88 cerr << argv[0] <<
" <image> "<< endl;
93 if (LoadImage(argv[1], im[0]) != 0) {
94 BIASERR(
"error loading image " << argv[0]);
107 cout<<
"Testing AFFINITY"<<endl;
108 par.
newsize(6); par[0] = 0.0; par[1] = 0; par[2] = 0; par[3] = 0;
109 par[4] = 0; par[5] = 0;
114 cout<<
"using parameters "<<par<<endl;
119 (im[0].GetHeight()-1.0)/2.0);
124 cout<<
"setting origin !"<<thepoint<<endl;
129 p[0][2] = p[1][2] = 1.0;
153 K[1][1] =K[0][0] = im[0].
GetWidth()*2.0;
155 K[0][2] = (im[0].
GetWidth()-1.0)/2.0;
157 cout <<
"K is "<<K<<endl;
164 for (
unsigned int i=0;i<4;i++) PredictedCov[i][i] = 0.01;
166 for (
unsigned int i=0; i<4; i++)
167 EmpiricCov[i][i] = 0.5/(d*d*2.0);
168 for (
unsigned int i=4; i<6; i++)
169 EmpiricCov[i][i] = 0.5;
172 ofstream logfile(
"trackingerror.log");
173 logfile<<
"# log written by "<<argv[0]<<
" hws="<<d<<endl<<flush;
174 logfile<<
"# angle empiricdistance preddistance distance cornercovdistance result"<<endl<<flush;
177 P1.InvalidateDecomposition();
179 vrml.
AddPMatrix(P1,im[0].GetWidth(), im[0].GetHeight(),
RGBAuc(0,255,0,255),0.1);
182 for (
unsigned int angle=0; angle<80; angle+=2) {
183 cout<<
"angle is now "<<angle<<
" degree."<<endl;
198 Rot.
Set(axis,
double(angle)/180.0*M_PI);
199 cout<<
"Rot is "<<Rot<<endl;
202 cout <<
"C is "<<C<<endl;
206 vrml.
AddPMatrix(P2,im[0].GetWidth(), im[0].GetHeight(),
RGBAuc(0,0,255,255),0.1);
222 double pixelnoise = 0.0;
223 if (pixelnoise>0.0) {
228 }
while (pData++ < pDataEnd);
239 cout<<
"Setting alignment pixels "<<endl<<flush;
241 cout<<
"Aligning ..."<<endl<<flush;
256 int result = IA.
AutoAlign(i1, i2, par, cov);
260 BIASERR(
"alignment failed !!!"<<result);
262 cout<<
"angle is "<<angle<<endl;
263 if (angle%10==0) getchar();
268 double linearizationerror = 0;
269 for (
unsigned int corner=0; corner<5; corner++) {
272 case 0: cornerpoint =
HomgPoint2D(thepoint[0]-d,thepoint[1]-d);
break;
273 case 1: cornerpoint =
HomgPoint2D(thepoint[0]-d,thepoint[1]+d);
break;
274 case 2: cornerpoint =
HomgPoint2D(thepoint[0]+d,thepoint[1]-d);
break;
275 case 3: cornerpoint =
HomgPoint2D(thepoint[0]+d,thepoint[1]+d);
break;
278 linearizationerror +=
287 for (
unsigned int i=0; i<cornerParams.size(); i++) {
288 meanCornerParam += cornerParams[i];
290 meanCornerParam *= 1.0/double(cornerParams.size());
292 for (
unsigned int i=0; i<cornerParams.size(); i++) {
296 cornerCov *= 1.0/double(cornerParams.size()-1.0);
298 for (
unsigned int i=0; i<6; i++) cornerCov[i][i] += 1e-8;
300 linearizationerror /= 4.0;
301 cout<<
"average linearization error is "<<linearizationerror<<endl;
302 PredictedCov.SetIdentity();
303 PredictedCov *= 1e-8;
304 for (
unsigned int i=0; i<4; i++)
305 PredictedCov[i][i] += linearizationerror*linearizationerror/(d*d*2.0);
306 for (
unsigned int i=4; i<6; i++)
307 PredictedCov[i][i] += linearizationerror*linearizationerror;
313 cout<<
"empiric distance is "<<empiricdistance<<endl;
315 cout<<
"compensated distance is "<<preddistance<<endl;
316 for (
unsigned int i=0; i<6; i++) cov[i][i] += 1e-8;
318 cout<<
"obtained data distance is "<<distance<<endl;
320 cout<<
"corner cov distance is "<<cornercovdistance<<endl;
322 logfile<<angle<<
" "<<empiricdistance<<
" "<<preddistance<<
" "<<distance<<
" "<<cornercovdistance<<
" "<<result<<endl<<flush;
analytic properties of affine image warp
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
void AddDebugLevel(const long int lv)
int VRMLOut(const std::string &sFilename)
flush all 3d objects to a vrml file with name sFilename, this is the function most users would call ...
void SetOrigin(const Vector2< double > &origin)
origin relative to which rotation and scale is performed
a 3x3 Matrix describing projective transformations between planes
Maps image src to image sink with homography H (software implementation)
affine transformation of 2D image plane which relates image coordinate system and local affine featur...
Unified output of 3D entities via OpenGL or VRML.
Matrix< T > & newsize(Subscript M, Subscript N)
int AutoAlign(const PyramidImage< float > &I1, const PyramidImage< float > &I2, Vector< double > ¶ms, Matrix< double > &Cov)
aligns image1 with image2 with respect to parameters params, scale space tracking is used recursively...
unsigned int Size() const
length of the vector
void SetA(const BIAS::Matrix2x2< double > &A, const BIAS::Matrix< double > &cov=BIAS::Matrix< double >(4, 4, BIAS::MatrixZero))
return affine matrix
unsigned int GetWidth() const
void SetAlignmentPixelsRectangular(const LocalAffineFrame &LAF, int numberOfPixels=-1)
choose any rectangular tracking region and resolution
Vector< T > & newsize(Subscript N)
void SetUpdateThreshold(const double maxerr)
if a step smaller (norm L2) than this is taken, convergence is declared and the system finishes ...
void Set(const Vector3< ROTATION_MATRIX_TYPE > &w, const ROTATION_MATRIX_TYPE phi)
Set from rotation axis w and angle phi (in rad)
Vector< double > ExtractParameters(const Matrix3x3< double > &A) const
static int ConvertST(const BIAS::ImageBase &source, BIAS::ImageBase &dest, ImageBase::EStorageType targetST)
Function to convert the storage type of images e.g.
int GetInverse(Matrix3x3< T > &inv) const
Matrix inversion: inverts this and stores resulty in argument inv.
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
void SetT(const BIAS::Vector2< double > &T, const BIAS::Matrix< double > &cov=BIAS::Matrix< double >(2, 2, BIAS::MatrixZero))
return offset
void Init(const Image< StorageType > &image, const unsigned py_size=0)
copy image into level 0 and create other levels according to parameters set so far (pyramidsize...
void MapAcrossPlane(const PMatrix &P1, const PMatrix &P2, const HomgPlane3D &scenePlane=HomgPlane3D(0, 0, 0, 1))
Set (*this) to map coordinates from camera p1 to p2 using the given scene plane.
HMatrix GetLinearized(const HomgPoint2D &x) const
returns 1st order Taylor expansion of homography at x
unsigned int GetHeight() const
int Map(const Image< InputStorageType > &src, Image< OutputStorageType > &sink, InterpolationMethod=MapTrilinear, bool newSink=false, double SuperSampling=1.0)
backward mapping with various interpolations
Inverse Compositional Image Alignment ("Registration")
BIAS::Vector4< unsigned char > RGBAuc
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
void SetIdentity()
Converts matrix to identity matrix.
static std::string toString(const T thenumber, int numzeros=DEFAULT_LEADING_ZEROS)
Converts number to string with leading zeros.
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
static int Load(const std::string &FileName, ImageBase &img)
first tries a call to Read MIP image and if that fails, tries to Import Image with all other availabl...
K describes the mapping from world coordinates (wcs) to pixel coordinates (pcs).
void SetTextureTransform(const TextureTransform &T)
pass object type which holds texture transform
double ComputeLinearizationError(const HomgPoint2D &x0, const HomgPoint2D &y) const
compute distance between y mapped with homography or first order taylor approximation at development ...
unsigned int AddPMatrix(BIAS::PMatrix &P, const unsigned int &width, const unsigned int &height, const BIAS::RGBAuc &Color=RGBAuc_WHITE_OPAQUE, const double &dScale=DEF_P_SCALE, const std::string &name="")
decompose P and add to internal data structures.
double MahalanobisDistance(const BIAS::Matrix< double > &Sigma, const BIAS::Vector< double > &V)
computes squared Mahalanobis distance V^T Sigma^-1 V efficiently using cholesky decomposition and exp...
double GetNormalDistributed(const double mean, const double sigma)
on succesive calls return normal distributed random variable with mean and standard deviation sigma ...
describes a projective 3D -> 2D mapping in homogenous coordinates
unsigned int AddPoint(const BIAS::Vector3< double > &v, const BIAS::RGBAuc &Color=RGBAuc_WHITE_OPAQUE)
void SetDampening(const double &dampening)
adds a dampening d to hessian's diag
void SetMetaData(const MetaData &m)
unsigned long int GetPixelCount() const
returns number of pixels in image
This is the base class for images in BIAS.
void SetHomography(const HMatrix &H)
set your homography H (source = H * sink) before calling Map()
class for producing random numbers from different distributions
void SetZero()
zeroes the image
class BIASGeometryBase_EXPORT HomgPoint2D
void SetMaxIterations(const int maxiter)
number of iterations, before the system stops unsuccessfully
void SetIdentity()
set the elements of this matrix to the identity matrix (possibly overriding the inherited method) ...
void SetParameters(const Vector< double > &p)
a11-1, a12, a21, a22-1, dx, dy
A homogeneous plane (in P^3) All points X on the plane p fulfill p ' * X = 0.
static int ToGrey(const ImageBase &source, ImageBase &dest)
wrapper for the templated function ToGrey