【问题标题】:Draw an ellipse on a figure and get coordinates_Python在图形上绘制椭圆并获取坐标_Python
【发布时间】:2018-02-07 18:19:10
【问题描述】:

我正在开发 Python 2.7。我必须在图片上定义一些感兴趣的区域 (AoI)。基本上,我试图在图片的特定部分绘制一个椭圆(或更多)并获取其轮廓的坐标(x; y)。我想将这些坐标保存在一个文件中,以便以后使用它们来查看我的数据是否(或不)在这个区域内。

这是我的代码:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Ellipse, Circle
from matplotlib.path import Path

# Get an example image

img = imread('sposa.png')

# Create a figure. Equal aspect so circles look circular
fig,ax = plt.subplots(1)
ax.set_aspect('equal')

# Show the image
ax.imshow(img)
ax.set_xlim(0,1600)
ax.set_ylim(0,1200)
# Now, loop through coord arrays, and create a circle at each x,y pair
ellipse = Ellipse((1000, 400), width=400, height=100, edgecolor='white',facecolor='none',linewidth=2)

ax.add_patch(ellipse)
path = ellipse.get_path()

# Show the image
plt.show()

当我运行代码时,我得到了这个(这正是我想要的):

但是,当我打印路径以检查它时,我得到以下输出,(我想)它与椭圆完全相关。

Path(array([[ 0.        , -1.        ],
   [ 0.2652031 , -1.        ],
   [ 0.51957987, -0.89463369],
   [ 0.70710678, -0.70710678],
   [ 0.89463369, -0.51957987],
   [ 1.        , -0.2652031 ],
   [ 1.        ,  0.        ],
   [ 1.        ,  0.2652031 ],
   [ 0.89463369,  0.51957987],
   [ 0.70710678,  0.70710678],
   [ 0.51957987,  0.89463369],
   [ 0.2652031 ,  1.        ],
   [ 0.        ,  1.        ],
   [-0.2652031 ,  1.        ],
   [-0.51957987,  0.89463369],
   [-0.70710678,  0.70710678],
   [-0.89463369,  0.51957987],
   [-1.        ,  0.2652031 ],
   [-1.        ,  0.        ],
   [-1.        , -0.2652031 ],
   [-0.89463369, -0.51957987],
   [-0.70710678, -0.70710678],
   [-0.51957987, -0.89463369],
   [-0.2652031 , -1.        ],
   [ 0.        , -1.        ],
   [ 0.        , -1.        ]]), array([ 1,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
    4,  4,  4,  4,  4,  4,  4,  4, 79], dtype=uint8))

但是,我需要一个椭圆相对于图片像素 (1600 X 1200) 的坐标列表。我可能使用了错误的函数,或者图片和椭圆之间存在不匹配的内容。

我应该得到这样的东西(这是以前实验的一个例子):

[ Path(array([[ 1599.        ,   868.86791294],
   [ 1598.        ,   868.87197971],
   [ 1597.        ,   868.8801087 ],
   ..., 
   [ 1597.        ,   675.30378536],
   [ 1598.        ,   675.31373204],
   [ 1599.        ,   675.31870792]]), None)]
665 

谁能帮帮我? 先感谢您, 回复

【问题讨论】:

    标签: python get coordinates roi


    【解决方案1】:

    您应该使用Ellipse.get_path().vertices,但它不在正确的坐标系中。要对其进行转换,请将ellipse.get_patch_transform().transform 应用于它。请参阅下面的工作示例

    import matplotlib.pyplot as plt
    from matplotlib.patches import Ellipse
    from matplotlib.path import Path
    from matplotlib.patches import PathPatch
    
    img = plt.imread("image.jpg")
    
    fig, ax = plt.subplots(1)
    ax.set_aspect('equal')
    
    ax.imshow(img)
    
    # Create the base ellipse
    ellipse = Ellipse((300, 300), width=400, height=100,
                      edgecolor='white', facecolor='none', linewidth=2)
    
    # Get the path
    path = ellipse.get_path()
    # Get the list of path vertices
    vertices = path.vertices.copy()
    # Transform the vertices so that they have the correct coordinates
    vertices = ellipse.get_patch_transform().transform(vertices)
    
    # You can then save the vertices array to a file: csv, pickle... It's up to you
    
    plt.show()
    

    【讨论】:

    • 完全正确!谢谢你!
    【解决方案2】:

    路径数组似乎是一个粗略的标准化圆 - 我会忽略它

    你已经有了椭圆信息
    ellipse = Ellipse((1000, 400), width=400, height=100, ...)

    如果你想明确地绘制高分辨率椭圆,我会根据Ellipse((1000, 400), width=400, height=100 中的第一个数字来做一个罪恶的 cos 参数化椭圆

    对于会员测试(x - x_0)^2/a^2 + (y - y_0)^2/b^2 <= 1 可能是最好的a, b 是各自width=400, height=100的1/2

    当然,您只需要测试边界矩形内的像素索引

    【讨论】:

    • 谢谢。我不确定您的解决方案如何帮助我找到椭圆坐标。我试图实现它,但没有成功。你能举个例子吗?
    猜你喜欢
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    相关资源
    最近更新 更多