I'm trying to get the amount of motion by calculating the optical flow magnitude between two images (in Python 3.7 and cv2 v4.0). But passing the same images, I see the final value is not deterministic. Sometimes it prints inf and sometimes it prints 7.372749678324908e-05.
What is the problem? Why it's not deterministic?!
def getOpticalMag(prev_image, curr_image):
prev_image_gray = cv2.cvtColor(prev_image, cv2.COLOR_BGR2GRAY)
curr_image_gray = cv2.cvtColor(curr_image, cv2.COLOR_BGR2GRAY)
flow = cv2.calcOpticalFlowFarneback(prev_image_gray, curr_image_gray, flow=None,
pyr_scale=0.5, levels=1, winsize=15,
iterations=2,
poly_n=5, poly_sigma=1.1, flags=0)
mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
return np.mean(mag)
cv2.cartToPolar. Theflowis always the same for my images, but the conversion behaves strangely. I use OpenCV 4.5.1