text: import numpy as np
  import cv2
  from scipy.ndimage.measurements import label
  
  """ Utility functions to filter the bounding boxes by using heatmap with threshold
  """
  
  
  def add_heat(heatmap, bbox_list):
      # Iterate through list of bboxes
      for box in bbox_list:
          # Add += 1 for all pixels inside each bbox
          # Assuming each "box" takes the form ((x1, y1), (x2, y2))
          heatmap[box[0][1]:box[1][1], box[0][0]:box[1][0]] += 1
  
      # Return updated heatmap
      return heatmap  # Iterate through list of bboxes
  
  
  def apply_threshold(heatmap, threshold):
      # Zero out pixels below the threshold
      heatmap[heatmap <= threshold] = 0
      # Return thresholded map
      return heatmap
  
  
  def get_filtered_bbox(labels):
      bbox_list = []
      # Iterate through all detected cars
      for car_number in range(1, labels[1] + 1):
          # Find pixels with each car_number label value
          nonzero = (labels[0] == car_number).nonzero()
          # Identify x and y values of those pixels
          nonzeroy = np.array(nonzero[0])
          nonzerox = np.array(nonzero[1])
          # Define a bounding box based on min/max x and y
          bbox = ((np.min(nonzerox), np.min(nonzeroy)), (np.max(nonzerox), np.max(nonzeroy)))
          bbox_list.append(bbox)
      return bbox_list
  
  
  def draw_labeled_bboxes(img, labels):
      return img
  
  
  def draw_filtered_bbox(image, bbox_list, min_thr=1):
      heat = np.zeros_like(image[:, :, 0]).astype(np.float)
  
      # Add heat to each box in box list
      heat = add_heat(heat, bbox_list)
  
      # Apply threshold to help remove false positives
      heat = apply_threshold(heat, min_thr)
  
      # Visualize the heatmap when displaying
      heatmap = np.clip(heat, 0, 255)
  
      # Find final boxes from heatmap using label function
      labels = label(heatmap)
      for bbox in get_filtered_bbox(labels):
          # Draw the box on the image
          cv2.rectangle(image, bbox[0], bbox[1], (0, 0, 255), 6)
      return image, heatmap
  
  
  if __name__ == '__main__':
      import matplotlib.image as mpimg
      import matplotlib.pyplot as plt
      import pickle
  
      # Read in a pickle file with bboxes saved
      # Each item in the "all_bboxes" list will contain a
      # list of boxes for one of the images shown above
      box_list = pickle.load(open("bbox_pickle.p", "rb" ))
  
      # Read in image similar to one shown above
      image = mpimg.imread('test_image.jpg')
      draw_img, heatmap = draw_filtered_bbox(image, box_list)
  
      fig = plt.figure()
      plt.subplot(121)
      plt.imshow(draw_img)
      plt.title('Car Positions')
      plt.subplot(122)
      plt.imshow(heatmap, cmap='hot')
      plt.title('Heat Map')
      # fig.tight_layout()
      plt.show()
  