Samuel Schmidt
Update src/helper.py
e4ec374
raw
history blame
1.14 kB
import numpy as np
import cv2
def pil_cv2_image_converter(image):
numpy_image = np.array(image)
# Convert to an OpenCV image; notice the COLOR_RGB2BGR flag, which means
# that the color is converted from RGB to BGR format.
opencv_image = cv2.cvtColor(numpy_image, cv2.COLOR_RGB2BGR)
return opencv_image
def chi2_distance(histA, histB, eps = 1e-10):
# compute the chi-squared distance
d = 0.5 * np.sum([((a - b) ** 2) / (a + b + eps)
for (a, b) in zip(histA, histB)])
# return the chi-squared distance
return d
def euclidean_distance(x, y):
# compute the Euclidean distance
d = np.sqrt(np.sum((x - y) ** 2))
# return the Euclidean distance
return d
def merge_features(feature1, feature2):
# pad or truncate hist_color and hist_lbp to the same length
max_length = max(feature1.shape[0], feature2.shape[0])
feature1_padded = np.pad(hist_color, (0, max_length - hist_color.shape[0]))
feature2_padded = np.pad(hist_lbp, (0, max_length - hist_lbp.shape[0]))
# concatenate the padded feature vectors
return np.hstack((hist_color_padded, hist_lbp_padded))