From 8ed15e0c3daedc9ac0348f361d919a27339371ca Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Sun, 29 Jan 2023 22:57:29 +0100 Subject: [PATCH] Euclidean distance comparison method --- comparisons.py | 13 +++++++++++++ main.py | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/comparisons.py b/comparisons.py index a4defff..7029786 100644 --- a/comparisons.py +++ b/comparisons.py @@ -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) diff --git a/main.py b/main.py index 8046255..af5ad1b 100644 --- a/main.py +++ b/main.py @@ -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):