MATLAB: How to use functions “fitgeotrans” and “transformPointsForward” instead of “cp2tform” and “tformfwd”

Image Processing Toolbox

I am using the function "cp2tform", and the hover tip says to use the function "fitgeotrans" instead. However, the 'tform' output is different from each, and I need to run a forward transform using the function "tformfwd" for a set of points (not an image). The output of the function "fitgeotrans" is an object which appears to be suited for image I/O only. Can you advise how to proceed?

Best Answer

It is possible to use the function "cp2tform" to get a 'tform' structure and use it as an input to the function "tformfwd". However, the documentation for the function "cp2tform" recommends using the function "fitgeotrans" instead. The function "fitgeotrans" returns a 'tform' object, which is not a valid input to the function "tformfwd". This issue can be resolved by using the function "transformPointsForward" instead of "tformfwd".
The following is an example of how one can use the function "fitgeotrans" followed by "transformPointsForward" instead of "cp2tform" followed by "tformfwd". The only additional step required in the new workflow is to create a 'projective2d' object from the 'tform' object returned by the function "fitgeotrans". This 'projective2d' object is a valid input to the function "transformPointsForward".
Example:
Please refer to the following example that shows how to use the function "cp2tform":
 
% Transform an image, use the cp2tform function to return the transformation, and
% compare the angle and scale of the TFORM to the angle and scale of the original
% transformation:
I = checkerboard;
J = imrotate(I,30);
fixedPoints = [11 11; 41 71];
movingPoints = [14 44; 70 81];
cpselect(J,I,movingPoints,fixedPoints);
t = cp2tform(movingPoints,fixedPoints,'nonreflective similarity');
% Recover angle and scale by checking how a unit vector
% parallel to the x-axis is rotated and stretched.
u = [0 1];
v = [0 0];
[x, y] = tformfwd(t, u, v);
dx = x(2) - x(1);
dy = y(2) - y(1);
angle = (180/pi) * atan2(dy, dx);
scale = 1 / sqrt(dx^2 + dy^2);
In this example, an image is transformed and the function "cp2tform" is used to return the transformation as a 'tform structure'. Subsequently, the angle and scale of the 'tform' is obtained using the function "tformfwd", and compared to the angle and scale of the original transformation.
The MATLAB script "usingFITGEOTRANS.m" demonstrates how the same example can be rewritten using functions "fitgeotrans" and "transformPointsForward".
Please note the following key differences in these two examples:
(1) Using the function "fitgeotrans" instead of "cp2tform":
 
% Using "cp2tform"
t = cp2tform(movingPoints,fixedPoints,'nonreflective similarity');
% Using "fitgeotrans"
t = fitgeotrans(movingPoints,fixedPoints,'nonreflectivesimilarity');
(2) Creating a 'projective2d' object from the 'tform' object, and using it with the function "transformPointsForward" instead of "tformfwd":
 
% Using "tformfwd"
[x, y] = tformfwd(t, u, v);
% Using "transformPointsForward"
projectiveObj = projective2d(t.T);
[x, y] = transformPointsForward(projectiveObj, u, v);