用matplotlib和numpy在图像上画圆

2024-10-01 19:22:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有核阵列,它有圆心。

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

如何在图像上的给定位置显示圆?


Tags: toimportnumpyimgmatplotlibasnpplt
1条回答
网友
1楼 · 发布于 2024-10-01 19:22:28

您可以使用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

相关问题 更多 >

    热门问题