Converting boundary boxes to observations

In order to pass the boundary boxes to the Kalman filter, we will have to define a transformation function from each boundary box to the observation model, and, in order to use the predicted boundary boxes for object tracking, we need to define a function from a state to a boundary box.

Let's start with a transformation function from a boundary box to an observation:

  1. First, we calculate the center coordinates of the boundary box:
def bbox_to_observation(bbox):
x, y = (bbox[0:2] + bbox[2:4]) / 2
  1. Next, we calculate the width and height of the box, which we will use to calculate the size (that is, the area) and the scale:
    w, h = bbox[2:4] - bbox[0:2]
  1. Then, we calculate the size of bbox, that is, the area:
    s = w * h
  1. After that, we calculate the aspect ratio, which is done just by dividing the width by the height:
    r = w / h
  1. Then return the result as a 4 x 1 matrix:
    return np.array([x, y, s, r])[:, None].astype(np.float64)

Now, since we know that we have to define the inverse transformation as well, let's define state_to_bbox:

  1. It takes a 7 x 1 matrix as an argument and unpacks all the components that we need to construct a boundary box:
def state_to_bbox(x):
center_x, center_y, s, r, _, _, _ = x.flatten()
  1. Then, it calculates the width and the height of the boundary box, from the aspect ratio and scale:
    w = np.sqrt(s * r)
h = s / w
  1. After that, it calculates the coordinates of the center:
    center = np.array([center_x, center_y])
  1. Then, it calculates the half size of the box as a numpy tuple, and uses it to calculate the coordinates of the opposite corners of the box:
    half_size = np.array([w, h]) / 2
corners = center - half_size, center + half_size
  1. Then, we return the boundary box as a one-dimensional numpy array:
    return np.concatenate(corners).astype(np.float64)

Geared with the transformation functions, let's see how we can use OpenCV to build a Kalman filter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.228.178