Python中文
首页
教程
问答
标签
搜索
登录
注册
python中按距离排序点的奇怪结果
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我通过提取图像的边缘得到了一个点列表,比如:<a href="https://i.stack.imgur.com/N5GQo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/N5GQo.png" alt="enter image description here"/></a>但是它的顺序不是很好,所以如果我把它连接成一条线,它将是:<a href="https://i.stack.imgur.com/eOWVe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eOWVe.png" alt="enter image description here"/></a></p> <p>因此,我想排序这个列表如果点。比如,从点0开始,找出哪一个距离它最短,比如说,点3,然后找到哪一个离点3最近,然后继续。。。 为了对这些点进行排序,我写了这样一段话:</p> <pre><code>import matplotlib.pyplot as plt import numpy as np import math def dist(now, seek): return math.sqrt((now[0] - seek[0])**2 + (now[1] - seek[1])**2) def sortNearest(x, y): if len(x) != len(y): raise Exception('Error! Array length do not match!') return False xNew = []; yNew = [] nearest = 0 #record which point is nearest now = [x[0], y[0]] #start point index seekValue = 0 while len(x) > 0: distance = (max(x) - min(x)) + (max(y) - min(y)) for seek in range(len(x)): # other temp = dist(now, [x[seek], y[seek]]) if temp < distance and temp != 0.0: distance = temp seekValue = x[seek] xNew.append(now[0]); yNew.append(now[1]); if len(x) > 0: x.remove(now[0]) y.remove(now[1]) if len(x) > 0: nearest = x.index(seekValue) now = [x[nearest], y[nearest]] x = list(xNew); y = list(yNew) return xNew, yNew x, y = getBorder('large.png', maxRes = 125) x, y = sortNearest(x, y) </code></pre> <p>但效果不好,我想到了:<a href="https://i.stack.imgur.com/JtFcq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JtFcq.png" alt="enter image description here"/></a> 这显然是不正确的,如果我放大,请看:<a href="https://i.stack.imgur.com/OevRp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OevRp.png" alt="enter image description here"/></a> 如果我的代码运行我想要的,点644应该连接620或675,除了645。。。怎么了?你知道吗</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>好吧,644点不能连接到620点,因为620点已经是你路径的一部分了。你知道吗</p> <p>至于为什么它连接到645而不是更接近的675:在你的循环中,你实际上不记得最近点的索引,你只记得它的x坐标。在循环之后,您可以定位一个具有相同x坐标的任意点-它可以位于穿过所需点的垂直线上的任何位置。你知道吗</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何重塑数组、迭代列的所有行并将重塑后的数组分配给新列?Python/Pandas/Numpy
7 回答
如何重塑数组的形状?
4 回答
如何重塑文本数据以适应keras的LSTM模型
3 回答
如何重塑未对齐的数据集,并使用numpy丢弃剩余数据?
2 回答
如何重塑此数据以使用绘图
4 回答
如何重塑此数据帧?
10 回答
如何重塑此数据集以适应RNN
8 回答
如何重塑没有列的数组?
10 回答
如何重塑测试数据帧,使其维数与训练和预测工作中使用的维数相同?
5 回答
如何重塑系列以在StandardScaler中使用它
4 回答
如何重塑线性回归的数据
6 回答
如何重塑线性回归的数据?
6 回答
如何重塑表格?
4 回答
如何重塑要堆叠的重复宽数据帧?
7 回答
如何重塑输入以放入二维层?
5 回答
如何重塑输入神经网络的三通道数据集
7 回答
如何重塑这个numpy数组
4 回答
如何重塑这个numpy数组以排除“额外维度”?
10 回答
如何重塑这个numpy阵列?
4 回答
如何重塑这个数据帧
2 回答