Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Image from polar to cartesian gives only one quadrant

Subject: Image from polar to cartesian gives only one quadrant

From: Sal

Date: 1 May, 2012 01:02:06

Message: 1 of 6

Hello,

I'm converting an image from Polar to Cartesian, but I'm only getting one quadrant as a result.

Could anyone please tell me what is wrong with my code? Thanks in advance!
(xStart = 0, xEnd = width of output image, numX = endX, numY = height of output image, centerX,centerY = center of output image)

function[cartIm, xValues, yValues] = createCartImage(polarInputImage, ...
                                                     xStart, ...
                                                     xEnd, ...
                                                     numX, ...
                                                     numY)
[numRows, numCols] = size(polarInputImage);

cartIm = zeros(numX, numY);
xStep = (xEnd-xStart)/numX;
xValues = xStart + (0:numX-1)*xStep;

yStart = 0;
yStep = 1;
yValues = yStart + (0:numY-1)*yStep;

% compute corresponding Polar coordinates
[X,Y] = meshgrid(xValues, yValues); %create a grid of X and Y values
RD = sqrt(X.^2 + Y.^2);


for i = 1:numY
    for j = 1:numX
        x = X(i,j);
        y = Y(i,j);
        TH(i,j) = atan(y/x) * (180/pi);
    end
end

RD = round(RD);
TH = round(TH);

% create Cartesian image
for y=1:numY %(rows)
    for x=1:numX %(cols)
        row = RD(y,x); %RD(c,r)
        col = TH(y,x);
        if (row > 0 && row < numRows && col > 0 && col < numCols)
            xx = x + centerX;
            yy = y + centerY;
            cartIm(xx,yy) = polarInputImage(row,col);
            if(polarInputImage(row,col) > 0)
                counter = counter + 1;
            end
        end
    end
end
imshow(cartIm);

Subject: Image from polar to cartesian gives only one quadrant

From: Roger Stafford

Date: 1 May, 2012 02:29:11

Message: 2 of 6

"Sal" wrote in message <jnncme$9b8$1@newscl01ah.mathworks.com>...
> I'm converting an image from Polar to Cartesian, but I'm only getting one quadrant as a result.
- - - - - - - - - - - - -
  As you have defined them, X and Y are always non-negative. For that reason the 'TH' angle you have computed using 'atan' will always lie between 0 and pi/2 (90 degrees,) which is why you are getting only the first quadrant.

  You need to change your definition of the X, Y mesh so that its values range over values that are equally positive and negative. That is, the (0,0) point in the mesh should occur at the center of the mesh. Moreover, you need to use 'atan2', not 'atan'. The latter will only range from -pi/2 to +pi/2, whereas 'atan2' ranges the full circle from -pi to +pi. (You should read its documentation to use it correctly.) That way you will be able to get all four quadrants. (I am assuming you want the origin of the polar coordinates to coincide with the center of the image.)

  Also, whatever it takes, you need to have the two step sizes, xStep and yStep, be equal. Otherwise the scaling will be off and you will have circles changing into ellipses and right angles becoming acute or obtuse.

Roger Stafford

Subject: Image from polar to cartesian gives only one quadrant

From: Sal

Date: 1 May, 2012 03:57:06

Message: 3 of 6

"Roger Stafford" wrote in message <jnnhpn$s45$1@newscl01ah.mathworks.com>...
> "Sal" wrote in message <jnncme$9b8$1@newscl01ah.mathworks.com>...
> > I'm converting an image from Polar to Cartesian, but I'm only getting one quadrant as a result.
> - - - - - - - - - - - - -
> As you have defined them, X and Y are always non-negative. For that reason the 'TH' angle you have computed using 'atan' will always lie between 0 and pi/2 (90 degrees,) which is why you are getting only the first quadrant.
>
> You need to change your definition of the X, Y mesh so that its values range over values that are equally positive and negative. That is, the (0,0) point in the mesh should occur at the center of the mesh. Moreover, you need to use 'atan2', not 'atan'. The latter will only range from -pi/2 to +pi/2, whereas 'atan2' ranges the full circle from -pi to +pi. (You should read its documentation to use it correctly.) That way you will be able to get all four quadrants. (I am assuming you want the origin of the polar coordinates to coincide with the center of the image.)
>
> Also, whatever it takes, you need to have the two step sizes, xStep and yStep, be equal. Otherwise the scaling will be off and you will have circles changing into ellipses and right angles becoming acute or obtuse.
>
> Roger Stafford

Thank you Roger!

I had in fact tried using equally positive values for the X, Y mesh but I couldn't understand how to plot those negative coordinates to the final image. It will never satisfy the 'if' condition in the 'for' loop. How do I get around that problem?

Subject: Image from polar to cartesian gives only two quadrants

From: Sal

Date: 1 May, 2012 16:21:07

Message: 4 of 6

"Sal" wrote in message <jnnmui$idd$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <jnnhpn$s45$1@newscl01ah.mathworks.com>...
> > "Sal" wrote in message <jnncme$9b8$1@newscl01ah.mathworks.com>...
> > > I'm converting an image from Polar to Cartesian, but I'm only getting one quadrant as a result.
> > - - - - - - - - - - - - -
> > As you have defined them, X and Y are always non-negative. For that reason the 'TH' angle you have computed using 'atan' will always lie between 0 and pi/2 (90 degrees,) which is why you are getting only the first quadrant.
> >
> > You need to change your definition of the X, Y mesh so that its values range over values that are equally positive and negative. That is, the (0,0) point in the mesh should occur at the center of the mesh. Moreover, you need to use 'atan2', not 'atan'. The latter will only range from -pi/2 to +pi/2, whereas 'atan2' ranges the full circle from -pi to +pi. (You should read its documentation to use it correctly.) That way you will be able to get all four quadrants. (I am assuming you want the origin of the polar coordinates to coincide with the center of the image.)


Okay so I implemented what you said. I changed two things, the X,Y mesh now goes equally positive and negative, and I changed the function for calculating the angle from 'atan' to 'atan2'.

Now, however I am only getting two quadrants in the output image. Should there be a condition while calculating 'theta' that if it occurs in a specific quadrant, I have to add 180 degrees to it?

Subject: Image from polar to cartesian gives only two quadrants

From: Roger Stafford

Date: 1 May, 2012 17:17:07

Message: 5 of 6

"Sal" wrote in message <jnp2hj$h5t$1@newscl01ah.mathworks.com>...
> Okay so I implemented what you said. I changed two things, the X,Y mesh now goes equally positive and negative, and I changed the function for calculating the angle from 'atan' to 'atan2'.
>
> Now, however I am only getting two quadrants in the output image. Should there be a condition while calculating 'theta' that if it occurs in a specific quadrant, I have to add 180 degrees to it?
- - - - - - - - -
  Please show us your revised code. Very likely your trouble lies with the negative values given out by 'atan2'. Remember, I told you that 'atan2' ranges full circle from -pi to +pi. These need to be properly transformed into the entirely positive-valued columns of polarInputImage.

Roger Stafford

Subject: Image from polar to cartesian gives only two quadrants

From: Sal

Date: 1 May, 2012 17:49:06

Message: 6 of 6

"Roger Stafford" wrote in message <jnp5qj$24n$1@newscl01ah.mathworks.com>...
> Please show us your revised code. Very likely your trouble lies with the negative values given out by 'atan2'. Remember, I told you that 'atan2' ranges full circle from -pi to +pi. These need to be properly transformed into the entirely positive-valued columns of polarInputImage.
>
> Roger Stafford

-----------------------

I got the solution now. I just added 180 degrees to each value of 'theta' so that they go over the full range of 0 to 360 degrees.

Thanks a lot for your help!!

function[cartIm, xValues, yValues] = createCartImage(polarInputImage, ...
                                                     xStart, ...
                                                     xEnd, ...
                                                     numX, ...
                                                     numY)
[numRows, numCols] = size(polarInputImage);

cartIm = zeros(numX, numY);
xStep = (xEnd-xStart)/numX;
xValues = xStart + (0:numX-1)*xStep;

yStart = -360;
yStep = 1;
yValues = yStart + (0:numY-1)*yStep;

% compute corresponding Polar coordinates
[X,Y] = meshgrid(xValues, yValues); %create a grid of X and Y values
RD = sqrt(X.^2 + Y.^2);
TH = atan2(Y,X).*(180/pi) + 180;

RD = round(RD);
TH = round(TH);

% create cartesian image
%counter = 1;
for y=1:numY %(rows)
    for x=1:numX %(cols)
        row = RD(y,x);
        col = TH(y,x);
       
        if (row > 0 && row < numRows && col > 0 && col < numCols)
            xx = x;% + 240;
            yy = y;% + 360;
            cartIm(xx,yy) = polarInputImage(row,col);
        end
    end
end

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us