我试着做下面的第一个图像上的极坐标变换,并与第二告终。 但是我的结果是第三图像。 我有一种感觉它与我选择什么样的位置,作为我的“原点”但我不确定这样做。
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)
有一些分歧/错误:
- 他们用图像的中心为原点
- 他们适当地缩放的轴。 在你的榜样,你绘制你的角度(从0到你的情况,PI),而不是利用图象的最大高度。
- 您使用了错误的ATAN函数(ATAN2工作在这种情况下:)好了很多)
- 不惊人的重要,但你四舍五入不必要颇多,其中抛出了准确一点,可以慢下来。
这是我的合并建议的改进的代码。 这不是大规模高效,但它应该有希望的工作:)
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)
特别是,它修复了以下几个方面的上述问题:
- 我们使用
dx = x - width / 2
作为距离从中心的度量,并类似地与dy
。 然后,我们在使用这些替代的x
, y
在整个计算。 - 我们将我们的
r
满足0 <= r <= sqrt( (width/2)^2 +(height/2)^2 )
并且我们的t
最终满足0 < t <= 2 pi
所以,我创建适当的规模因素把r
和t
沿x
和y
分别轴。 - 普通
atan
只能区分基于梯度,并接近垂直线计算不稳定......相反, atan2
(见http://en.wikipedia.org/wiki/Atan2 )解决了这两个问题,并接受(y,x)
对得到的角度。 atan2
返回角度-pi < t <= pi
,所以我们可以找到其余的模2 * math.pi
到它得到它在范围0 < t <= 2pi
准备缩放。 - 我只四舍五入到了最后,当新的像素置位。
如有任何问题,只是问!