如何将文本添加到图像片段

How to add text to an image segment

我有以下 Python 代码,它在检测到的片段周围添加了一个边界框

%matplotlib qt
fig, ax = plt.subplots(figsize=(10, 6))
ax.imshow(image_label_overlay)
for region in regions:
 # take regions with large enough areas
 if region.area >= 100:
    # draw rectangle around segmented coins
    minr, minc, maxr, maxc = region.bbox
    rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
                              fill=False, edgecolor='red', linewidth=2)
    ax.add_patch(rect)


 ax.set_axis_off()

 plt.tight_layout()
 plt.show()

我不想绘制边界框,而是想对线段进行编号。即我想在每个段的中心添加一个数字。我该怎么做?

plt.text(x, y, s, bbox=dict(fill=False, edgecolor='red', linewidth=2))

其中 x 是您的 x 轴坐标,y 是您的 y 轴坐标。 s 是您要写入绘图的字符串。

bbox 让我们在它周围有一个文本和一个矩形。 bbox 需要一个具有 Rectangle 属性 (https://matplotlib.org/api/_as_gen/matplotlib.patches.Rectangle.html#matplotlib.patches.Rectangle) 的字典,您已经在代码中使用了它。