使用边界框列表从图像中裁剪多个边界框

时间:2020-01-13 19:08:16

标签: python opencv image-processing computer-vision crop

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

rowNum = 0
with xlsxwriter.Workbook('testingThis.xlsx') as workbook:
    worksheet = workbook.add_worksheet()

def createStringArray(theFilePath):
    theFinalString = ""
    with open(theFilePath) as file_in:
        for line in file_in:
            lineToString = str(line)
            theCompleteString = lineToString.split()
            printStringArray(theCompleteString)
            for aString in theCompleteString:
                theFinalString = theFinalString + aString + "--"
    print(theFinalString)


def printStringArray(theStringArray):
    colNum = 0
    global rowNum
    worksheet.write(15, 15, "Aapple")
    for data in theStringArray:
        worksheet.write(rowNum, colNum, str(data))
        print(rowNum)
        print(colNum)
        print(data)
        colNum = colNum + 1
    rowNum = rowNum + 1

输出是边界框的列表:

    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)]

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

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

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

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

1 个答案:

答案 0 :(得分:1)

假设边界框的坐标为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()