-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_video_processing.py
162 lines (135 loc) · 5.99 KB
/
basic_video_processing.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""Basic Video Processing methods."""
import os
import cv2
# Replace ID1 and ID2 with your IDs.
ID1 = '308345891'
ID2 = '211670849'
INPUT_VIDEO = 'atrium.avi'
GRAYSCALE_VIDEO = f'{ID1}_{ID2}_atrium_grayscale.avi'
BLACK_AND_WHITE_VIDEO = f'{ID1}_{ID2}_atrium_black_and_white.avi'
SOBEL_VIDEO = f'{ID1}_{ID2}_atrium_sobel.avi'
def get_video_parameters(capture: cv2.VideoCapture) -> dict:
"""Get an OpenCV capture object and extract its parameters.
Args:
capture: cv2.VideoCapture object. The input video's VideoCapture.
Returns:
parameters: dict. A dictionary of parameters names to their values.
"""
fourcc = int(capture.get(cv2.CAP_PROP_FOURCC))
fps = int(capture.get(cv2.CAP_PROP_FPS))
height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
parameters = {"fourcc": fourcc, "fps": fps, "height": height, "width": width}
return parameters
def convert_video_to_grayscale(input_video_path: str,
output_video_path: str) -> None:
"""Convert the video in the input path to grayscale.
Use VideoCapture from OpenCV to open the video and read its
parameters using the capture's get method.
Open an output video using OpenCV's VideoWriter.
Iterate over the frames. For each frame, convert it to gray scale,
and save the frame to the new video.
Make sure to close all relevant captures and to destroy all windows.
Args:
input_video_path: str. Path to input video.
output_video_path: str. Path to output video.
Additional References:
(1) What are fourcc parameters:
https://docs.microsoft.com/en-us/windows/win32/medfound/video-fourccs
"""
"""INSERT YOUR CODE HERE."""
cap = cv2.VideoCapture(input_video_path)
if cap.isOpened()== False:
print("Error open video file")
exit(-1)
param = get_video_parameters(cap)
out = cv2.VideoWriter(output_video_path, fourcc=param["fourcc"], fps=param["fps"], frameSize=(param["width"],
param["height"]), isColor=False)
while True:
ret, frame = cap.read()
if ret:
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
out.write(gray_frame)
else:
break
cap.release()
out.release()
def convert_video_to_black_and_white(input_video_path: str,
output_video_path: str) -> None:
"""Convert the video in the input path to black and white.
Use VideoCapture from OpenCV to open the video and read its
parameters using the capture's get method.
Open an output video using OpenCV's VideoWriter.
Iterate over the frames. For each frame, first convert it to gray scale,
then use OpenCV's THRESH_OTSU to slice the gray color values to
black (0) and white (1) and finally convert the frame format back to RGB.
Save the frame to the new video.
Make sure to close all relevant captures and to destroy all windows.
Args:
input_video_path: str. Path to input video.
output_video_path: str. Path to output video.
Additional References:
(1) What are fourcc parameters:
https://docs.microsoft.com/en-us/windows/win32/medfound/video-fourccs
"""
"""INSERT YOUR CODE HERE."""
cap = cv2.VideoCapture(input_video_path)
if cap.isOpened()== False:
print("Error open video file")
exit(-1)
param = get_video_parameters(cap)
out = cv2.VideoWriter(output_video_path, fourcc=param["fourcc"], fps=param["fps"], frameSize=(param["width"],
param["height"]), isColor=False)
while True:
ret, frame = cap.read()
if ret:
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray_frame, (3, 3), 0)
ret3, binary_frame = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
out.write(binary_frame)
else:
break
cap.release()
out.release()
def convert_video_to_sobel(input_video_path: str,
output_video_path: str) -> None:
"""Convert the video in the input path to sobel map.
Use VideoCapture from OpenCV to open the video and read its
parameters using the capture's get method.
Open an output video using OpenCV's VideoWriter.
Iterate over the frames. For each frame, first convert it to gray scale,
then use OpenCV's THRESH_OTSU to slice the gray color values to
black (0) and white (1) and finally convert the frame format back to RGB.
Save the frame to the new video.
Make sure to close all relevant captures and to destroy all windows.
Args:
input_video_path: str. Path to input video.
output_video_path: str. Path to output video.
Additional References:
(1) What are fourcc parameters:
https://docs.microsoft.com/en-us/windows/win32/medfound/video-fourccs
"""
"""INSERT YOUR CODE HERE"""
cap = cv2.VideoCapture(input_video_path)
if cap.isOpened()== False:
print("Error open video file")
exit(-1)
param = get_video_parameters(cap)
out = cv2.VideoWriter(output_video_path, fourcc=param["fourcc"], fps=param["fps"], frameSize=(param["width"],
param["height"]), isColor=False)
while True:
ret, frame = cap.read()
if ret:
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
sobel = cv2.Sobel(gray_frame, dx=1, dy=1, ddepth=-1, ksize=5)
out.write(sobel)
else:
break
cap.release()
out.release()
def main():
convert_video_to_grayscale(INPUT_VIDEO, GRAYSCALE_VIDEO)
convert_video_to_black_and_white(INPUT_VIDEO, BLACK_AND_WHITE_VIDEO)
convert_video_to_sobel(INPUT_VIDEO, SOBEL_VIDEO)
if __name__ == "__main__":
main()