【问题标题】:matplotlib: how to draw a rectangle on imagematplotlib:如何在图像上绘制矩形
【发布时间】:2016-09-22 22:53:10
【问题描述】:

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

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)

我不知道如何继续。

【问题讨论】:

    标签: python image matplotlib


    【解决方案1】:

    您可以将 Rectangle 补丁添加到 matplotlib 轴。

    例如(使用教程中的图片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()
    

    【讨论】:

    • 感谢您的回答!它有效,但似乎矩形是在轴上绘制的,而不是图片本身。如果我尝试将图像保存到文件中,则不会保存矩形。有没有办法让矩形替换图像上的像素值?再次感谢!
    • 没关系。我找到了这个link,它似乎正在工作:)
    • 如果你仍然得到填充矩形,将fill=False标志传递给Rectangle
    • 这很奇怪。 patches.Rectangle 的文档说前两个数字是 The bottom and left rectangle coordinates。我在这里看到前两个数字 (50,100) 对应于矩形的顶部和左侧坐标。我很困惑。
    • 不,矩形在正确的位置。它在数据坐标中。如果您想在坐标轴坐标中更改变换,您可以更改
    【解决方案2】:

    不需要子图,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)
    

    或者,简短的版本:

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

    【讨论】:

      【解决方案3】:

      你需要使用补丁。

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

      【讨论】:

      • 我喜欢您将轴封装在图形对象中的方式:轴进行绘图,图形执行高级界面操作
      【解决方案4】:

      据我了解,matplotlib 是一个绘图库。

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

      这里是PIL's ImageDraw method to draw a rectangle

      这是OpenCV's methods for drawing a rectangle 之一。

      你的问题是关于 Matplotlib,但可能应该只是问关于在图像上绘制一个矩形。

      这是另一个问题,它解决了我认为您想知道的问题: Draw a rectangle and a text in it using PIL

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-19
        • 1970-01-01
        • 2019-05-03
        相关资源
        最近更新 更多