Skip to content Skip to sidebar Skip to footer

Most Efficient/quickest Way To Crop Multiple Bounding Boxes In 1 Image, Over Thousands Of Images?

I have a dataset of around 3000 images, of which I want to crop multiple areas of each image if I have the coordinates of the bounding boxes for their location. The only problem is

Solution 1:

How large are the images?

It might be worth investigating some of the other library suggestions on the pillow performance page especially with regards to efficiency.

ImageMagick was my first thought. It is quite powerful and can be run from command line (see docs). Batch cropping a whole folder is straightforward, although I'm not sure how to crop each image in a different area (there must be a way). A hacky but workable solution could be to use python to process the csv and generate ImageMagic calls.

Apologies for not having a clear solution.

Solution 2:

You are decoding the whole image for each crop position. Try changing your loop to be:

def split_images(name, labels):
    imsource = Image.open('train_images/{}.jpg'.format(name))
    boundingboxes = np.array(labels.split(' ')).reshape(-1, 5)

    for (unicode, x, y, w, h) in boundingboxes:
        try:
            # Create target Directory
            os.mkdir('unicodes/{}'.format(str(unicode)))
        except FileExistsError:
            None

        (x, y, w, h) = (int(x), int(y), int(w), int(h))
        cropped_image = imsource.crop((x, y, x + w, y + h))
        cropped_image.save('unicodes/{}/{}.jpg'.format(unicode, name))

You have the mkdir inside the loop too, it might help a bit to move that out, if you can. Maybe make one pass over labels first to get an array of unique directory names that need creating.

You could try in other image processing libraries, but crop and save is extremely simple, so I doubt if it would make much difference.

I guess this is training a network, is that right? You can get a very big speedup by sending the cropped patches directly to pytorch or whatever you are using rather than going via JPG files. You could consider generating the set of rotates and flips at the same time.

Post a Comment for "Most Efficient/quickest Way To Crop Multiple Bounding Boxes In 1 Image, Over Thousands Of Images?"