Python 使用 matplotlib 和 numpy 在图像上绘制圆圈
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34902477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
drawing circles on image with matplotlib and numpy
提问by orkan
I have numpy arrays which hold circle centers.
我有保存圆心的 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()
How can I show circles at the given positions on my image?
如何在图像上的给定位置显示圆圈?
采纳答案by tmdavison
You can do this with the matplotlib.patches.Circlepatch.
你可以用matplotlib.patches.Circle补丁来做到这一点。
For your example, we need to loop through the X and Y arrays, and then create a circle patch for each coordinate.
对于您的示例,我们需要遍历 X 和 Y 数组,然后为每个坐标创建一个圆形补丁。
Here's an example placing circles on top of an image (from the matplotlib.cbook)
这是在图像顶部放置圆圈的示例(来自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()


