Python matplotlib:如何在图像上绘制矩形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37435369/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:24:17  来源:igfitidea点击:

matplotlib: how to draw a rectangle on image

pythonimagematplotlib

提问by KAI ZHAO

How to draw a rectangle on a image, like this: enter image description here

如何在图像上绘制矩形,如下所示: 在此处输入图片说明

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.

我不知道接下来要做什么。

回答by tmdavison

You can add a Rectanglepatch to the matplotlib Axes.

您可以Rectangle向 matplotlib 轴添加补丁。

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()

enter image description here

在此处输入图片说明

回答by Serenity

You need use patches.

你需要使用补丁。

import matplotlib.pyplot as plt
import matplotlib.patches as patches

fig2 = plt.figure()
ax2 = fig2.add_subplot(111, aspect='equal')

ax2.add_patch(
     patches.Rectangle(
        (0.1, 0.1),
        0.5,
        0.5,
        fill=False      # remove background
     ) ) 
fig2.savefig('rect2.png', dpi=90, bbox_inches='tight')

回答by James 'Fluffy' Burton

There is no need for subplots, and pyplot can display PIL images, so this can be simplified further:

不需要 subplots,pyplot 可以显示 PIL 图像,因此可以进一步简化:

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from PIL import Image

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

# Display the image
plt.imshow(im)

# Get the current reference
ax = plt.gca()

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

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

Or, the short version:

或者,简短版本:

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from PIL import Image

# Display the image
plt.imshow(Image.open('stinkbug.png'))

# Add the patch to the Axes
plt.gca().add_patch(Rectangle((50,100),40,30,linewidth=1,edgecolor='r',facecolor='none'))

回答by user3731622

From my understanding matplotlibis a plotting library.

根据我的理解matplotlib是一个绘图库。

If you want to change the image data (e.g. draw a rectangle on an image), you could use PIL's ImageDraw, OpenCV, or something similar.

如果您想更改图像数据(例如在图像上绘制矩形),您可以使用PIL 的 ImageDrawOpenCV或类似的东西。

Here is PIL's ImageDraw method to draw a rectangle.

这是PIL 的 ImageDraw 绘制矩形的方法

Here is one of OpenCV's methods for drawing a rectangle.

这是OpenCV 绘制矩形的方法之一

Your question asked about Matplotlib, but probably should have just asked about drawing a rectangle on an image.

您的问题询问了 Matplotlib,但可能应该询问有关在图像上绘制矩形的问题。

Here is another question which addresses what I think you wanted to know: Draw a rectangle and a text in it using PIL

这是另一个问题,它解决了我认为您想知道的问题: 使用 PIL 在其中绘制一个矩形和一个文本