Euclidean distance comparison method

This commit is contained in:
Marcin Kostrzewski 2023-01-29 22:57:29 +01:00
parent 16e03179ee
commit 8ed15e0c3d
2 changed files with 15 additions and 1 deletions

View File

@ -27,3 +27,16 @@ def histogram_comparison(data_a: np.ndarray, data_b: np.ndarray) -> dict:
def structural_similarity_index(data_a: np.ndarray, data_b: np.ndarray) -> float:
return structural_similarity(cv2.cvtColor(data_a, cv2.COLOR_BGR2GRAY), cv2.cvtColor(data_b, cv2.COLOR_BGR2GRAY))
def euclidean_distance(data_a: np.ndarray, data_b: np.ndarray) -> float:
gray_a = cv2.cvtColor(data_a, cv2.COLOR_BGR2GRAY)
histogram_a = cv2.calcHist([gray_a], [0], None, [256], [0, 256])
gray_b = cv2.cvtColor(data_b, cv2.COLOR_BGR2GRAY)
histogram_b = cv2.calcHist([gray_b], [0], None, [256], [0, 256])
result, i = [0.], 0
while i < len(histogram_a) and i < len(histogram_b):
result += (histogram_a[i] - histogram_b[i]) ** 2
i += 1
return result[0] ** (1 / 2)

View File

@ -3,7 +3,7 @@ import cv2
import numpy as np
import matplotlib.pyplot as plt
from comparisons import histogram_comparison, structural_similarity_index
from comparisons import histogram_comparison, structural_similarity_index, euclidean_distance
# Allows imports from the style transfer submodule
sys.path.append('DCT-Net')
@ -45,6 +45,7 @@ def compare_with_anime_characters(data: np.ndarray) -> int:
plot_two_images(example_face, data_rescaled)
print(histogram_comparison(data_rescaled, example_face))
print(f'structural-similarity: {structural_similarity_index(data_rescaled, example_face)}')
print(f'euclidean-distance: {euclidean_distance(data_rescaled, example_face)}')
def transfer_to_anime(img: np.ndarray):