【问题标题】:Image to polar co-ordinates图像到极坐标
【发布时间】:2018-01-02 04:30:40
【问题描述】:

我在将图像转换为极坐标时遇到问题。在 Photoshop 中这很简单 :) 所以这对我来说是一个新领域。

我有以下图片:

它最终应该是这样的:

我已经看过here 并且我已经掌握了基础知识,但仍然对方形圆圈有点困惑:

import math
from PIL import Image, ImageDraw


# image size
imgX = 200
imgY = 200

image = Image.new("RGB", (imgX, imgY))
draw = ImageDraw.Draw(image)

#fill with white first
colour = "#ffffff"
box = [0,0, imgX, imgY]
image.paste(colour, box)

# draw line near base
draw.line((0,180, 200, 180), fill="#FF0000", width=2)

print "Line done!"
image.save("line.png", "PNG")

# there's got to be a way to get the current image
# without having to open it up again

im = Image.open("line.png")
rgb_im = im.convert("RGB")

# rectangle to polar coordinates
maxradius = math.sqrt(imgX**2 + imgY**2)/2
rscale = imgX / maxradius
tscale = imgY / (2*math.pi)

for y in range(0, imgY):
    dy = y - imgY/2

    for x in range(0, imgX):
        dx = x - imgX/2
        t = math.atan2(dy,dx)%(2*math.pi)
        r = math.sqrt(dx**2+dy**2)

        r, g, b = rgb_im.getpixel((x, y))
        # this is where it goes wrong
        col = b * 65536 + g * 256 + r
        image.putpixel((x, y), b * 65536 + g * 256 + r

image.save("polar.png", "PNG")

我几乎对如何重绘图像有点困惑。还有一个警告:由于管理限制,我想避免使用像 Numpy 这样的外部库。

【问题讨论】:

    标签: image python-2.7 python-imaging-library polar-coordinates


    【解决方案1】:

    以下代码对我有用。主要变化:

    • line_imagecircle_image 创建了单独的变量。没有理由重新加载图像,您可以重复使用line_image...

    • tr 将为您提供要在线条图像中访问的图像坐标,用于圆图像中每个对应的 xy。它们已经存在,您只需有效地将它们用作索引来获取像素颜色。

    • rscaletscale 应用到rt,使2×pi 对应于图像的右边缘。您肯定需要更改 r 的比例,以便能够在输出中看到这个圆圈,或者将线画得更接近顶部(例如,在第 100 行而不是 180 行)。

      李>
    • 我还为行图像访问添加了边界检查。

    ```

    import math
    from PIL import Image, ImageDraw     
    # image size
    imgX = 200
    imgY = 200
    
    line_image = Image.new("RGB", (imgX, imgY))
    draw = ImageDraw.Draw(line_image)
    
    #fill with white first
    colour = "#ffffff"
    box = [0,0, imgX, imgY]
    line_image.paste(colour, box)
    
    # draw line near base
    draw.line((0,180, 200, 180), fill="#FF0000", width=2)
    
    print "Line done!"
    line_image.save("line.png", "PNG")
    
    circle_image = Image.new("RGB", (imgX, imgY))
    
    # rectangle to polar coordinates
    maxradius = math.sqrt(imgX**2 + imgY**2)/2
    rscale = imgX / maxradius
    tscale = imgY / (2*math.pi)
    
    for y in range(0, imgY):
        dy = y - imgY/2
    
        for x in range(0, imgX):
            dx = x - imgX/2
            t = math.atan2(dy,dx)%(2*math.pi)*tscale
            r = math.sqrt(dx**2+dy**2)*rscale
    
            if 0<= t < imgX and 0 <= r < imgY:
                r, g, b = line_image.getpixel((t, r))
                # this is where it goes wrong
                col = b * 65536 + g * 256 + r
                circle_image.putpixel((x, y), col)
    
    circle_image.save("polar.png", "PNG")
    

    ```

    【讨论】:

    • 这比我所希望的要好!谢谢你。现在只需要计算比例因子
    猜你喜欢
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    相关资源
    最近更新 更多