Monday, 12 September 2011

Plotting polar images in Matlab

I'm currently working with ultrasound images acquired on a polar grid, with equispaced range and angle values. Such images cannot be plotted directly using the image or imagesc command in Matlab, since it requires a x-y grid of coordinates. At first I tried resampling the polar image to a rectangular grid using a 2D interpolator (TriScatteredInterp), which works but takes quite a lot of time. However, at a conference I attended recently I got a very useful tip from Marcelo Matuda from the University of São Paulo: 

Convert the polar coordinates to rectangular coordinates, and plot the image as a surface, seen directly from above. Simple! And fast.

Here is a little example code that illustrates this approach:

%% ---------------------------- %%
close all
clear all

%% Create an example image using peaks function
im = peaks(512);

%% Specify axes for the image (chosen arbitrarily)
[nZ,nX] = size(im); 
theta = ((0:(nX-1))-nX/2)*(0.1*(pi/180)) - pi/2;
rr = (0:(nZ-1))*0.1e-3 + 0.05;

%% Plot image in rectangular coordinates
figure
imagesc(theta*(180/pi), rr*1e3, im)
xlabel('theta [deg]')
ylabel('r [mm]')

%% Create grids and convert polar coordinates to rectangular
[THETA,RR] = meshgrid(theta,rr);
[XX,YY] = pol2cart(THETA,RR);

%% Plot as surface, viewed from above
figure
surf(XX*1e3,YY*1e3,im,'edgecolor','none')
view(0,90)
xlabel('x [mm]')
ylabel('y [mm]')
%% ---------------------------- %%

The code outputs two images, one of the polar plot made using the imagesc command:


... and one with the "proper" polar plot using surf: 


5 comments:

Anonymous said...

This is great. It adapted very nicely to plotting the surface velocities of a circular tank. Nicely done. Thanks.

Anonymous said...

Great example. Works great with radar data. Thanks

Sina said...

Thank you Sir....It was a great help

Anonymous said...

Thank you, it's useful. But I can't understand why it plots only (1:nX-1,1:nZ-1) array, without last column and string. So I simply add one extra column and string to the data I want to plot.

Rachael H said...

Thanks for doing this!

Post a Comment