I am using a dense optical flow algorithm in order to calculate the optical flow on a given video, after I run the algorithm I receive the output bellow.
I would like to find a way to sum up the changes between two frames (or two sets of vectors (in pixel units)), meaning to find a numerical value to the change between frames in order to determine if these two frames are "similar" or "different"
this is the output (from what I understand, for each pixel it is the change x, y basically):
flow
[[[ 0.00080293 0.00456178]
[ 0.0023454 0.00762859]
[ 0.00337119 0.01088941]
...
[ 0.08646814 0.17195833]
[ 0.07680464 0.15070145]
[ 0.04990056 0.09711792]]
[[ 0.00197109 0.00610898]
[ 0.00431191 0.01074001]
[ 0.00629149 0.01567514]
...
[ 0.11541913 0.23083425]
[ 0.10006026 0.19827926]
[ 0.06407876 0.12646647]]
[[ 0.00333168 0.0071025 ]
[ 0.00625938 0.01281219]
[ 0.01047979 0.02093185]
...
[ 0.15598673 0.31461456]
[ 0.1284331 0.25725985]
[ 0.08006614 0.16013806]]
...
[[-0.11634359 0.09029744]
[-0.14934781 0.11287674]
[-0.24678642 0.17862432]
...
[ 0.00260158 0.00103487]
[ 0.00391656 0.00041338]
[ 0.00312206 0.00064316]]
[[-0.06021533 0.04847184]
[-0.07352059 0.05851178]
[-0.12553327 0.09319763]
...
[ 0.00314228 -0.00119414]
[ 0.00410303 -0.00139949]
[ 0.00334636 -0.00098234]]
[[-0.0192373 0.010998 ]
[-0.02326458 0.01555626]
[-0.04161371 0.02764582]
...
[ 0.00236979 -0.00039244]
[ 0.00327405 -0.00078911]
[ 0.00281549 -0.00057979]]]
flow
[[[-8.4514404e-03 -9.1092577e-03]
[-8.2096420e-03 -1.6217180e-02]
[-9.7641135e-03 -2.3235001e-02]
...
[ 8.4836602e-02 9.4629139e-02]
[ 7.0593305e-02 7.2248474e-02]
[ 6.2410351e-02 5.8204494e-02]]
[[-1.6573617e-02 -1.5174728e-02]
[-1.5833536e-02 -2.2253623e-02]
[-1.7538801e-02 -3.1138226e-02]
...
[ 1.3201687e-01 1.3085920e-01]
[ 1.1270510e-01 1.0012541e-01]
[ 1.0345179e-01 8.3722569e-02]]
[[-2.1787306e-02 -2.0292744e-02]
[-2.2391599e-02 -2.8152039e-02]
[-2.3549989e-02 -3.8980592e-02]
...
[ 1.5739001e-01 1.6933599e-01]
[ 1.3471533e-01 1.2855931e-01]
[ 1.2196152e-01 1.0327549e-01]]
...
[[-3.9006339e-03 -3.0767643e-03]
[-1.8084457e-02 -8.7532159e-03]
[-4.0460575e-02 -1.6521217e-02]
...
[ 5.4473747e-03 -1.9708525e-03]
[ 4.3195980e-03 -1.6532388e-03]
[ 2.4038905e-03 -2.6415614e-04]]
[[-2.2322503e-03 -3.0169063e-03]
[-1.1787469e-02 -8.9037549e-03]
[-2.8192652e-02 -1.6921449e-02]
...
[ 1.9799198e-03 -3.8150212e-04]
[ 1.5747466e-03 -5.4049061e-04]
[ 9.2306529e-04 -1.1204407e-04]]
[[-1.1798806e-03 -1.9108414e-03]
[-6.6612735e-03 -5.3157108e-03]
[-1.6056010e-02 -9.3358066e-03]
...
[ 4.8137631e-04 6.4036541e-04]
[ 3.4130082e-04 3.7227676e-04]
[ 1.7955518e-04 1.8480681e-04]]]...
this id the code we are using for the optical flow calculation:
def _calc_optical_flow_(distance_array):
cap = cv2.VideoCapture("videos/3.1.avi")
output_file_text = open("output.txt", "w+")
ret, frame1 = cap.read()
prvs = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
hsv = np.zeros_like(frame1)
frames_array.append(frame1)
hsv[..., 1] = 255
count = 0
distance = 0
while(1):
ret, frame2 = cap.read()
if frame2 is None:
break
frames_array.append(frame2)
next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)
flow = cv2.calcOpticalFlowFarneback(prvs, next, None, pyr_scale=0.5, levels=3, winsize=15, iterations=1,
poly_n=5, poly_sigma=1.2, flags=0)
mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
hsv[..., 0] = ang*180/np.pi/2
hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
if count == 10:
count = 0
output_file_text.write("flow\n")
output_file_text.write(np.array_str(flow) + "\n")
distance = function(flow, distance)
distance_array.append(distance)
#print ("flow",flow)
cv2.imshow('frame2',rgb)
count=count+1
k = cv2.waitKey(10) & 0xff
if k == 27:
break
elif k == ord('s'):
#cv2.imwrite('opticalfb.png', frame2)
#cv2.imwrite('opticalhsv.png', rgb)
prvs = next
output_file_text.close()
cap.release()
cv2.destroyAllWindows()
return distance_array