在numpy或scipy或scikit-learn中,如何找到一种颜色与多种颜色之间的距离?我知道如何找到2种颜色之间的欧式距离。但是我只有一种颜色,我想知道它与颜色数组之间的欧几里得距离,这样我就可以在该颜色数组中找到“最接近的”颜色?
以下内容适用于2种颜色,但不适用于1种颜色与多种颜色:
from scipy.spatial.distance import cdist, euclidean
colour = (255,0,0)
colours = [(255,0,0), (255,0,0), (255,0,0)]
colour_array = [(255,255,255), (0,0,0), (255,0,0)]
dists = cdist(colour, colour_array) # error because not same shape/dimensions
答案 0 :(得分:1)
您只需将单一颜色放入一个元素数组中,然后将其与颜色数组进行比较。例如:
from scipy.spatial.distance import cdist
colour = (255, 0, 0)
colour_array = [(255, 255, 255), (0, 0, 0), (255, 0, 0)]
# Note change here.
dists = cdist([colour], colour_array)
我希望这会有所帮助!