iou_matching.py
Go to the documentation of this file.
1 # vim: expandtab:ts=4:sw=4
2 from __future__ import absolute_import
3 import numpy as np
4 from . import linear_assignment
5 
6 
7 def iou(bbox, candidates):
8  """Computer intersection over union.
9 
10  Parameters
11  ----------
12  bbox : ndarray
13  A bounding box in format `(top left x, top left y, width, height)`.
14  candidates : ndarray
15  A matrix of candidate bounding boxes (one per row) in the same format
16  as `bbox`.
17 
18  Returns
19  -------
20  ndarray
21  The intersection over union in [0, 1] between the `bbox` and each
22  candidate. A higher score means a larger fraction of the `bbox` is
23  occluded by the candidate.
24 
25  """
26  bbox_tl, bbox_br = bbox[:2], bbox[:2] + bbox[2:]
27  candidates_tl = candidates[:, :2]
28  candidates_br = candidates[:, :2] + candidates[:, 2:]
29 
30  tl = np.c_[np.maximum(bbox_tl[0], candidates_tl[:, 0])[:, np.newaxis],
31  np.maximum(bbox_tl[1], candidates_tl[:, 1])[:, np.newaxis]]
32  br = np.c_[np.minimum(bbox_br[0], candidates_br[:, 0])[:, np.newaxis],
33  np.minimum(bbox_br[1], candidates_br[:, 1])[:, np.newaxis]]
34  wh = np.maximum(0., br - tl)
35 
36  area_intersection = wh.prod(axis=1)
37  area_bbox = bbox[2:].prod()
38  area_candidates = candidates[:, 2:].prod(axis=1)
39  return area_intersection / (area_bbox + area_candidates - area_intersection)
40 
41 
42 def iou_cost(tracks, detections, track_indices=None,
43  detection_indices=None):
44  """An intersection over union distance metric.
45 
46  Parameters
47  ----------
48  tracks : List[deep_sort.track.Track]
49  A list of tracks.
50  detections : List[deep_sort.detection.Detection]
51  A list of detections.
52  track_indices : Optional[List[int]]
53  A list of indices to tracks that should be matched. Defaults to
54  all `tracks`.
55  detection_indices : Optional[List[int]]
56  A list of indices to detections that should be matched. Defaults
57  to all `detections`.
58 
59  Returns
60  -------
61  ndarray
62  Returns a cost matrix of shape
63  len(track_indices), len(detection_indices) where entry (i, j) is
64  `1 - iou(tracks[track_indices[i]], detections[detection_indices[j]])`.
65 
66  """
67  if track_indices is None:
68  track_indices = np.arange(len(tracks))
69  if detection_indices is None:
70  detection_indices = np.arange(len(detections))
71 
72  cost_matrix = np.zeros((len(track_indices), len(detection_indices)))
73  for row, track_idx in enumerate(track_indices):
74  if tracks[track_idx].time_since_update > 1:
75  cost_matrix[row, :] = linear_assignment.INFTY_COST
76  continue
77 
78  bbox = tracks[track_idx].to_tlwh()
79  candidates = np.asarray([detections[i].tlwh for i in detection_indices])
80  cost_matrix[row, :] = 1. - iou(bbox, candidates)
81  return cost_matrix
def iou(bbox, candidates)
Definition: iou_matching.py:7
def iou_cost(tracks, detections, track_indices=None, detection_indices=None)
Definition: iou_matching.py:43


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Mon May 3 2021 03:03:27