-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_contours.py
42 lines (34 loc) · 1.2 KB
/
video_contours.py
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
# Capture Video from Camera
import numpy as np
import cv2 as cv
cap = cv.VideoCapture('240fps.mp4')
# if not cap.isOpened():
# print("Cannot open camera")
# exit()
width = cap.get(cv.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv.CAP_PROP_FRAME_HEIGHT)
fps = cap.get(cv.CAP_PROP_FPS)
print(f"width: {width}, height: {height}, fps: {fps}")
fourcc = cv.VideoWriter_fourcc(*'mp4v')
out = cv.VideoWriter('contours_240.mp4', fourcc, fps, (int(width), int(height)))
while cap.isOpened():
# Capture frame-by-frame
ret, frame = cap.read()
# if frame is read correctly ret is True
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
# Display the resulting frame
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(gray, 100, 255, cv.THRESH_BINARY)
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
with_contours = cv.drawContours(frame, contours, -1, (0, 255, 0), 1)
# Show keypoints
out.write(frame)
cv.imshow("contours", with_contours)
if cv.waitKey(1) == ord('q'):
break
# When everything done, release the capture
cap.release()
out.release()
cv.destroyAllWindows()