Jump to content

Transform Image to Cartesian from Polar Coordinates


Mirkules

Recommended Posts

Hi All,

 

I am trying to create an image transform that will plot all images in the cartesian coordinate system from a polar system.  So far, I haven't had luck. Images are coming out distorted.  My input is a picture of a perfect circle, and the expected output is a straight line.  The circle actually gets pretty distorted but it is nowhere near the straight line that I'm expecting.  I will be converting other pictures to cartesian coordinates later, so I'm only using the circle as baseline.  I am not sure if it is a problem with math or if it is a bug in the program.

 

All the images are Black and White ONLY (this is part of a bigger project where the image is converted to only B&W prior to hitting this portion of the code), so colors are not a concern (which is why, as you will see in the code, only the red channel is tested).

 

The flow of the program is:

for every point (i, j) in the original image $im:

 

if the pixel is black, find the radius and angle.

convert radius and angle to cartesian coordinates using formula x = r cos (angle) and y = r sin (angle)

plot a pixel in the new image at x,y  (if x or y is less than 0, "wrap" around)

 

I've attached the code.  If anyone has patience to look through it, and if you have any idea what the problem might be, I would be greatful.

 

Thanks

mk

 

function translate_polar_to_cartesian($im)
{
$PI =  3.14159265;

$w = 1000;
$h = 1000;
$cart_im = imagecreate($w,$h);

$im_w = imagesx($im);
$im_h = imagesy($im);

$back = imagecolorallocate($cart_im, 255, 255, 255);
$front = imagecolorallocate($cart_im, 0, 0, 0);
for ($i = 0; $i < $im_w; $i++)
{
	for ($j = 0; $j < $im_h; $j++)
	{
		//echo "i: $i, new x: $new_x<br/>";

		$im_rgb = imagecolorat($im, $i, $j);
		$c = $back;
		$red = ($im_rgb >> 16) & 0xFF;
		if ($red < 255)
		{
			$c = $front;
			$r = sqrt($i*$i + $j*$j);

			if ($i != 0)
				$t = tan($j/$i);
			else
				$t = $PI/2;

			$x = $r * cos($t);
			$y = $r * sin($t);
			if ($x < 0)
				$x = imagesx($im) - $x;

			if ($y < 0)
				$y = imagesy($im) - $y;

			imagesetpixel( $cart_im, $x, $y, $c);
			//echo "r: $r, t: $t<br/>";
		}	
	}	
}
return $cart_im;	
}

Link to comment
Share on other sites

You are right, it makes no sense because it should be arctan.

 

Still, I can't make it work. I get the input image back now, only lightly distorted. Still not the desired output.

 

It could be the way I am displaying the image at the end. I found this: http://www.codeguru.com/forum/archive/index.php/t-278733.html

 

I rewrote the code to look more like that, but still no luck. I think it might be the way I am displaying the coordinates at the very end when I obtain radius and angle. What do you think?

Link to comment
Share on other sites

Thank you -- you are right, I was replotting the image in polar space.  This brings me a step closer.

 

Now it's an issue of displaying floating point numbers. Here's a sample of the points that are being plotted:

 

x: 0.1624891092172, y: 61.814237842102

x: 0.14648417839356, y: 61.660360037872

x: 0.13040330788913, y: 61.522353661088

x: 0, y: 61

x: -0.016391974308005, y: 61.008196170679

x: -0.032775144404076, y: 61.032778078669

x: -0.099668652491162, y: 60.299253726725

 

As you can see, I am in floating point hell.  The issue isn't just multiplying by 100 (or 10), rounding off and setting the pixel.  The problem is that when I do that, I get a lot of empty space between the dots, resulting in a non-continuous image.  I attached my input and output images so you can see what I mean. I'm not sure if this is just a function of converting from polar to rectangular space, or if it's something wrong with the way I'm displaying the images (I think it's the latter). I am trying to mimic this: http://microship.com/resources/rotation-scaling.html

 

I'm not sure if that's possible at all.

 

Anyway, thank you very much for your help, I appreciate it!!

 

[attachment deleted by admin]

Link to comment
Share on other sites

[pre]

            | Ymax

            |

            |

-------------+-------------                                         

Xmin        |        Xmax

            |

            | Ymin

[/pre]

 

The values in polar mode are different from those required in cartesian mode

Link to comment
Share on other sites

Let's say your points range from -50 to 100, and your image is 300 pixels wide.

 

$imageWidth = 300;
$xMin = -50;
$xMax = 100;
$xRange = $xMax - $xMin; # That's 150
$xShifted = $xOriginal - $xMin; # Shift so it has a 0 offset
$xScaled = $xShifted * $imageWidth / $xRange; # And scale it to the image width, 300

 

Is that what you were after?

Link to comment
Share on other sites

btherl, thank you for the scaling. Even though it's not directly what I was asking for in this post, I would have had to tackle that problem eventually.

 

The problem I'm having is that I cannot connect the dots. If you look at the images I attached, you will see the original smiley face, the version of the smiley translated into polar with my script and a version done with Photoshop's Filter->Distort->Polar Coordinates.

 

What I'm really trying to achieve is continuity, just like PS. Any deas how PS may be doing that?

 

[attachment deleted by admin]

Link to comment
Share on other sites

Since your original image is not continuous, the only way to make a continuous output would be to extrapolate a bit.  Maybe you could assume that for all input pixels i1 and i2 which are adjacent in the input, you should draw a line in the output from f(i1) to f(i2).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.