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

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

问题描述

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

将 matplotlib.pyplot 导入为 plt从 PIL 导入图像将 numpy 导入为 npim = np.array(Image.open('dog.png'), dtype=np.uint8)plt.imshow(im)

我不知道如何继续.

解决方案

您可以添加一个

How to draw a rectangle on an 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 how to proceed.

解决方案

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

im = Image.open('stinkbug.png')

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

# 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天全站免登陆