首页 > 解决方案 > 从单个图像中获取多个图像裁剪

问题描述

我正在开发一个 OCR,它扫描检测器模块检测到的文本,假设一个图像具有n边界框,我想要一个读取.xml坐标的函数来裁剪边界框周围的图像。这是我尝试过的:

def get_cropped_image(image_file, 
                  images_path = "data/split/train/images",
                  annotations_path = "data/split/train/annotations"):

    images_filenames = os.listdir(images_path)
    annotations_filenames = os.listdir(annotations_path)

    image = cv2.imread(os.path.join(images_path, image_file))
    annotation_filename = image_file.split(sep='.')[0]+'.xml'


    bboxes, labels = read_annotation_file(annotations_path, annotation_filename)

    for idx, label in enumerate(labels):
        try:
            cropped_img = imcrop(image, bboxes[idx]).copy()
            return cropped_img
        except:
            raise

但是,此函数仅返回每个图像的第一个边界框裁剪。我怎样才能真正n从每张图像中返回作物?

标签: pythonopencvcomputer-vision

解决方案


您可以返回裁剪图像的列表:

imgList = []

for idx, label in enumerate(labels):
    imgList.append(imcrop(image, bboxes[idx]).copy())

return imgList

推荐阅读