Affine Transformation matrix estimation gives inconsistent result with MATLAB

3 views (last 30 days)
I wanted to compare the result from the tform obtained using "fitgeotrans" with the result obtained from least-squares parameters estimation of the affine transformation. I get different results in the translation but correct results in the rotation. Below is an example of the code, maybe I am missing something in my code or maybe I missing something on how Matlab does its computation albeit I know it uses a variant of ransac, but the kepoints are not unmatched in this simple example.
clear all;
I = checkerboard(40);
J = imrotate(I,30);
imshowpair(I,J,'montage')
fixedPoints = [41 41; 281 161; 281 281];
movingPoints = [56 175; 324 160;384 263];
tform1 = fitgeotrans(movingPoints,fixedPoints,'NonreflectiveSimilarity');
tform1.T
ans = 3×3
0.8667 0.4988 0 -0.4988 0.8667 0 79.7088 -138.8091 1.0000
Jregistered1 = imwarp(J,tform1,'OutputView',imref2d(size(I)));
matchedOriginal = fixedPoints;
matchedDistorted = movingPoints;
if size(matchedDistorted) == size(matchedOriginal)
Amat = zeros(2*size(matchedOriginal,1),6);
bvec= zeros(2*size(matchedDistorted,1),1);
j = 1;
for i=1:size(matchedOriginal,1)
Amat(j,:) = [matchedOriginal(i,:) 1 0 0 0];
Amat(j+1,:) = [0 0 0 matchedOriginal(i,:) 1];
bvec(j,1) = matchedDistorted(i,1);
bvec(j+1,1)= matchedDistorted(i,2);
j = j+2 ;
end
affineParam = Amat\bvec;
end
tform2 = affine2d([affineParam(1,1) ,affineParam(2,1) ,0 ; ...
affineParam(4,1) , affineParam(5,1),0 ; ...
affineParam(3,1) ,affineParam(6,1) , 1]);
tform2.T
ans = 3×3
0.8667 0.5000 0 -0.4917 0.8583 0 -0.0333 159.9667 1.0000
Jregistered2 = imwarp(J,tform2,'OutputView',imref2d(size(I)));
centroidOrig = [sum(fixedPoints(:,1))/size(fixedPoints,1), sum(fixedPoints(:,2))/size(fixedPoints,1)];
centroidDistorted = [sum(movingPoints(:,1))/size(movingPoints,1), sum(movingPoints(:,2))/size(movingPoints,1)];
% To check the translation
tvec = centroidDistorted' - tform2.T(1:2,1:2)*centroidOrig';
figure(1)
imshowpair(I,Jregistered1)
figure(2)
imshowpair(I,Jregistered2)

Accepted Answer

Matt J
Matt J on 4 Jan 2022
Edited: Matt J on 4 Jan 2022
These were mixed up:
matchedOriginal = movingPoints;
matchedDistorted = fixedPoints;
Also, you loaded your affineParam(i) elements in the wrong order into the matrix
tform2 = affine2d([affineParam(1) ,affineParam(4) ,0 ; ...
affineParam(2) , affineParam(5),0 ; ...
affineParam(3) ,affineParam(6) , 1]);
tform2.T;
With those corrections, the images agree much better.
figure(1)
imshowpair(I,Jregistered1)
figure(2)
imshowpair(I,Jregistered2)
  2 Comments
Sam Callahan
Sam Callahan on 4 Jan 2022
Edited: Sam Callahan on 4 Jan 2022
Thanks. I get that the affine transform is transposed in Matlab. But I don't get why are these mixed up. Is it because I am reversing the warp ?!
matchedOriginal = movingPoints;
matchedDistorted = fixedPoints;
Matt J
Matt J on 4 Jan 2022
Edited: Matt J on 4 Jan 2022
The movingPoints are the ones to which the transform is applied. In your equations,
Amat*affineParams=bvec
the left hand side is where the parameters are applied, so Amat must be built from the movingPoint data, not the fixedPoint data.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 3 Jan 2022
Edited: Matt J on 4 Jan 2022
Your algebraic method estimates a general 6DOF affine transform, whereas your call to fitgeotrans requests a non-reflective similarity transform. Because similarity transforms are a restricted special case of affine transforms, you are doing a less constrained estimation than what fitgeotrans is doing.
  1 Comment
Sam Callahan
Sam Callahan on 4 Jan 2022
Edited: Sam Callahan on 4 Jan 2022
Shouldn't theoretically speaking 3 pairs be sufficient to estimate the 6 parameters of affine. Even if the affine structure is general, shouldn't the least squares estimation result in matrix parameters that show accurately only the rotation and translation. I actually tried 6 pairs and got exactly the same translation value from the least-squares. Is there anything fitgeotrans is doing that I am not aware of.....maybe pixel manipulation to register the images, which is something I am not doing....am I erroneously misinterpreting the translation value from the least squares ?

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!