Compare video frames
I want to compare each frames of video so that I can detect movements. I am trying to do in OpenCV Python. it would be great if anyone can help me on this.
I want to compare each frames of video so that I can detect movements. I am trying to do in OpenCV Python. it would be great if anyone can help me on this.
This little program runs with python 3.5 using OpenCV 3.0 and compares image frames from an AVI file, displaying the difference image in a window. The differences are just scaled to fit, but you can come up with all kinds of variations on this.
import numpy as np
import cv2
# Capture video from file
cap=cv2.VideoCapture('/data/Code/Python/AAT/sim_imagery.avi')
old_frame = None
while True:
ret, frame = cap.read()
if ret == True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if old_frame is not None:
diff_frame = gray - old_frame
diff_frame -= diff_frame.min()
disp_frame = np.uint8(255.0*diff_frame/float(diff_frame.max()))
cv2.imshow('diff_frame',disp_frame)
old_frame = gray
if cv2.waitKey(30) & 0xFF == ord('q'):
break
else:
print('ERROR!')
break
cap.release()
cv2.destroyAllWindows()
Code:
import numpy as np
import cv2
cap = cv2.VideoCapture('/Users/nimya/Desktop/people-walking2.mp4')
ret, current_frame = cap.read()
fgbg = cv2.createBackgroundSubtractorMOG2()
previous_frame = current_frame
def framediff(current_frame_gray,previous_frame_gray):
frame_diff = cv2.absdiff(current_frame_gray,previous_frame_gray)
cv2.imshow('frame diff ',frame_diff)
return frame_diff
while(cap.isOpened()):
current_frame_gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY)
previous_frame_gray = cv2.cvtColor(previous_frame, cv2.COLOR_BGR2GRAY)
frame_diff=framediff(current_frame_gray,previous_frame_gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
previous_frame = current_frame.copy()
ret, current_frame = cap.read()
cap.release()
cv2.destroyAllWindows()
Asked: 2018-06-22 02:48:14 -0600
Seen: 13,250 times
Last updated: Feb 27 '20
cannot import xfeatures2d in OpenCV 3- Python binding
Opencv 3.0 dev python documentation [closed]
OpenCV-Python used in commercial application
Eye detection using OpenCV in python
What is the equivalent of cv::Mat in python?
Error at template matching in Python
How do I live stream wifi camera using python opencv ?
@Nimya, can you try to read over your question again, and improve it ? it's all too broad. movement of what ? compare ?
what are you trying to achieve ? what is the purpose ?
try do do some research on your own, like looking up optical flow.
I want to compare two frames in a video
sorry, too broad.
step 1:read two fames into two mats step 2:compare them