import numpy as np def bounding_box_volume(bounding_box): min_point = bounding_box['min'] max_point = bounding_box['max'] length_x = max_point['x'] - min_point['x'] length_y = max_point['y'] - min_point['y'] length_z = max_point['z'] - min_point['z'] return length_x * length_y * length_z def overlap_volume (bounding_box1, bounding_box2): box1_min = bounding_box1['min'] box1_max = bounding_box1['max'] box2_min = bounding_box2['min'] box2_max = bounding_box2['max'] # Calculate overlap in each dimension x_overlap = max(0, min(box1_max['x'], box2_max['x']) - max(box1_min['x'], box2_min['x'])) y_overlap = max(0, min(box1_max['y'], box2_max['y']) - max(box1_min['y'], box2_min['y'])) z_overlap = max(0, min(box1_max['z'], box2_max['z']) - max(box1_min['z'], box2_min['z'])) # Calculate the volume of the intersecting region overlap_vol = x_overlap * y_overlap * z_overlap return overlap_vol def calculate_bounding_box_overlap(bounding_box1, bounding_box2, overlap_threshold): """ Returns true when bounding boxes overlap. Else return false. """ overlap = overlap_volume(bounding_box1, bounding_box2) return ((overlap / bounding_box_volume(bounding_box1) > overlap_threshold) or (overlap / bounding_box_volume(bounding_box2) > overlap_threshold)) def bhattarcharyya_coefficient(distribution_1, distribution_2): """ Computes the difference between two normalized statistical distributions. E.g. of two color histograms. Smaller values indicate higher similarity. """ value1 = distribution_1 / np.sum(distribution_1) value2 = distribution_2 / np.sum(distribution_2) return np.sum(np.sqrt(value1 * value2)) def cosine_similarity(vector_1, vector_2): """ Calculates the cosine similarity of two vectors. Higher values indicate more alike vectors. """ return np.dot(vector_1, vector_2) / (np.linalg.norm(vector_1) * np.linalg.norm(vector_2))