python - 按欧几里得距离排序颜色列表
问题描述
我正在尝试按欧几里得距离对我的颜色(在颜色空间 CieLAB 中)进行排序。所以我使用下面的代码但是它重新排列我的颜色而不是排序。我是否需要指定不同的轴或使用不同的功能。如果我需要不同的功能,你能建议哪一个可以工作吗?
a = np.array([(255,9,255), (0,0,0), (125,125,4)])
a.sort(axis=0)
print(a)
结果(注意它是如何重新排列颜色的?):
[[ 0 0 0]
[ 4 125 125]
[ 9 255 255]]
它应该是:
[[ 0 0 0]
[ 125 125 4]
[ 255 9 255]]
解决方案
我希望我理解了这个问题,
也许你应该先计算成对距离,然后按这些距离排序。
类似的东西:
import numpy as np
from scipy.spatial.distance import cdist
def sort_by_eucledian_distance(a):
dist = cdist(a, a)[:, 0] # calculate distances
dist = sorted(zip(dist, np.arange(len(dist)))) #add indexes and sort
idxs = [v[1] for v in dist] # get the new, sorted indexes
return a[idxs]
a = np.array([(255,9,255), (0,0,0), (125,125,4)])
b = sort_by_eucledian_distance(a)
print(b)
将打印
array([[ 0., 0., 0.],
[125., 9., 4.],
[255., 125., 255.]])
推荐阅读
- hive - 配置单元中的 Regex_extract 以提取日期
- angular - 如何在 Angular 5、6 或 7 的移动视图中显示 PDF?
- c# - 运行状况检查响应缺少 .json 扩展名
- laravel - 如何在一张表中保存许多记录 Laravel Eloquent(无关系)
- python - tsFresh - 给定的 id 列在数据中不存在
- c# - 无法从 oracle 存储过程中获取布尔值
- batch-file - 批处理两个有一个字母差异的WAV文件?
- windows - 从 cmd 脚本打开另一个 cmd 脚本而不打开新窗口
- algorithm - 是否有最佳算法来查找行子集,总和在指定间隔内?
- php - zend 框架会话过期或销毁配置设置