4

Hi I was wondering how to add a grid on top of my image then display it in python. Here is a picture of what I want to do. NOTE: I also want to specify the line type and color for some of the blocks in the image just like the image below. Thanks a lot. this image was generated in matlab

4

2 回答 2

1

下面的示例展示了如何显示 .tif 文件、创建网格,以及如何将网格放在其他绘图元素下方,以便您可以在图像和网格上绘制框和线。

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

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

# Flip the .tif file so it plots upright
im1 = im.transpose(Image.FLIP_TOP_BOTTOM)

# Plot the image
plt.imshow(im1)
ax = plt.gca()

# create a grid
ax.grid(True, color='r', linestyle='--', linewidth=2)
# put the grid below other plot elements
ax.set_axisbelow(True)

# Draw a box
xy = 200, 200,
width, height = 100, 100
ax.add_patch(mpatches.Rectangle(xy, width, height, facecolor="none",
    edgecolor="blue", linewidth=2))

plt.draw()

plt.show()

您可以使用 matplotlib.patches 在图像上绘制所有类型的形状。要绘制单独的线条,我喜欢使用以下线条,但您也可以使用 matplotlib.lines.Line2D。

plt.axvline(x=0.069, ymin=0, ymax=40, linewidth=4, color='r')
于 2012-05-25T21:51:24.243 回答
0

imshow显示图像的功能。在轴的顶部显示网格很简单grid(True).

于 2012-05-17T10:08:25.227 回答