1

我有以下正在稳步增加的点列表,例如:

[[0, 0], [9, 4], [18, 19], [25, 34], [48, 48], [54, 53], [61, 65], [69, 82], [73, 86], [87, 99]]

但我碰巧遇到了一些违反这种模式的点,如下表所示:

[526, 590], [532, 599], [605, 539], [740, 519], [753, 539], [858, 700], [981, 615], [985, 539], [1105, 700], [1222, 590], [1359, 343], [1456, 86], [1617, 4], [1790, 44], [1885, 1927], [2016, 2008]

所以,我需要找到一种方法来修剪/过滤掉这些违反模式的点,在这种情况下,指向从[605, 539][1790, 44]

我尝试使用以下内容,仅接受 y 坐标位于前一个点的 y 坐标和下一个点的 y 坐标之间的点:

for i, pt in enumerate(points[1:-1]):
    x,y=cur_pt=points[i]
    x0,y0=prev_pt=points[i-1]
    x1,y1=next_pt=points[i+1]
    if y<y1 and y>y0:
        print 'acceptable point'
    else:
        print 'pruned'

问题是它修剪了许多有效点并留下了一些需要修剪的点。

点列表在这里:

points=[[0, 0], [9, 4], [18, 19], [25, 34], [48, 48], [54, 53], [61, 65], [69, 82], [73, 86], [87, 99], [93, 105], [96, 108], [98, 110], [99, 111], [100, 112], [106, 118], [119, 131], [128, 140], [134, 146], [137, 149], [139, 151], [140, 152], [141, 153], [147, 159], [160, 172], [185, 153], [213, 219], [215, 241], [219, 245], [223, 249], [236, 262], [247, 276], [249, 278], [274, 302], [282, 294], [288, 318], [313, 352], [365, 419], [377, 423], [416, 458], [435, 468], [468, 519], [481, 539], [508, 559], [526, 590], [532, 599], [605, 539], [740, 519], [753, 539], [858, 700], [981, 615], [985, 539], [1105, 700], [1222, 590], [1359, 343], [1456, 86], [1617, 4], [1790, 44], [1885, 1927], [2016, 2008], [2072, 2137], [2186, 2212], [2219, 2477], [2260, 2482], [2425, 2477], [2568, 2460], [2646, 2609], [2816, 2792], [2913, 2686], [2960, 2853], [3072, 2959], [3210, 2925], [3249, 2809], [3359, 2959], [3446, 3057], [3517, 2809], [3809, 2959], [4033, 3190], [4232, 3057], [4439, 2809], [4632, 3057], [4706, 2809], [4715, 1922], [4725, 1596], [5058, 1560], [5066, 1596], [5107, 1560], [5362, 1333], [5432, 1471], [5519, 1560], [5610, 1471], [5693, 249], [5782, 1471], [5968, 1560], [6068, 1471], [6105, 1560], [6298, 1700], [6390, 3765], [6416, 5926], [6446, 6440], [6503, 7300], [6511, 7332], [6522, 7342]]
4

4 回答 4

2
>>> points = [526, 590], [532, 599], [605, 539], [740, 519], [753, 539], [858, 700], [981, 615], [985, 539], [1105, 700], [1222, 590], [1359, 343], [1456, 86], [1617, 4], [1790, 44], [1885, 1927], [2016, 2008]
>>> def prune(points):
        yield points[0]
        last = points[0]
        for point in points[1:]:
            if point[1] > last[1]:
                last = point
                yield point


>>> list(prune(points))
[[526, 590], [532, 599], [858, 700], [1885, 1927], [2016, 2008]]
于 2013-04-17T13:19:20.343 回答
0

我尝试使用以下内容,仅接受其 y 坐标位于前一个点的 y 坐标和下一个点的 y 坐标之间的点...

用类似的东西检查 x 坐标不是更有意义吗?

for i, pt in enumerate(points[1:-1]):
    x,y=cur_pt=points[i]
    x0,y0=prev_pt=points[i-1]
    x1,y1=next_pt=points[i+1]
    if y<y1 and y>y0 and x<x1 and x>x0:
        print 'acceptable point'
    else:
        print 'pruned'
于 2013-04-17T13:24:39.930 回答
0

您可以在第一次迭代中执行第二次迭代,从第一次迭代的索引开始。如果当前检查的变量(在迭代 2 中)小于第一次迭代的变量,您可以将其与布尔变量结合起来,如下所示:

#! /usr/bin/env python
points=points=[[0, 0], [9, 4], [18, 19], [25, 34], [48, 48], [54, 53], [61, 65], [69, 82], [73, 86], [87, 99], [93, 105], [96, 108], [98, 110], [99, 111], [100, 112], [106, 118], [119, 131], [128, 140], [134, 146], [137, 149], [139, 151], [140, 152], [141, 153], [147, 159], [160, 172], [185, 153], [213, 219], [215, 241], [219, 245], [223, 249], [236, 262], [247, 276], [249, 278], [274, 302], [282, 294], [288, 318], [313, 352], [365, 419], [377, 423], [416, 458], [435, 468], [468, 519], [481, 539], [508, 559], [526, 590], [532, 599], [605, 539], [740, 519], [753, 539], [858, 700], [981, 615], [985, 539], [1105, 700], [1222, 590], [1359, 343], [1456, 86], [1617, 4], [1790, 44], [1885, 1927], [2016, 2008], [2072, 2137], [2186, 2212], [2219, 2477], [2260, 2482], [2425, 2477], [2568, 2460], [2646, 2609], [2816, 2792], [2913, 2686], [2960, 2853], [3072, 2959], [3210, 2925], [3249, 2809], [3359, 2959], [3446, 3057], [3517, 2809], [3809, 2959], [4033, 3190], [4232, 3057], [4439, 2809], [4632, 3057], [4706, 2809], [4715, 1922], [4725, 1596], [5058, 1560], [5066, 1596], [5107, 1560], [5362, 1333], [5432, 1471], [5519, 1560], [5610, 1471], [5693, 249], [5782, 1471], [5968, 1560], [6068, 1471], [6105, 1560], [6298, 1700], [6390, 3765], [6416, 5926], [6446, 6440], [6503, 7300], [6511, 7332], [6522, 7342]]
accept=[]
for counter,i in enumerate(points): # Iteration 1
    y = i[1]
    flag = 0 # Set boolean to 0, indicating this value is still valid
    for j in points[counter:]: # Iteration2
        if int(j[1]) < y:
             flag = 1 # Set boolean to 1, indicating this value must be omitted
             break # No need to continue with iteration 2
    if flag == 0:
        accept.append(i)

这给出了输出:

[[0, 0], [9, 4], [1617, 4], [1790, 44], [5693, 249], [5782, 1471], [6068, 1471], [6105, 1560], [6298, 1700], [6390, 3765], [6416, 5926], [6446, 6440], [6503, 7300], [6511, 7332], [6522, 7342]]

我希望那是你想要的?

于 2013-04-17T13:32:53.953 回答
0

感谢大家的帮助,我刚刚发现我可以取平均 y/x 比率(最大 y 坐标)/最大值(x 坐标),在这种情况下约为 1.1 并过滤掉具有 y/x 的点偏离这个比率的比率:

valid=[v for v in points if (v[0]>0.9*v[1] and v[0]<1.5*v[1]) or v[0]==v[1]]
于 2013-04-17T13:54:55.503 回答