-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathipc_s01e01.py
More file actions
140 lines (107 loc) · 3.7 KB
/
Copy pathipc_s01e01.py
File metadata and controls
140 lines (107 loc) · 3.7 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
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
import cv2
from matplotlib import pyplot as plt
def ex_0():
cap = cv2.VideoCapture(0) # open the default camera
key = ord('a')
while key != ord('q'):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame comes here
# Convert RGB image to grayscale
img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Blur the image
img_filtered = cv2.GaussianBlur(img_gray, (7, 7), 1.5)
# Detect edges on the blurred image
img_edges = cv2.Canny(img_filtered, 0, 30, 3)
# Display the result of our processing
cv2.imshow('result', img_edges)
# Wait a little (30 ms) for a key press - this is required to refresh the image in our window
key = cv2.waitKey(30)
# When everything done, release the capture
cap.release()
# and destroy created windows, so that they are not left for the rest of the program
cv2.destroyAllWindows()
def ex_1():
# TODO 1
img_from_file = cv2.imread('../_data/no_idea.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imshow('img_from_file', img_from_file)
cv2.waitKey(0)
cv2.imwrite('../_data/no_idea_grayscale.png', img_from_file)
cv2.destroyAllWindows()
def ex_2():
img_color = cv2.imread('../_data/no_idea.jpg', cv2.IMREAD_COLOR)
img_grayscale = cv2.imread('../_data/no_idea.jpg', cv2.IMREAD_GRAYSCALE)
# TODO 2
print(f'Color image parameters: {img_color.shape}')
print(f'Grayscale image parameters: {img_grayscale.shape}')
print(f'Pixel (220, 270) value - color: {img_color[220, 270]} , grayscale: {img_grayscale[220, 270]}')
cv2.imshow('img_color', img_color)
cv2.imshow('img_grayscale', img_grayscale)
_ = cv2.waitKey(0)
cv2.destroyAllWindows()
# TODO 3
head = img_color[10:120, 250:420]
img_with_head = img_color.copy()
img_with_head[60:170, 50:220] = head
cv2.imshow('head', head)
cv2.imshow('img_with_head', img_with_head)
_ = cv2.waitKey(0)
cv2.destroyAllWindows()
# TODO 4
cv2.imshow('img_color', img_color)
cv2.waitKey(1)
# Convert image to RGB for correct Matplotlib display
img_rgb = cv2.cvtColor(img_color, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(img_color)
plt.title('BGR (Incorrect)')
plt.xticks([]), plt.yticks([])
plt.subplot(1, 2, 2)
plt.imshow(img_rgb)
plt.title('RGB (Correct)')
plt.xticks([]), plt.yticks([])
plt.tight_layout()
plt.show()
cv2.destroyAllWindows()
# TODO 5
img_bgr = cv2.imread('../_data/s01e01/AdditiveColor.png', cv2.IMREAD_COLOR)
img_bgr = cv2.resize(img_bgr, (0, 0), fx=0.5, fy=0.5)
b, g, r = cv2.split(img_bgr)
cv2.imshow('img_bgr', img_bgr)
cv2.imshow('b', b)
cv2.imshow('g', g)
cv2.imshow('r', r)
_ = cv2.waitKey(0)
cv2.destroyAllWindows()
def ex_3():
# TODO 6
cap = cv2.VideoCapture(0)
key = ord(' ')
# NOTE(MF): thanks to the way key variable is initialized we do not need to load one frame before the loop
while key != ord('q'):
if key == ord(' '):
_, frame = cap.read()
cv2.imshow('video_frame', frame)
key = cv2.waitKey(10)
cap.release()
cv2.destroyAllWindows()
# TODO 7
cap = cv2.VideoCapture('../_data/s01e01/Wildlife.mp4')
key = ord(' ')
ret = True
while key != ord('q') and ret:
if key == ord(' '):
ret, frame = cap.read()
if ret:
cv2.imshow('video_frame', frame)
else:
print('End of video file!')
key = cv2.waitKey(10)
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
ex_0()
ex_1()
ex_2()
ex_3()