image - 色样上的 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)
谢谢!
解决方案
推荐阅读
- c++ - C++ 正则表达式捕获组混淆
- python - 如何获取unittest.TestResult中成功测试的方法名列表?
- docker - 堆栈部署后未创建 Docker 容器。在哪里可以找到错误日志?
- java - 由于可选对象的属性也是可选的,如何优化 if-else 语句?
- delphi - PAServer 调试在异常后停止工作
- sql - 如何根据第二个表中的计数和字符串查询顶级记录组
- rust - 实现基本类型的特征(浮点数)
- ios - 使用 FetchedResultsController 时 UITableView 中的部分不正确
- c - 链接器找不到外部引用
- r - 在森林函数中重复变量名