微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有没有更快的方法来计算两点矩阵的距离平方和?

如何解决有没有更快的方法来计算两点矩阵的距离平方和?

我正在尝试使以下代码运行得更快。 该代码试图通过计算每帧有 137 个点 (x,y) 的两帧的平方距离之和来尝试找到两个对象的最近帧。 填充距离矩阵后,我正在寻找矩阵中的最小距离并返回此条目的 indee。

enter code here
def getSqurareddistancesSum(frame1,frame2):
  sum = 0
  distance = 0
  for i in range(0,137):
      x1 = frame1[i][0]
      y1 = frame1[i][1]
      x2 = frame2[i][0]
      y2 = frame2[i][1]
      sum += math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
  return sum

def get_closest_frames(dataobj1,dataobj2,endpoint,startpoint):
    nf1 = len(dataobj1.body.data)
    nf2 = len(dataobj2.body.data)
    window1size= int(0.15*nf1) 
    window2size = int(0.15*nf2)  
    distancematrix = np.zeros(shape=(window1size,window2size))
    for i in range(endpoint - window1size,endpoint):
      for j in range(startpoint,startpoint + window2size):
        d = getSqurareddistancesSum(dataobj1.body.data[i][0],dataobj2.body.data[j][0])
        distancematrix[i - (endpoint - window1size)][j - startpoint] = d
    min = 1000000
    newstartpoint = startpoint
    newendpoint = endpoint
    for i in range(0,window1size):
      for j in range(0,window2size):
        if distancematrix[i][j] <= min:
            min = distancematrix[i][j]
            newstartpoint = j + startpoint
            newendpoint = i + (endpoint - window1size)
    return newstartpoint,newendpoint

解决方法

我无法完全理解问题想要达到的目标。要获取两组点的所有组合之间的差异,这可能会有所帮助。通过将 x 和 y 坐标转换为复数,numpy 可以将您的帧作为 2 1D 数组处理。减法生成所有 f1 - f2 差值。

如果这没有帮助,请尝试显示少量测试数据集和预期测试结果。

import numpy as np 

# Create some test data
np.random.seed( 1234 )
frame1 = np.array( [ np.random.rand( ) * 100 + 50,np.random.rand( ) * 100 + 50 ] )

frame2 = np.array( [ np.random.rand( ) * 100 + 50,np.random.rand( ) * 100 + 50 ] )
frame1                                                                                            
# array([[ 69.15194504,112.2108771,93.7727739 ],#        [128.53585837,127.99758081,77.25926053]])

frame2                                                                                            
# array([[ 77.64642551,130.18721775,145.81393537],#        [137.59326347,85.781727,100.09951255]])


f1 = frame1[ 0 ] + frame1[ 1 ] * 1j  # Make each x & y coord a complex number
f2 = frame2[ 0 ] + frame2[ 1 ] * 1j  # Make each x & y coord a complex number 
f1                                                                                                
# array([ 69.15194504+128.53585837j,112.2108771 +127.99758081j,# 93.7727739  +77.25926053j])
f2                                                                                                
# array([ 77.64642551+137.59326347j,130.18721775 +85.781727j,# 145.81393537+100.09951255j])

differences = f1 - f2[:,None]  # complex differences,2 dimensions 3 x 3
differences 

# array([[ -8.49448048 -9.0574051j,34.56445159 -9.59568266j,#          16.12634839-60.33400295j],#        [-61.03527272+42.75413138j,-17.97634065+42.21585382j,#         -36.41444385 -8.52246647j],#        [-76.66199033+28.43634582j,-33.60305826+27.89806826j,#         -52.04116147-22.84025202j]])

distances = np.abs( differences )

print( distances,'\n' )                                                                                    
# [[12.41743878 35.87169413 62.45198975]
#  [74.519932   45.88384396 37.39845125]
#  [81.76604751 43.67456625 56.83273352]] 

print( distances.sum( axis = 0 ),'\n',distances.sum( axis = 1 ) )
#  [168.70341828 125.43010434 156.68317452] # Sum down columns 
#  [110.74112265 157.80222721 182.27334728] # Sum across rows

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。