使用Matplotlib和NumPy在图像上绘制圆圈 [英] Drawing circles on image with Matplotlib and NumPy

查看:138
本文介绍了使用Matplotlib和NumPy在图像上绘制圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有NumPy个数组,它们保存圆心.

I have NumPy arrays which hold circle centers.

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

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

How can I show circles at the given positions on my image?

推荐答案

您可以使用 matplotlib.patches.Circle 补丁进行此操作.

You can do this with the matplotlib.patches.Circle patch.

以您的示例为例,我们需要遍历X和Y数组,然后为每个坐标创建一个圆形补丁.

For your example, we need to loop through the X and Y arrays, and then create a circle patch for each coordinate.

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

Here's an example placing circles on top of an image (from the 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()

这篇关于使用Matplotlib和NumPy在图像上绘制圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆