python用函数绘制椭圆,在图形上绘制椭圆并获取坐标

本文介绍如何使用Python的Matplotlib库在图片上绘制椭圆,并获取该椭圆轮廓的像素坐标,以便后续进行数据点是否位于该区域内的判断。

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.

This is my code:

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):

vvIWN.png

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))

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

Can anyone help me?

Thank you in advance,

R

解决方案

the path array appears to be a coarse normalized circle - I'd ignore it

you have the ellipse info already

ellipse = 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=100which are the center point and axis lengths if you want to draw a hi res ellipse explicitly

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

Python中,可以使用matplotlib库来绘制坐标图形,包括椭圆函数。具体步骤如下: 1. 导入matplotlib库和numpy库: ```python import matplotlib.pyplot as plt import numpy as np ``` 2. 定义椭圆函数: ```python def ellipse(theta, a, b): r = (a * b) / np.sqrt((b * np.cos(theta))**2 + (a * np.sin(theta))**2) return r ``` 其中,theta是极角,a和b分别是椭圆的长轴和短轴。 3. 设置极坐标轴: ```python fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) ``` 4. 绘制椭圆: ```python theta = np.linspace(0, 2*np.pi, 1000) a, b = 2, 1 # 椭圆长轴和短轴 r = ellipse(theta, a, b) ax.plot(theta, r) ``` 5. 设置图形属性: ```python ax.set_rmax(2) # 设置极径最大值 ax.set_rticks([0.5, 1, 1.5, 2]) # 设置极径标签 ax.set_rlabel_position(-22.5) # 设置极径标签位置 ax.grid(True) # 显示网格 ax.set_title("Ellipse in Polar Coordinates", va='bottom') # 设置标题 plt.show() # 显示图形 ``` 完整代码如下: ```python import matplotlib.pyplot as plt import numpy as np def ellipse(theta, a, b): r = (a * b) / np.sqrt((b * np.cos(theta))**2 + (a * np.sin(theta))**2) return r fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) theta = np.linspace(0, 2*np.pi, 1000) a, b = 2, 1 # 椭圆长轴和短轴 r = ellipse(theta, a, b) ax.plot(theta, r) ax.set_rmax(2) # 设置极径最大值 ax.set_rticks([0.5, 1, 1.5, 2]) # 设置极径标签 ax.set_rlabel_position(-22.5) # 设置极径标签位置 ax.grid(True) # 显示网格 ax.set_title("Ellipse in Polar Coordinates", va='bottom') # 设置标题 plt.show() # 显示图形 ``` 运行以上代码,即可在极坐标绘制椭圆函数图形
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值