fitting the content of an elliptical region in an image into the equivalent stretched circle

351 Views Asked by At

Assuming that I know the ellipse parameters (the major/minor axis, theta and the centre), and I could get the equivalent circle of this ellipse (I mean the equivalent x and y positions of the pixels within the equivalent circle), how can I transfer the content (pixel values) of that ellipse into this circle so that it completely fills it (i.e, the content of the ellipse has been stretched to fill that of the circle? The figure below illustrates an example of what I mean.enter image description here

1

There are 1 best solutions below

6
On

Let $f$ be the image in the ellipse, $g$ the image in the circle, and $\phi$ the coordinate mapping from the ellipse to the circle.

Define the set of values you want to resample to in the circle and call them $(a_i,b_i)$, for $i=1, \dots, N$. Map the circle locations to the equivalent positions in the ellipse: $\phi^{-1}(a_i,b_i)\rightarrow (x_i,y_i)$. Now, you already have image data for a set of $(x,y)$ pairs in the ellipse, so let $f(x,y)$ be the value of the image at the point $(x,y)$. All you have to do is interpolate the known image values $f(x,y)$ onto the locations given by $(x_i,y_i)$, that is, $g(a_i,b_i) = f(x_i,y_i)$.

Here is an example in matlab, though since it starts with circular coins and deforms an ellipse to a circle, it looks like we are deforming circles to ellipses. I hope that makes sense. I couldn't find a built-in image of an ellipse, so I used this one.

% Test image and its sample locations.
f = double( imread( 'coins.png' ) );
x = ( -1 : 2 / ( size( f, 2 ) - 1 ) : 1 );
y = ( -1 : 2 / ( size( f, 1 ) - 1 ) : 1 )';

% Define the output sample locations. We go bigger
% than the input so that we can see the ellipse
% deformed into a circle.
a = (-2:0.01:2);
b = (-2:0.01:2)';
[ a_mesh, b_mesh ] = meshgrid( a, b );

% The parameters of the ellipse that is mapped
% to the unit circle.
e_x = 1;
e_y = 1/2;

% Now we map the so-called circle coordinates
% to the ellipse coordinates.
theta = atan2( b_mesh, a_mesh );
r = reshape( sqrt( sum( a_mesh(:).^2 + b_mesh(:).^2, 2 ) ), size( a_mesh ) );
x_i = e_x * r .* cos( theta );
y_i = e_y * r .* sin( theta );

% Perform the interpolation.
g = interp2( x, y, f, x_i, y_i, 'linear' );

figure;
title( 'Circle Image' );
imagesc( a, b, g, [0 255] );
colormap( gray );
axis( 'equal' );

figure;
title( 'Ellipse Image' );
imagesc( x, y, f, [0 255] );
colormap( gray );
axis( 'equal' );