如何在Python中比较两条3D曲线? [英] How to compare two 3D curves in Python?

查看:55
本文介绍了如何在Python中比较两条3D曲线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个巨大的数组,其中的坐标描述了3D曲线,〜20000点.我正尝试使用较少的积分,而忽略了一些积分,比如说每2积分取1.当我这样做并绘制减少的点数时,形状看起来是相同的.但是,我想适当地比较两条曲线,类似于卡方检验,以查看缩小的图与原始图有多少不同.

是否有一种简单的内置方法来执行此操作,或者是否有人对如何解决此问题有任何想法.

解决方案

行简化"的一般问题似乎是整个研究领域.我建议您看一下例如

  def normed_distance_along_path(polyline):折线= np.asarray(折线)距离= np.cumsum(np.sqrt(np.sum(np.diff(折线,轴= 1)** 2,轴= 0)))返回np.insert(distance,0,0)/distance [-1]def average_distance_between_polylines(xy1,xy2):s1 = normed_distance_along_path(xy1)s2 = normed_distance_along_path(xy2)interpol_xy1 = interp1d(s1,xy1)xy1_on_2 = interpol_xy1(s2)node_to_node_distance = np.sqrt(np.sum((xy1_on_2-xy2)** 2,轴= 0))返回node_to_node_distance.mean()#或使用最大#两个折线示例:xy1 = [0,1,8,2,1.7],[1,0,6,7,1.9]#它也应该在3D模式下工作xy2 = [.1,.6,4,8.3,2.1,2.2,2],[.8,.1,2,6.4,6.7,4.4,2.3]average_distance_between_polylines(xy1,xy2)#0.45004578069119189 

I have a huge array with coordinates describing a 3D curves, ~20000 points. I am trying to use less points, by ignoring some, say take 1 every 2 points. When I do this and I plot the reduced number of points the shape looks the same. However I would like to compare the two curves properly, similar to the chi squared test to see how much the reduced plot differs from the original.

Is there an easy, built-in way of doing this or does anyone have any ideas on how to approach the problem.

解决方案

The general question of "line simplification" seems to be an entire field of research. I recommend you to have a look, for instance, at the Ramer–Douglas–Peucker algorithm. There are several python modules, I could find: rdp and simplification (which also implement the Py-Visvalingam-Whyatt algorithm).

Anyway, I am trying something for evaluating the difference between two polylines, using interpolation. Any curves can be compared, even without common points.

The first idea is to compute the distance along the path for both polylines. They are used as landmarks to go from one given point on the first curve to a relatively close point on the other curve.

Then, the points of the first curve can be interpolated on the other curve. These two datasets can now be compared, point by point.

On the graph, the black curve is the interpolation of xy2 on the curve xy1. So the distances between the black squares and the orange circles can be computed, and averaged.

This gives an average distance measure, but nothing to compare against and decide if the applied reduction is good enough...

def normed_distance_along_path( polyline ):
    polyline = np.asarray(polyline)
    distance = np.cumsum( np.sqrt(np.sum( np.diff(polyline, axis=1)**2, axis=0 )) )
    return np.insert(distance, 0, 0)/distance[-1]

def average_distance_between_polylines(xy1, xy2):   
    s1 = normed_distance_along_path(xy1)
    s2 = normed_distance_along_path(xy2)

    interpol_xy1 = interp1d( s1, xy1 )
    xy1_on_2 = interpol_xy1(s2)

    node_to_node_distance = np.sqrt(np.sum( (xy1_on_2 - xy2)**2, axis=0 ))

    return node_to_node_distance.mean() # or use the max

# Two example polyline:
xy1 = [0, 1, 8, 2, 1.7],  [1, 0, 6, 7, 1.9]   # it should work in 3D too
xy2 = [.1, .6, 4, 8.3, 2.1, 2.2, 2],  [.8, .1, 2, 6.4, 6.7, 4.4, 2.3]

average_distance_between_polylines(xy1, xy2)  # 0.45004578069119189

这篇关于如何在Python中比较两条3D曲线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆