将笛卡尔图像转换为极坐标,外观差异 [英] Converting Cartesian image to polar, appearance differences

查看:177
本文介绍了将笛卡尔图像转换为极坐标,外观差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对下面的第一张图片进行极坐标变换,最后是第二张图片。但是我的结果是第三张图片。我有一种感觉,它与我选择的位置有关,但我不确定。

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 choose as my "origin" but am unsure.

  radius = sqrt(width**2 + height**2)
  nheight = int(ceil(radius)/2)
  nwidth = int(ceil(radius/2))
 for y in range(0, height):
   for x in range(0, width):
     t =  int(atan(y/x))
     r = int(sqrt(x**2+y**2)/2)
     color = getColor(getPixel(pic, x, y))
     setColor( getPixel(radial,r,t), color)


推荐答案

有一些差异/错误:


  1. 他们使用图像的中心作为原点

  2. 他们适当地缩放轴。在您的示例中,您正在绘制角度(在0和在您的情况下,pi之间),而不是使用图像的完整高度。

  3. 您使用的是错误的atan函数(在这种情况下,atan2工作得更好:))

  4. 并不是非常重要,但是你不必要地进行了大量的四舍五入,这会使准确性稍微下降,并且会减慢速度。 / li>
  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 in range(0, height):
   dy = y - height/2
   for x in range(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. 我们使用 dx = x - width / 2 as衡量距离中心的距离,类似于 dy 。然后我们在整个计算过程中用这些代替 x y

  2. 我们将 r 满足 0< = r< = sqrt((width / 2)^ 2 +(height / 2)^ 2),我们的 t 最终满足 0< t< = 2 pi 所以,我创建了适当的比例因子来放置 r t 分别沿 x y 轴。

  3. 正常 atan 只能根据渐变进行区分,并且在垂直线附近计算不稳定...相反, atan2 (参见 http://en.wikipedia.org/wiki/Atan2 )解决了这两个问题,并接受(y,x)对给出一个角度。 atan2 返回一个角度 -pi< t< = pi ,所以我们可以找到模数为 2 * math.pi 的余数,以便在<$ c $范围内得到它c> 0< t< = 2pi 准备好进行缩放。

  4. 当新像素设置完毕时,我只在最后舍入。

  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.

有任何问题,请问!

这篇关于将笛卡尔图像转换为极坐标,外观差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆