Skip to content Skip to sidebar Skip to footer

Converting Cartesian Image To Polar, Appearance Differences

I'm trying to do a polar transform on the first image below and end up with the second. However my result is the third image. I have a feeling it has to do with what location I cho

Solution 1:

There are a few differences / errors:

  1. They use the centre of the image as the origin
  2. They scale the axis appropriately. In your example, you're plotting your angle (between 0 and in your case, pi), instead of utilising the full height of the image.
  3. You're using the wrong atan function (atan2 works a lot better in this situation :))
  4. Not amazingly important, but you're rounding unnecessarily quite a lot, which throws off accuracy a little and can slow things down.

This is the code combining my suggested improvements. It's not massively efficient, but it should hopefully work :)

  maxradius = sqrt(width**2 + height**2)/2
  rscale = width / maxradius
  tscale = height / (2*math.pi)
  for y inrange(0, height):
   dy = y - height/2for x inrange(0, width):
     dx = x - width/2
     t =  atan2(dy,dx)%(2*math.pi)
     r = sqrt(dx**2+dy**2)
     color = getColor(getPixel(pic, x, y))
     setColor( getPixel(radial,int(r*rscale),int(t*tscale)), color)

In particular, it fixes the above problems in the following ways:

  1. We use dx = x - width / 2 as a measure of distance from the centre, and similarly with dy. We then use these in replace of x, y throughout the computation.
  2. We will have our r satisfying 0 <= r <= sqrt( (width/2)^2 +(height/2)^2 ), and our t eventually satisfying 0 < t <= 2 pi so, I create the appropriate scale factors to put r and t along the x and y axes respectively.
  3. Normal atan can only distinguish based on gradients, and is computationally unstable near vertical lines... Instead, atan2 (see http://en.wikipedia.org/wiki/Atan2) solves both problems, and accepts (y,x) pairs to give an angle. atan2 returns an angle -pi < t <= pi, so we can find the remainder modulo 2 * math.pi to it to get it in the range 0 < t <= 2pi ready for scaling.
  4. I've only rounded at the end, when the new pixels get set.

Any questions, just ask!

Post a Comment for "Converting Cartesian Image To Polar, Appearance Differences"