motion_crop.py
1.54 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import cv2
import numpy as np
cap = cv2.VideoCapture('./videos/test.mp4') #load video
count1 = 0 #counts number of frames
#capture two frames
rval, frame1 = cap.read()
rval, frame2 = cap.read()
while cap.isOpened():
diff = cv2.absdiff(frame1, frame2) #difference of two frames
gray = cv2.cvtColor(diff,cv2.COLOR_BGR2GRAY) #convert diff into greyscale
blur = cv2.GaussianBlur(gray, (5,5), 0) #blur image to reduce noise
_, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY) #set threshold
dilated = cv2.dilate(thresh, None, iterations=3) #fill holes in the thresholded image
#find contour
contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
count2 = 0 #counts number of motions in a frame
#further implementation(removing, rectangle)
for contour in contours:
(x, y, w, h) = cv2.boundingRect(contour) #values of contour
if cv2.contourArea(contour) < 7000: #to remove small unnecessary contours, (modify number for better results)
continue
filename = "clip{}-{}.jpg".format(count1,count2)
motion = frame1[y:y+h, x:x+w]
cv2.imwrite(filename, motion)
count2 += 1
#cv2.rectangle(frame1, (x, y), (x+w, y+h), (0, 0, 255), 2) #draw rectangle on frame1
count1 += 1
#show results
#cv2.imshow("feed", frame1)
#change to next frame
frame1 = frame2
rval, frame2 = cap.read()
#incase of error
if cv2.waitKey(40) == 27:
break
cap.release()
cv2.destroyAllWindows()