-1

I have a function which takes all the points in particle flow and computes the difference. This is using openCV to track the way an object is moving within a video frame.

I am wanting to compute the angle at which the elements are moving within the frame. my following code is like so:

 # 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

I have a set of say 20 instances where the data is coming in like so in a 20x2 matrix. What i want to do is filter out any points which are heading in a direction not within 2 degrees of 0 or 180 degrees.

[[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  ]]

this is to purely track horizontal direction within the frame. the biggest issue I am having here is getting the function to perform row wise on each data set so I can filter the data. I am fairly new to python and would appreciate any help i can get.

Mitch Storrie
  • 131
  • 1
  • 10

1 Answers1

1

A very simple way to do it is something like this:

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)

Returns

[86.93456590483308, 85.12236824360552, 85.66283347933461, 84.97833049013788, 85.86636608956897, 86.42519659041078, 85.8620735792404, 86.59541811184934]
Eric M
  • 1,360
  • 11
  • 19