Mirkules Posted November 6, 2007 Share Posted November 6, 2007 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 https://forums.phpfreaks.com/topic/76147-transform-image-to-cartesian-from-polar-coordinates/ Share on other sites More sharing options...
btherl Posted November 6, 2007 Share Posted November 6, 2007 What value does tan($j/$i) represent? Link to comment https://forums.phpfreaks.com/topic/76147-transform-image-to-cartesian-from-polar-coordinates/#findComment-385398 Share on other sites More sharing options...
Mirkules Posted November 6, 2007 Author Share Posted November 6, 2007 $j is the y coordinate of the image's pixel, while $i is the x coordinate. $t = tan($j/$i) is the angle of the line segment specified by (0,0), ($i,$j) with respect to the x-axis (polar axis). Link to comment https://forums.phpfreaks.com/topic/76147-transform-image-to-cartesian-from-polar-coordinates/#findComment-385484 Share on other sites More sharing options...
btherl Posted November 6, 2007 Share Posted November 6, 2007 I don't quite get that. Let's say $j = pi/2 and $i = 1. That point doesn't exist in the image but let's use it anyway. tan($j/$i) = tan((pi/2)/1) = tan(pi/2) = ??? Link to comment https://forums.phpfreaks.com/topic/76147-transform-image-to-cartesian-from-polar-coordinates/#findComment-385492 Share on other sites More sharing options...
Mirkules Posted November 6, 2007 Author Share Posted November 6, 2007 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 https://forums.phpfreaks.com/topic/76147-transform-image-to-cartesian-from-polar-coordinates/#findComment-385502 Share on other sites More sharing options...
Barand Posted November 6, 2007 Share Posted November 6, 2007 If you plot x = r cos(t), y = r sin(t) then aren't you just redoing the polar plot? x = t, y = rt should give a straight line in the form y = Mx Link to comment https://forums.phpfreaks.com/topic/76147-transform-image-to-cartesian-from-polar-coordinates/#findComment-385555 Share on other sites More sharing options...
Mirkules Posted November 6, 2007 Author Share Posted November 6, 2007 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 https://forums.phpfreaks.com/topic/76147-transform-image-to-cartesian-from-polar-coordinates/#findComment-386099 Share on other sites More sharing options...
Barand Posted November 6, 2007 Share Posted November 6, 2007 Perhaps store results in arrays. Get min and max values and scale the axes accordingly. Link to comment https://forums.phpfreaks.com/topic/76147-transform-image-to-cartesian-from-polar-coordinates/#findComment-386111 Share on other sites More sharing options...
Mirkules Posted November 6, 2007 Author Share Posted November 6, 2007 I'm not exactly sure what you mean by "scale the axes"? How could I do that in an image? Link to comment https://forums.phpfreaks.com/topic/76147-transform-image-to-cartesian-from-polar-coordinates/#findComment-386114 Share on other sites More sharing options...
Barand Posted November 6, 2007 Share Posted November 6, 2007 [pre] | Ymax | | -------------+------------- Xmin | Xmax | | Ymin [/pre] The values in polar mode are different from those required in cartesian mode Link to comment https://forums.phpfreaks.com/topic/76147-transform-image-to-cartesian-from-polar-coordinates/#findComment-386196 Share on other sites More sharing options...
Mirkules Posted November 7, 2007 Author Share Posted November 7, 2007 Yeah, I understand that I need to convert the X coordinate and Y coordinate so that they are between the xMin/xMax and yMin/yMax bounds. What I don't think I understand is what is the function to get a point like x=1.5674243 between the xMin and xMax? Link to comment https://forums.phpfreaks.com/topic/76147-transform-image-to-cartesian-from-polar-coordinates/#findComment-386504 Share on other sites More sharing options...
btherl Posted November 7, 2007 Share Posted November 7, 2007 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 https://forums.phpfreaks.com/topic/76147-transform-image-to-cartesian-from-polar-coordinates/#findComment-386553 Share on other sites More sharing options...
Mirkules Posted November 8, 2007 Author Share Posted November 8, 2007 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 https://forums.phpfreaks.com/topic/76147-transform-image-to-cartesian-from-polar-coordinates/#findComment-387215 Share on other sites More sharing options...
Barand Posted November 8, 2007 Share Posted November 8, 2007 Your result of a horizontal straight line is the result of y = constant rather then y = mx, which should give a sloping line. BTW, Is your test circle centred on the origin, the polar coords calculation assumes that it is. Link to comment https://forums.phpfreaks.com/topic/76147-transform-image-to-cartesian-from-polar-coordinates/#findComment-387228 Share on other sites More sharing options...
btherl Posted November 8, 2007 Share Posted November 8, 2007 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 https://forums.phpfreaks.com/topic/76147-transform-image-to-cartesian-from-polar-coordinates/#findComment-387397 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.