user3641381
user3641381

Reputation: 1076

Determine maximum and minimum pixel value to draw bounding box

Given an image with semantic map,

enter image description here

enter image description here

In python, How can I draw a bounding-box around the person standing for instance?

I've done some research and understood that I need to choose the maximum and minimum pixel value to get the bounding box information. But I don't understand how to implement this.

Upvotes: 1

Views: 837

Answers (1)

Alderven
Alderven

Reputation: 8260

  1. Redraw the object you want to box in unique for that semantic map color. For instance I've used green:

semantic map

  1. Run following script:

.

from PIL import Image, ImageDraw

IMAGE = 't5XM4.png'
IMAGE_MAP = 'Gz8b7.png'
IMAGE_OUTPUT = 'Result.png'
GREEN = (0, 255, 0)
OFFSET = 10

image_map = Image.open(IMAGE_MAP)
image = Image.open(IMAGE)
pixels = image_map.load()
size_sm = image_map.size
size = image.size
ratio = (size_sm[0]/size[0], size_sm[1]/size[1])
x_list = []
y_list = []

for x in range(size_sm[0]):
    for y in range(size_sm[1]):
        if pixels[x, y] == GREEN:
            x_list.append(x)
            y_list.append(y)

draw = ImageDraw.Draw(image)
draw.rectangle(((min(x_list)/ratio[0]-OFFSET, min(y_list)/ratio[1]-OFFSET),
                (max(x_list)/ratio[0]+OFFSET,max(y_list)/ratio[1]+OFFSET)),
               width=5, outline=GREEN)
image.save(IMAGE_OUTPUT, 'PNG')
  1. You've got following image at the end:

result image with bounding box

Upvotes: 1

Related Questions