gpt4 book ai didi

python - 使用 Matplotlib 和 NumPy 在图像上绘制圆圈

转载 作者:太空狗 更新时间:2023-10-29 17:27:20 24 4
gpt4 key购买 nike

我有包含圆心的 NumPy 数组。

import matplotlib.pylab as plt
import numpy as np
npX = np.asarray(X)
npY = np.asarray(Y)
plt.imshow(img)
// TO-DO
plt.show()

如何在图片的给定位置显示圆圈?

最佳答案

您可以使用 matplotlib.patches.Circle 补丁来做到这一点。

对于您的示例,我们需要遍历 X 和 Y 数组,然后为每个坐标创建一个圆形面片。

这是一个将圆圈放在图像顶部的示例(来自 matplotlib.cbook)

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle

# Get an example image
import matplotlib.cbook as cbook
image_file = cbook.get_sample_data('grace_hopper.png')
img = plt.imread(image_file)

# Make some example data
x = np.random.rand(5)*img.shape[1]
y = np.random.rand(5)*img.shape[0]

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

# Show the image
ax.imshow(img)

# Now, loop through coord arrays, and create a circle at each x,y pair
for xx,yy in zip(x,y):
circ = Circle((xx,yy),50)
ax.add_patch(circ)

# Show the image
plt.show()

enter image description here

关于python - 使用 Matplotlib 和 NumPy 在图像上绘制圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34902477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com