Я пытаюсь рассчитать относительные цветовые расстояния между RGB
образцами цветов в моем наборе данных и тремя образцами цветов из моего тестового набора. Таким образом, из цветовых образцов являются numpy массивы с формой 1,6,3
, а значения RGB
нормализованы. Чтобы сравнить образцы, я конвертирую каждый образец цвета в цветовую гистограмму, используя np.histogram
, сглаживая np.flatten()
массивы. Наконец, я сравниваю гистограммы друг с другом, используя встроенный метод scipy.stats.wasserstein_distance
. Тем не менее, расстояния повторяют друг друга, и все это кажется выключенным. Мне было интересно, если кто-то может дать мне подсказку о том, как подойти к этому. Я думаю, что ошибка заключается в сглаживании массивов, но я не совсем уверен, как еще, я могу конвертировать цвета RGB для сравнения друг с другом.
Test-1.jpg - 1x5px! - https://ibb.co/Fsg7Mkd Test-2.jpg - 1x5px! - https://ibb.co/0jz4Zk1 Test-3.jpg - 1x5px! - https://ibb.co/SB0fhFh
from scipy.stats import wasserstein_distance as wd
import numpy as np
import cv2
def compute_histogram(image, nr_bins):
hist = np.histogram(image, bins=nr_bins, range=[0, 255])
return hist
# data swatches to measure the test swatches against
data_sw = np.array(
[[[225, 254, 239],
[145, 212, 167],
[116, 186, 137],
[ 91, 162, 115],
[ 72, 143, 98]],
[[ 97, 165, 214],
[211, 234, 230],
[ 61, 195, 182],
[ 77, 92, 75],
[145, 157, 136]],
[[ 1, 35, 220],
[ 12, 20, 142],
[105, 159, 177],
[ 11, 85, 130],
[ 15, 39, 49]]])
# the test Files stored locally
test_files = ['Test-1',
'Test-2',
'Test-3']
# n bins
n_bins_ = 6
# distances from each test file to each swatch
dict_distances = {}
#computation of distances
for tst_name in test_files:
# load testfile locally
tst_fl = cv2.imread(tst_name+'.jpg')[0]
# store distances temporarily
temp_dist = {}
# loop over the data swatches to compare test swatches to
for i,d_sw in enumerate(list(data_sw)):
# make 1-D
curr_testsw = tst_fl.tolist()
curr_datasw = d_sw.tolist()
#create histogram
test_hist = compute_histogram(curr_testsw, n_bins_)[0]
data_hist = compute_histogram(curr_datasw, n_bins_)[0]
#calculate the distance between histograms
distance = wd(test_hist, data_hist)
# put into temp dict using the indices of data as keys
temp_dist[i] = distance
dict_distances[tst_name] = temp_dist
print(dict_distances)
Спасибо!