首页 > 解决方案 > 色样上的 wasserstein 距离

问题描述

RGB我尝试使用我的测试集的三个色样来计算我的数据集的色样之间的相对颜色距离。因此,颜色样本是具有 shape 的 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)


谢谢!

标签: imagenumpycolorsscipyhistogram

解决方案


推荐阅读