繁体   English   中英

将边界框另存为图像

[英]Save bounding box as image

我有一些python代码,可以接收A4字母的图像,然后在每个字符周围绘制边框。

我想知道如何将每个边界框保存为图像,因此从本质上讲,它将占用它检测到的每个字符并将其保存。 最好将.png调整为20x20

这里也提出类似的问题,但答案很模糊,不知道如何在我的代码中实现)

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from scipy.misc import imread,imresize
from skimage.segmentation import clear_border
from skimage.morphology import label
from skimage.measure import regionprops


image = imread('./adobe.png',1)

#apply threshold in order to make the image binary
bw = image < 120

# remove artifacts connected to image border
cleared = bw.copy()
clear_border(cleared)

# label image regions
label_image = label(cleared,neighbors=8)
borders = np.logical_xor(bw, cleared)
label_image[borders] = -1

print label_image.max()

fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
ax.imshow(bw, cmap='jet')



for region in regionprops(label_image, ['Area', 'BoundingBox']):
    # skip small images
    if region['Area'] > 50:

        # draw rectangle around segmented coins
        minr, minc, maxr, maxc = region['BoundingBox']
        rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
                              fill=False, edgecolor='red', linewidth=2)
        ax.add_patch(rect)

plt.show()

如果我不够清楚,请发表评论,我会尽力而为,

谢谢

您引用的问题使用来自OpenCV的findContours ,OpenCV是图像处理的通用库。 如果已经有了边界框(以x,y和宽度,高度表示),则可以使用matplotlib或opencv进行简单导出:

image_patch = img[minr:maxr, minc:maxc]  # get region of interest (slice)
# .. maybe do some scaling
plt.imsave("filename.png", image_patch)

将其渲染到图形后,也可以使用fig.savefig(path)。 或使用opencv:

import cv2
cv2.imsave("path.png", img_patch)

您可能需要在文件名中添加后缀(和/或检查文件是否已存在?),以避免覆盖。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM