MATLAB: Affine transformation that takes a given, known ellipse and maps it to a circle with diameter equal to the major axis.

geometric matrix transposeimage analysisimage manipulationimage processingMATLAB

I am looking for the affine transformation that takes a given, known ellipse and maps it to a circle with diameter equal to the major axis. I plan to use this transformation matrix to map the image's original coordinates to new ones, thereby stretching the ellipse into a circle. Some assistance would be greatly appreciated.

Best Answer

I'm writing this down as a new answer so I can get markup for the code snippets. Let's work with the first blob from regionprops:
stats = regionprops(L)
'Orientation' is alpha, but in degrees:
alpha = pi/180 * stats(1).Orientation;
Q = [cos(alpha), -sin(alpha); sin(alpha), cos(alpha)];
'Centroid' is x0, but as a row vector (I think)
x0 = stats(1).Centroid.';
'MajorAxisLength' and 'MinorAxisLength' are a and b respectively
a = stats(1).MajorAxisLength;
b = stats(1).MinorAxisLength;
We've now got everything, so lets assemble these into the matrices we need:
S = diag([1, a/b]);
C = Q*S*Q';
d = (eye(2) - C)*x0;
You now have the matrices you need to build the affine transformation. Now if you're using maketform, it wants an transformation matrix that acts on homogeneous coordinates in row vector form, not columns, so we take the transpose of what you might expect.
tform = maketform('affine', [C d; 0 0 1]');
Then with a bit of luck, this will work (I'm doing this without the benefit of the image processing toolbox on my computer)
Im2 = imtransform(Im, tform)