matplotlib:如何在图像上绘制一个矩形 [英] matplotlib: how to draw a rectangle on image

查看:4350
本文介绍了matplotlib:如何在图像上绘制一个矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在图像上绘制矩形,如下所示:

  import matplotlib.pyplot作为plt 
来自PIL import Image
import numpy as np
im = np.array(Image.open('dog.png'),dtype = np.uint8)
plt.imshow(im)

我不知道下一步该做什么。

解决方案

您可以添加


How to draw a rectangle on a image, like this:

import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
im = np.array(Image.open('dog.png'), dtype=np.uint8)
plt.imshow(im)

I don't know what to do the next.

解决方案

You can add a Rectangle patch to the matplotlib Axes.

For example (using the image from the tutorial here):

import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
import numpy as np

im = np.array(Image.open('stinkbug.png'), dtype=np.uint8)

# Create figure and axes
fig,ax = plt.subplots(1)

# Display the image
ax.imshow(im)

# Create a Rectangle patch
rect = patches.Rectangle((50,100),40,30,linewidth=1,edgecolor='r',facecolor='none')

# Add the patch to the Axes
ax.add_patch(rect)

plt.show()

这篇关于matplotlib:如何在图像上绘制一个矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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