Affine Transformation matrix estimation gives inconsistent result with MATLAB
3 views (last 30 days)
Show older comments
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
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
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)
0 Comments
Accepted Answer
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)
More Answers (1)
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.
See Also
Categories
Find more on Computer Vision with Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


