从具有边界框列表的图像中裁剪多个边界框

问题描述 投票:1回答:1

使用亚马逊的Rekognition,我使用以下方法从JSON响应中提取了感兴趣的边界框:

    def __init__(self, image):
        self.shape = image.shape 

    def bounding_box_convert(self, bounding_box):

        xmin = int(bounding_box['Left'] * self.shape[1])
        xmax = xmin + int(bounding_box['Width'] * self.shape[1])
        ymin = int(bounding_box['Top'] * self.shape[0])
        ymax = ymin + int(bounding_box['Height'] * self.shape[0])

        return (xmin,ymin,xmax,ymax)

    def polygon_convert(self, polygon):
        pts = []
        for p in polygon:
            x = int(p['X'] * self.shape[1])
            y = int(p['Y'] * self.shape[0])
            pts.append( [x,y] )

        return pts

def get_bounding_boxes(jsondata):
    objectnames = ('Helmet','Hardhat')
    bboxes = []
    a = jsondata
    if('Labels' in a):
        for label in a['Labels']:

            #-- skip over anything that isn't hardhat,helmet
            if(label['Name'] in objectnames):
                print('extracting {}'.format(label['Name']))


                lbl = "{}: {:0.1f}%".format(label['Name'], label['Confidence'])
                print(lbl)

                for instance in label['Instances']:
                    coords = tmp.bounding_box_convert(instance['BoundingBox'])
                    bboxes.append(coords)

    return bboxes

if __name__=='__main__':

    imagefile = 'image011.jpg'
    bgr_image = cv2.imread(imagefile)
    tmp = Tmp(bgr_image)

    jsonname = 'json_000'
    fin = open(jsonname, 'r')

    jsondata = json.load(fin)
    bb = get_bounding_boxes(jsondata)
    print(bb)

输出是边界框的列表:

[(865, 731, 1077, 906), (1874, 646, 2117, 824)]

我能够轻松地从列表中提取一个位置,并使用以下方式另存为新图像:

from PIL import Image
img = Image.open("image011.jpg")
area = (865, 731, 1077, 906)
cropped_img = img.crop(area)
cropped_img.save("cropped.jpg")

但是,我还没有找到一个好的解决方案,可以使用'bb'列表输出来裁剪并保存图像中的多个边界框。

我确实找到了一种从csv中提取信息的解决方案:Most efficient/quickest way to crop multiple bounding boxes in 1 image, over thousands of images?

但是,我相信有一种比将边界框数据保存到csv并读回的更有效的方法。

我不太擅长编写自己的函数-非常感谢所有建议!

python opencv image-processing computer-vision crop
1个回答
0
投票
假设边界框的坐标为x,y,w,h的形式,则可以进行ROI = image[y:y+h,x:x+w]裁剪。使用此输入图像:

enter image description here

使用how to get ROI Bounding Box Coordinates without Guess & Check中的脚本来获取x,y,w,h边界框坐标以裁剪出这些ROI:

enter image description here

我们只是简单地遍历边界框列表,然后使用Numpy切片对其进行裁剪。提取的投资回报率:

enter image description here

这是最小的示例:

import cv2 import numpy as np image = cv2.imread('1.png') bounding_boxes = [(17, 24, 47, 47), (74, 28, 47, 50), (125, 15, 51, 61), (184, 18, 53, 53), (247, 25, 44, 46), (296, 6, 65, 66) ] num = 0 for box in bounding_boxes: x,y,w,h = box ROI = image[y:y+h, x:x+w] cv2.imwrite('ROI_{}.png'.format(num), ROI) num += 1 cv2.imshow('ROI', ROI) cv2.waitKey()

© www.soinside.com 2019 - 2024. All rights reserved.