Code covered by the BSD License

### Highlights from Polar To/From Rectangular Transform of Images

4.38889
4.4 | 18 ratings Rate this file 90 Downloads (last 30 days) File Size: 47 KB File ID: #17933 Version: 1.0

# Polar To/From Rectangular Transform of Images

### Prakash Manandhar (view profile)

08 Dec 2007 (Updated )

converts rectangular image to polar and back

File Information
Description

Conversion from Rectangular to Polar Image and back from Polar to Rectangular.

V0.1 16 Dec 2007 (Created), Prakash Manandhar pmanandhar@umassd.edu

Required Products Image Processing Toolbox
MATLAB release MATLAB 7 (R14)
17 Aug 2015 Yannick Mermet

### Yannick Mermet (view profile)

hi there
I am using the ImtoPolar.m file. how do I display the polar image after the transformation?

Comment only
14 Sep 2014 Sophia

### Sophia (view profile)

18 May 2014 Kristian Moesgaard

### Kristian Moesgaard (view profile)

Here the code is to work with RGB image and independent on image format. Thought somebody might find it useful :)

function imP = FISHCOLOR2(imR)

rMin=0.1;
rMax=1;

reduced_dim = min(size(imR,1),size(imR,2));
imR = imresize(imR,[reduced_dim reduced_dim]);

M=size(imR,1);N=size(imR,2);

CenterX = (M+1)/2; % co-ordinates of the center of the image
CenterY = (N+1)/2;
ScaleX = (M-1)/2; % scale factors
ScaleY = (N-1)/2;

DeltaR = (rMax - rMin)/(M-1);
DeltaTheta = 2*pi/N;

R=rMin:DeltaR:rMin+(M-1)*DeltaR;
Theta=0:DeltaTheta:(N-1)*DeltaTheta;
[R,Theta]=meshgrid(R,Theta);

X=R*cos(Theta);
Y=R*sin(Theta);
LatX = X*ScaleX + CenterX;
LongY = Y*ScaleY + CenterY;

for k=1:3 % colors
imP(:,:,k) = interp2(imR(:,:,k), LatX, LongY); % add k channel
end

imP = imresize(imP,[size(imP,1), size(imP,2)/3]);

imP = imrotate(imP,270);

Comment only

@Kristian: If your RGB image comes from the imread function, it is an M x N x 3 array. You can then apply ImToPolar or PolarToIm (whichever you're interested in) 3 times, once for each plane (R,G,B) of the image. For instance:

im(:,:,1) = PolarToIm(polar(:,:,1), rMin, rMax, Mr, Nr);
im(:,:,2) = PolarToIm(polar(:,:,2), rMin, rMax, Mr, Nr);
im(:,:,3) = PolarToIm(polar(:,:,3), rMin, rMax, Mr, Nr);

Hope this helps!

Comment only
23 Apr 2014 Kristian Moesgaard

### Kristian Moesgaard (view profile)

Great and functional work.. Does anybody know a quick way of converting this code so that i can be used on a RGB image?

18 Mar 2014 Killo

### Killo (view profile)

28 Jan 2014 ramsha

### ramsha (view profile)

it worked out :) awsm work

Comment only
26 Jan 2014 ramsha

### ramsha (view profile)

if i want to transform circluar to rectangle ? thn

04 Sep 2013 Yves DELIGNON

### Yves DELIGNON (view profile)

Thank you, very useful

20 Dec 2012 Florian

### Florian (view profile)

Cool! Thanks a lot

27 Mar 2012 Ruth Livingstone

### Ruth Livingstone (view profile)

Sorry, That looks a little unclear. Here it is again

function imC = Polar2Im(imP,W,method)
%Polar2Im turns a polar image (imP) into a cartesian image (imC) of width W
%method can be: '*linear', '*cubic', '*spline', or '*nearest'.
imP(isnan(imP))=0;
w = round(W/2);
xy = (1:W-w);
[M N P]= size(imP);
[x y] = meshgrid(xy,xy);
n = round(N/4);
rr = linspace(1,w,M);
W1 = w:-1:1;
PM = [2 1 3;1 2 3;2 1 3;1 2 3];
W2 = w+1:2*w;
nn = [1:n; n+1:2*n; 2*n+1:3*n; 3*n+1:N;];
w1 = [W1;W2;W2;W1];
w2 = [W2;W2;W1;W1];
aa = linspace(0,90*pi/180,n);
r = sqrt(x.^2 + y.^2);
a = atan2(y,x);
imC= zeros(W,W,P);
for i=1:4 %turn each quarter into a cartesian image
imC(w1(i,:),w2(i,:),:)=permute(interp2(rr,aa,imP(:,nn(i,:))',r,a,method),PM(i,:));
end
imC(isnan(imC))=0;

27 Mar 2012 Ruth Livingstone

### Ruth Livingstone (view profile)

Hi

Thank you, this code is great, but takes a little too long for my application. After reading the thread I wrote similar code which works ~12 times faster for the PolarToIm section. It doesn't have quite the same functionality but I thought I would share it here in case others are having the same problem

function imC = Polar2Im(imP,W,method)
%Polar2Im turns a polar image (imP) into a cartesian image (imC) of width W
%method can be: '*linear', '*cubic', '*spline', or '*nearest'.
imP(isnan(imP))=0;
w = round(W/2); [M N P]= size(imP);
xy = (1:W-w); [x y] = meshgrid(xy,xy);
n = round(N/4); rr = linspace(1,w,M);
W1 = w:-1:1; PM = [2 1 3;1 2 3;2 1 3;1 2 3];
W2 = w+1:2*w; nn = [1:n; n+1:2*n; 2*n+1:3*n; 3*n+1:N;];
w1 = [W1;W2;W2;W1]; aa = linspace(0,90*pi/180,n);
w2 = [W2;W2;W1;W1]; r = sqrt(x.^2 + y.^2);
a = atan2(y,x); imC= zeros(W,W,P);
for i=1:4 %turn each quarter into a cartesian image
imC(w1(i,:),w2(i,:),:) = permute(interp2(rr,aa,imP(:,nn(i,:))',r,a,method),PM(i,:));
end
imC(isnan(imC))=0;

Comment only
15 Sep 2011 Dhruv Agrawal

### Dhruv Agrawal (view profile)

Is it correct to use different scaling factors for different axes? A circle in an image should transpose as a rectangle in polar image, but this makes it an ellipse. Could take the min of the scaling factors (sx,sy) for both, though, you might end leaving some portion of the image.

18 Oct 2010 Fernando VillafaĆ±e

### Fernando VillafaĆ±e (view profile)

For what serving the matrix (A) and how is obtained?.

function v = interpolate (imR, xR, yR)
xf = floor(xR);
xc = ceil(xR);
yf = floor(yR);
yc = ceil(yR);
if xf == xc & yc == yf
v = imR (xc, yc);
elseif xf == xc
v = imR (xf, yf) + (yR - yf)*(imR (xf, yc) - imR (xf, yf));
elseif yf == yc
v = imR (xf, yf) + (xR - xf)*(imR (xc, yf) - imR (xf, yf));
else
A = [ xf yf xf*yf 1
xf yc xf*yc 1
xc yf xc*yf 1
xc yc xc*yc 1 ];
r = [ imR(xf, yf)
imR(xf, yc)
imR(xc, yf)
imR(xc, yc) ];
a = A\double(r);
w = [xR yR xR*yR 1];
v = w*a;
end

22 Mar 2010 Raphael Attie

### Raphael Attie (view profile)

im2polar : this update will save you 1 order of magnitude of processing time, by vectorizing the code, and using interp2 (can even do better with mex file ba_interp in file exchange) :
Watch out, this might be the transpose of the original output.

rMin=0;
rMax=1;

[Mr Nr] = size(imR); % size of rectangular image
xRc = (Mr+1)/2; % co-ordinates of the center of the image
yRc = (Nr+1)/2;
sx = (Mr-1)/2; % scale factors
sy = (Nr-1)/2;

imP = zeros(M, N);

dr = (rMax - rMin)/(M-1);
dth = 2*pi/N;

r=rMin:dr:rMin+(M-1)*dr;
th=(0:dth:(N-1)*dth)';
[r,th]=meshgrid(r,th);
x=r.*cos(th);
y=r.*sin(th);
xR = x*sx + xRc;
yR = y*sy + yRc;
imP = interp2(imR, xR, yR); %interpolate (imR, xR, yR);

02 Feb 2010 deepak lawrence

### deepak lawrence (view profile)

21 Jan 2010 Anna Saranti

### Anna Saranti (view profile)

Nice work! In PolarToIm.m at line 31 is it perhaps : y = (yi - On)/sy; instead of : y = (yi - On)/sx ?

06 Aug 2009 shoo chen

### shoo chen (view profile)

Thanks.

Comment only
08 Jun 2009 Petter

### Petter (view profile)

Lots of for-loops

09 Apr 2009 Saif

### Saif (view profile)

Very nice work !! thanks !

19 Mar 2008 kinsam yen

thank you. i need this function

24 Jan 2008 Eleni Vasilaki

Thanks a lot.

23 Jan 2008 anitha sumathi

requiring matlab codes for image normalization in iris recognition in image processing.

14 Jan 2008 Danny Luong

Thank you, it works great for me!