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

15 views (last 30 days)
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.

Accepted Answer

Richard Brown
Richard Brown on 11 Apr 2012
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)

More Answers (1)

Richard Brown
Richard Brown on 11 Apr 2012
Not difficult to do, but a bit fiddly. Exactly how difficult depends on the form of your known ellipse equation. I'm going to assume that your ellipse is in the following parametric form, you may need to adapt from there:
x = x0 + Q * [a * cos(theta); b * sin(theta)]
where x and x0 are 2-vectors, with a > b > 0, and where Q is a rotation matrix:
Q = [cos(alpha), -sin(alpha); sin(alpha) cos(alpha)]
First, change variables to y = Q' * (x - x0). then
y = [a * cos(theta); b * sin(theta)];
Lets say you want to your transformation to produce yp in the shifted, rotated coordinates, and xp in the original coordinates. Then, your change of variables will be as follows:
yp = diag([1, a/b]) * y
= S * y
and hence
xp = Q*yp + x0
= Q*S*Q'*(x - x0) + x0
= Q*S*Q'*x + (eye(2) - Q*S*Q')*x0
which is your required affine transformation. If your ellipse is in a different form, then you'll need to convert to parametric form first.
  1 Comment
LoserG G
LoserG G on 11 Apr 2012
Richard, thanks a lot for your excellent reply. My math is a bit lacking and I would appreciate if you could clarify a few things, but first let me elaborate on my question a bit.
By a known ellipse I meant that I have a binary labeled image with the borders of the ellipse region of interest (I ran edge detection on an image of a coin, basically). This allowed me to use the regionprops function to calculate the major axis length, minor axis length (I assume these values correspond to a and b respectively?), orientation and area of the ellipse. Essentially, what I was looking for was the affine transform that would enable me to stretch a grayscale image of the ellipse ROI into a circle using the major axis length as the diameter.
Do you think you could walk me through that process?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!