1

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

enter image description here
enter image description here

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.

enter image description here

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.

user3641381
  • 1,036
  • 3
  • 15
  • 29
  • It is not completely clear what you acctually ask. But as you have the labeled data which seam to include the location of your bounding box, I asume your are asking how to apply salt-and-pepper noise to that bounding box, which has been answered here: [impulse-gaussian-and-salt-and-pepper-noise-with-opencv](https://stackoverflow.com/questions/14435632/impulse-gaussian-and-salt-and-pepper-noise-with-opencv) – Tanja Bayer Jan 06 '19 at 14:33
  • @TanjaBayer I've updated the post. Please have a look. – user3641381 Jan 07 '19 at 19:30

2 Answers2

3

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
Mark.F
  • 1,624
  • 4
  • 17
  • 28
  • Lets suppose, we draw a polygon on RGB image and we have many point. How i can select the lowest and highest point in an image and draw the rectangle bounding box based on points? – Khawar Islam Dec 10 '20 at 04:19
2

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

Tanja Bayer
  • 711
  • 5
  • 13