1

I have been using Peter Kovesi's MatLab functions for machine vision (which are outstanding if you aren't aware of them).
I have been transforming images to polar co-ordinates using the polar transform. The function from Peter Kovesi is named 'PolarTrans' and can be found here - http://www.peterkovesi.com/matlabfns/#syntheticimages

The function beautifully transforms an images into polar co-ordinates. However, I would like the reverse to happen also. Peter Kovesi uses interp2 to transform images, but I can't seem to figure out how to reverse this transform. A requirement of interp2 is that it needs a meshgrid as input.

In short - can you help me reverse the transformation: polar to cartesian. I would like it be accordance with Peter's function - i.e. using the same parameters for coherence.

Dear Swjm, I am posting my reply here because I do not have space in the comments section.
Firstly, thank you very much indeed for your reply. You have shown me how to invert interp2 - something I thought was impossible. This is a huge step forwards. However your code only maps a small segment of the image. Please see the demo code below to understand what I mean.

clc; clear all; close all;

gauss   = fspecial('gauss',64,15);
gauss   = uint8(mat2gray(gauss).*255);
[H,W]   = size(gauss);

pim = polartrans(gauss,64,360);

cim = carttrans(pim,64,64);

subplot(2,2,1);
imagesc(gauss); colormap(jet);
axis off;
title('Image to be Transformed');


subplot(2,2,2);
imagesc(pim); colormap(jet);
axis off;
title('Polar Representation');

subplot(2,2,3);
imagesc(cim);   colormap(jet);
axis off;
title('Back to Cartesian');

subplot(2,2,4);
diff = uint8(gauss) - uint8(cim);
imagesc(diff); colormap(jet);
axis off;
title('Difference Image');
keyserSoze
  • 137
  • 1
  • 9
  • 1
    there is a built-in function `pol2cart` – obchardon Jan 19 '17 at 12:51
  • Thank you for your comment - could you show me how that works in accordance with Peter Kovesi's function? Essentially, given a transformed image, could you show me how to use pol2cart with the output of the function I have linked above. I am familiar with pol2cart, but I need a reverse transformation that preserves the image as much as possible. – keyserSoze Jan 19 '17 at 17:10
  • This person seems to be having similar difficulty and pol2cart was not appropriate. His question is still not solved - http://stackoverflow.com/questions/16062335/convert-image-in-polar-to-cartesian-coordinates – keyserSoze Jan 19 '17 at 17:14
  • Have you searched online for some assistance? I found this and others after a brief search: [link](http://www.mathworks.com/matlabcentral/fileexchange/17933-polar-to-from-rectangular-transform-of-images) – J. McCabe Jan 19 '17 at 18:42
  • Thank you for that link. I am actually currently using that implementation, but it is SO SLOW! Peter Kovesi's function runs in a faction of the time. I have also found there to be some distortions of the image using Im2Polar. Thank you for your input, but I would like to invert the interp2 function as used in the function I originally linked. – keyserSoze Jan 20 '17 at 09:34

1 Answers1

1

I've had a look at Kovesi's code and this code should perform the reverse transformation. It assumes you used the 'full' shape and 'linear' map parameters in polartrans. Note that polar transforms generally lose resolution at low radial values (and gain resolution at high values), so it won't be lossless even if your polar image has the same dimensions as your original image.

function im = carttrans(pim, nrows, ncols, cx, cy)

[rad, theta] = size(pim);      % Dimensions of polar image.

if nargin==3
    cx = ncols/2 + .5;         % Polar coordinate center, should match 
    cy = nrows/2 + .5;         % polartrans. Defaults to same.
end

[X,Y] = meshgrid(1:ncols, 1:nrows);

[TH,R] = cart2pol(X-cx,Y-cy);  % Polar coordinate arrays.
TH(TH<0) = TH(TH<0)+2*pi;      % Put angles in range [0, 2*pi].

rmax = max(R(:));              % Max radius.

xi = TH * (theta+1) / 2*pi;    % Query array for angles.
yi = R * rad / (rmax-1) + 1;   % Query array for radius.

pim = [pim pim(:,1)];          % Add first col to end of polar image.

[pX,pY] = meshgrid(1:theta+1, 1:rad);
im = interp2(pX, pY, pim, xi, yi);
swjm
  • 46
  • 5
  • Thank you so very much for your reply swjm. Your mapping only seems to map a small segment though - I have edited my question above because I did not have space here to paste the code. Any thoughts why this is happening? You have shown me how to invert interp2 and I am grateful - thank you. – keyserSoze Jan 23 '17 at 13:03