问题描述
我正在使用Python 2.7。我必须在图片上定义一些感兴趣的区域(AoI)。基本上,我想这样做是在图片的特定部分绘制一个椭圆(或更多),并获取其轮廓的坐标(x; y)。我想将这些坐标保存在文件中,以便以后使用它们以查看我的数据是否在此区域内。
I'm working on Python 2.7. I have to define some Areas of Interest (AoI) on a picture. Basically, I'm trying to do this drawing an ellipse (or more) on a specific part of the picture and to get the coordinates (x; y) of its contour. I want to save these coordinates on a file, in order to use them later to see whether (or not) my data are inside this area.
这是我的代码:
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()
运行代码时,我得到了(这正是我想要的):
When I run the code, I get this (that is exactly what I want):
但是,当我打印路径以进行检查时,我得到以下输出,(我想)与以下内容有关:
However, when I print the path in order to check it, I get the following output, which (I suppose) is exclusively related to the ellipse.
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)有关的椭圆坐标列表。我可能使用了错误的函数,或者图片和椭圆之间不匹配。
However, I need a list of coordinates of the ellipse in relation to the pixel of the picture (1600 X 1200). I'm probably using the wrong function or there is something that does not match between the picture and the ellipse.
我应该获得类似的内容(这是来自
I should obtain something like this (this is an example from a previous experiment):
[ Path(array([[ 1599. , 868.86791294],
[ 1598. , 868.87197971],
[ 1597. , 868.8801087 ],
...,
[ 1597. , 675.30378536],
[ 1598. , 675.31373204],
[ 1599. , 675.31870792]]), None)]
665
有人可以帮助我吗?
预先感谢您,
R
Can anyone help me?Thank you in advance,R
推荐答案
路径数组似乎是一个粗糙的归一化圆-我会忽略它
the path array appears to be a coarse normalized circle - I'd ignore it
您已经有了椭圆信息
ellipse = Ellipse((1000,400),width = 400,height = 100,...)
我只是根据图中的第一个数字做一个sin的cos参数化椭圆 Ellipse((1000,400),width = 400,height = 100
是要明确绘制高分辨率椭圆的中心点和轴长
I would just do a sin, cos paramaterized ellipse based on the 1st few numbers in Ellipse((1000, 400), width=400, height=100
which are the center point and axis lengths if you want to draw a hi res ellipse explicitly
用于成员资格测试(x-x_0)^ 2 / a ^ 2 +(y-y_0)^ 2 / b ^ 2 可能最好,其中
a
, b
是相应 width = 400,height = 100
for a membership test
(x - x_0)^2/a^2 + (y - y_0)^2/b^2 <= 1
is probably best where a
, b
are 1/2 of the respective width=400, height=100
当然,您只需要测试边界矩形内的像素索引
of course you would only need to test pixel indices within the bounding rectangle
这篇关于在图形上绘制椭圆并获取坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!