【发布时间】:2018-05-15 20:24:41
【问题描述】:
我的目标是为每个像素找到最近的 x,y 点坐标。基于此,我必须为像素点着色。
这是我尝试过的, 下面的代码将绘制点。
import numpy as np
import matplotlib.pyplot as plt
points = np.array([[0,40],[0,0],[5,30],[4,10],[10,25],[20,5],[30,35],[35,3],[50,0],[45,15],[40,22],[50,40]])
print (points)
x1, y1 = zip(*points)
plt.plot(x1,y1,'.')
plt.show()
现在找到每个像素的最近点。 我发现类似这样的东西,我必须手动给每个像素坐标,以获得最近的点。
from scipy import spatial
import numpy as np
A = np.random.random((10,2))*100
print (A)
pt = np.array([[6, 30],[9,80]])
print (pt)
for each in pt:
A[spatial.KDTree(A).query(each)[1]] # <-- the nearest point
distance,index = spatial.KDTree(A).query(each)
print (distance) # <-- The distances to the nearest neighbors
print (index) # <-- The locations of the neighbors
print (A[index])
输出会是这样的,
[[1.76886192e+01 1.75054781e+01]
[4.17533199e+01 9.94619127e+01]
[5.30943347e+01 9.73358766e+01]
[3.05607891e+00 8.14782701e+01]
[5.88049334e+01 3.46475520e+01]
[9.86076676e+01 8.98375851e+01]
[9.54423012e+01 8.97209269e+01]
[2.62715747e+01 3.81651805e-02]
[6.59340306e+00 4.44893348e+01]
[6.66997434e+01 3.62820929e+01]]
[[ 6 30]
[ 9 80]]
14.50148095039858
8
[ 6.59340306 44.48933479]
6.124988197559344
3
[ 3.05607891 81.4782701 ]
我想从图像中获取每个像素,而不是手动给出每个点,我想找到最近的蓝点。这是我的第一个问题。
之后我想将这些点分为两类, 基于像素和点我想给它上色,基本上我想在它上面做一个集群。
这不是正确的形式。但最后我想要这样。 在此先感谢各位。
【问题讨论】:
标签: python matplotlib kdtree