繁体   English   中英

在python中按行执行函数

[英]perform function row wise in python

我有一个函数,它获取粒子流中的所有点并计算差异。 这是使用 openCV 来跟踪对象在视频帧内移动的方式。

我想计算元素在框架内移动的角度。 我的以下代码是这样的:

 # Direction function. 
def direction(Dx, Dy):
    if (Dx > 0 and Dy > 0): 
        theta=math.atan(Dy/Dx)    
    elif (Dx < 0 and Dy > 0): 
        theta=math.atan(abs(Dx/Dy))+90*math.pi/180  
        print("Q2")    
    elif (Dx < 0 and Dy < 0): 
        theta=math.atan(abs(Dx/Dy))+180*math.pi/180
        print("Q3")   
    elif (Dx > 0 and Dy < 0): 
        theta=math.atan(abs(Dx/Dy))+270*math.pi/180
        print("Q4")       
    return theta*180/math.pi

我有一组 20 个实例,其中数据以 20x2 矩阵的形式传入。 我想要做的是过滤掉任何指向不在 0 度或 180 度的 2 度范围内的方向的点。

[[0.15859985 2.9615479 ]
 [0.23764038 2.784729  ]
 [0.22644043 2.9856567 ]
 [0.2416687  2.7503052 ]
 [0.21459961 2.9693756 ]
 [0.18655396 2.986145  ]
 [0.21240234 2.935913  ]
 [0.17762756 2.9857788 ]
 [0.20748901 2.8974915 ]
 [0.2079773  2.9221497 ]
 [0.27464294 2.7923584 ]
 [0.22880554 2.981659  ]
 [0.25639343 3.0173035 ]
 [0.19406128 2.9276123 ]
 [0.21862793 2.9695435 ]
 [0.24038696 2.7330017 ]
 [0.26013184 2.8272705 ]
 [0.2579422  3.0002136 ]
 [0.2731285  3.0145264 ]
 [0.17489624 2.989685  ]
 [0.31740952 3.005249  ]
 [0.20376587 3.0024414 ]
 [0.20892334 2.7820435 ]
 [0.22192383 2.7878723 ]
 [0.25608826 2.9936218 ]
 [0.23474121 2.8941154 ]
 [0.22346497 2.9910889 ]
 [0.24715424 3.0131226 ]
 [0.21356201 2.9688568 ]
 [0.21009064 3.0111084 ]
 [0.2687378  2.8110352 ]
 [0.3083496  2.744812  ]
 [0.32640076 2.929657  ]
 [0.17825317 2.978836  ]
 [0.20071411 2.7944946 ]
 [0.23487854 3.0315552 ]
 [0.22006226 2.9369202 ]
 [0.24085999 2.7827759 ]
 [0.24176025 2.8339844 ]
 [0.18780518 2.822998  ]
 [0.29027557 2.9614258 ]
 [0.20407104 2.8983154 ]
 [0.24299622 2.9848633 ]
 [0.24712372 3.0074768 ]
 [0.26487732 2.9702759 ]
 [0.20198059 3.0663452 ]
 [0.24079895 2.8884888 ]
 [0.19445801 2.7364197 ]
 [0.21140099 3.043457  ]]

这是纯粹跟踪框架内的水平方向。 我在这里遇到的最大问题是让函数在每个数据集上按行执行,以便我可以过滤数据。 我对 python 相当陌生,希望得到任何帮助。

一个非常简单的方法是这样的:

import numpy as np
import math

# Direction function. 
def direction(row):
    Dx, Dy = row[0], row[1]
    if (Dx > 0 and Dy > 0): 
        theta=math.atan(Dy/Dx)    
    elif (Dx < 0 and Dy > 0): 
        theta=math.atan(abs(Dx/Dy))+90*math.pi/180  
        print("Q2")    
    elif (Dx < 0 and Dy < 0): 
        theta=math.atan(abs(Dx/Dy))+180*math.pi/180
        print("Q3")   
    elif (Dx > 0 and Dy < 0): 
        theta=math.atan(abs(Dx/Dy))+270*math.pi/180
        print("Q4")       
    return theta*180/math.pi


data = np.array([[0.15859985, 2.9615479],
 [0.23764038, 2.784729],
 [0.22644043, 2.9856567],
 [0.2416687,  2.7503052],
 [0.21459961, 2.9693756],
 [0.18655396, 2.986145],
 [0.21240234, 2.935913],
 [0.17762756, 2.9857788]])

result = []
for row in data:
    result.append(direction(row))
print(result)

退货

[86.93456590483308, 85.12236824360552, 85.66283347933461, 84.97833049013788, 85.86636608956897, 86.42519659041078, 85.8620735792404, 86.59541811184934]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM