-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_difference_in_frames.py
More file actions
37 lines (28 loc) · 1.08 KB
/
detect_difference_in_frames.py
File metadata and controls
37 lines (28 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import cv2
# Read the video file / source
cap = cv2.VideoCapture(0)
# Capture the 1st & 2nd frames and store them in resp. variables:
ret1, frame1 = cap.read()
ret2, frame2 = cap.read()
# Loop the capture of frames:
while True:
# Convert the frame1 & frame2 into gray scale to calculate differences:
frame1_gray = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
frame2_gray = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
# Apply the Gaussian blur onto the gray scale frames
# Kernel size is 21*21 which applies a stronger level of blurring:
frame1_blur = cv2.GaussianBlur(frame1_gray, (21, 21), 0)
frame2_blur = cv2.GaussianBlur(frame2_gray, (21, 21), 0)
# Calculate the difference between the two frames:
frames_diff = cv2.absdiff(frame1_blur, frame2_blur)
# Display the difference in an open window:
cv2.imshow("Motion Captured:", frames_diff)
# Repeat the same for the upcoming frames in the video:
frame1 = frame2
ret, frame2 = cap.read()
if not ret:
break
k = cv2.waitKey(10)
if k == ord('q'):
break
cv2.destroyAllWindows()