简体   繁体   中英

Given pixel label, draw a bounding box in python

Using cityscapes dataset I would like to draw a bounding box around an pedestrian and fill this with noise (salt and pepper).

在此处输入图片说明
在此处输入图片说明

Which has the following annotations

"objects": [
    {
        "instanceId": 24000, 
        "bbox": [
            1580, 
            277, 
            150, 
            366
        ], 
        "bboxVis": [
            1594, 
            279, 
            126, 
            364
        ], 
        "label": "pedestrian"
    }, 

How I go about drawing a bounding box around the pedestrian? Or what's the best practice?

Below an example of what I am trying to achieve.

在此处输入图片说明

Note : I resized the original (1024x2048) for viewing purposes.

Update : Tips or suggestions are very much welcome!

Update #2 Added example of what I am trying to achieve. So there are two things here. First, drawing the rectangle bounding box and 2) filling in up with noise. Hope this clears things up.

You can achieve a salt-and-pepper bounding box like in the image if you crop the area and apply the salt ans pepper function from the link above (I just hardcoded the area but you can read it it from the label):

salt-and-peper function is taken from here

import cv2
import numpy as np
import time

def noisy(image):
    row, col, ch = image.shape
    s_vs_p = 0.5
    amount = 0.5
    out = image
    # Salt mode
    num_salt = np.ceil(amount * image.size * s_vs_p)
    coords = [np.random.randint(0, i - 1, int(num_salt))
              for i in image.shape]
    out[coords] = 1

    # Pepper mode
    num_pepper = np.ceil(amount * image.size * (1. - s_vs_p))
    coords = [np.random.randint(0, i - 1, int(num_pepper))
              for i in image.shape]
    out[coords] = 0
    return out

im = cv2.imread('test.jpg', cv2.IMREAD_COLOR)
x = 1580
y = 277
h = 366
w = 150
crop_img = im[y:y+h, x:x+w]
noisy(crop_img)
cv2.rectangle(im, (x,y), (x+w, y+h), (0,0,0), 2) #change (0,0,0) to whatever color you want
cv2.imwrite('exp.jpg', im)

Bounding_box_pedestrian

Are you asking:

A. how to find the coordinates for the bounding boxes?

or

B. are you asking how to draw a rectangle in an image with python?

A. For every pedestrian, get the highest and lowest pixel values for each axis (x_min, x_max, y_min, y_max) and use the as the boundary values for the bounding box.

B. You can use openCV:

import cv2

image = cv2.imread('the path to your image')
cv2.rectangle(image,(x_min,y_min),(x_max,y_max),(0,255,0),2) # add rectangle to image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM