-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathOpenCV_getting_started.py
216 lines (158 loc) · 5.37 KB
/
OpenCV_getting_started.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""
OpenCV Python Tutorial
Read the story here:
https://analyticsindiamag.com/getting-started-with-opencv-in-python/
"""
import cv2
import os
os.chdir('C:/Users/Nandhini/Python2021')
# read an image
img = cv2.imread('daria.jpg', 0)
# display the image
cv2.imshow('Image Window', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# write the image
cv2.imwrite('daria_copy.jpg', img)
# ---------------------------------------------------------------------
# Date-Time
from datetime import datetime
text = str(datetime.now())
# read a video from file
capture = cv2.VideoCapture('drive_6.mp4')
# display the read video file
while capture.isOpened():
ret, frame = capture.read()
# add date-time to the frames
frame = cv2.putText(frame, text, (30,40), cv2.FONT_HERSHEY_PLAIN, 1, (0,0,255), 2)
if not ret:
break
cv2.imshow('Video Window', frame)
cv2.waitKey(30)
capture.release()
cv2.destroyAllWindows()
# ---------------------------------------------------------------------
# read a video and write it to another file
capture = cv2.VideoCapture('swan.mp4')
# get frame properties
print(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
print(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(capture.get(cv2.CAP_PROP_FRAME_COUNT))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output_1.avi', fourcc, 20.0, (640,360))
while capture.isOpened():
ret, frame = capture.read()
out.write(frame)
if not ret:
break
cv2.imshow('Video Window', frame)
cv2.waitKey(25)
capture.release()
out.release()
cv2.destroyAllWindows()
# ---------------------------------------------------------------------
# read a coloured image
img = cv2.imread('mason.jpg', 1)
print(img.shape)
# draw a green vertical line in it
# arguments: image, start, end, colour, thickness
img = cv2.line(img, (50,100), (50,300), (0,255,0), 10)
# display the image
cv2.imshow('Image Window', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# ---------------------------------------------------------------------
img = cv2.imread('mason.jpg', 1)
# draw a green vertical line in it
# arguments: image, start, end, colour, thickness
img = cv2.line(img, (50,100), (50,300), (0,255,0), 5)
# draw a blue circle on it
# arguments: image, centre, radius, colour, thickness
img = cv2.circle(img, (150,250), 60, (255,0,0), 5)
# draw a red rectangle on it
# arguments: image, diagonal_start, diagonal_end, colour, thickness
img = cv2.rectangle(img, (300,140), (400,270), (0,0,255), 5)
# display the image
cv2.imshow('Image Window', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# ---------------------------------------------------------------------
img = cv2.imread('senjuti.jpg')
img = cv2.resize(img,(300,450))
# display the Original image without Text
cv2.imshow('Original Image', img)
text_image = cv2.putText(img, 'I love Colours', (40,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255),2)
# display the image with Text on it
cv2.imshow('Image with Text', text_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# ---------------------------------------------------------------------
# read a video from file
capture = cv2.VideoCapture('swan.mp4')
# display the read video file
while capture.isOpened():
ret, frame = capture.read()
if not ret:
break
cv2.imshow('Video Window', frame)
cv2.waitKey(30)
capture.release()
cv2.destroyAllWindows()
# ---------------------------------------------------------------------
# coffee cup alteration
img = cv2.imread('brooke.jpg')
img = cv2.resize(img,(320,480))
coffee = img[150:235,200:300]
alter = img.copy()
alter[0:85,0:100] = coffee
alter[85:170,0:100] = coffee
alter[170:255,0:100] = coffee
alter[20:105,220:320] = coffee
# display the Original image
cv2.imshow('Original Image', img)
# display the altered image
cv2.imshow('Altered Image', alter)
cv2.waitKey(0)
cv2.destroyAllWindows()
# ---------------------------------------------------------------------
# read an image
img = cv2.imread('daria.jpg', 1)
img = cv2.resize(img, (320,225))
# split the colour image and merge back
B, G, R = cv2.split(img)
img_BGR = cv2.merge((B,G,R))
img_RGB = cv2.merge((R,G,B))
img_BRG = cv2.merge((B,R,G))
# display the images
cv2.imshow('Image in BGR', img_BGR)
cv2.imshow('Image in RGB', img_RGB)
cv2.imshow('Image in BRG', img_BRG)
cv2.waitKey(0)
cv2.destroyAllWindows()
# ---------------------------------------------------------------------
# read two images and resize them
img_1 = cv2.imread('senjuti.jpg', 1)
img_1 = cv2.resize(img_1, (300,450))
img_2 = cv2.imread('adriano.jpg',1)
img_2 = cv2.resize(img_2, (300,450))
img_1 = cv2.imread('xuan.jpg', 1)
img_1 = cv2.resize(img_1, (320,225))
img_2 = cv2.imread('daria.jpg',1)
img_2 = cv2.resize(img_2, (320,225))
# display original images
cv2.imshow('Image 1', img_1)
cv2.imshow('Image 2', img_2)
cv2.waitKey(0)
cv2.destroyAllWindows()
# simple addition
simple = cv2.add(img_1, img_2)
cv2.imshow('Simple Addition', simple)
cv2.waitKey(0)
cv2.destroyAllWindows()
# weighted addition
weight_70 = cv2.addWeighted(img_1, 0.7, img_2, 0.3, 0)
weight_30 = cv2.addWeighted(img_1, 0.3, img_2, 0.7, 0)
cv2.imshow('70-30 Addition', weight_70)
cv2.imshow('30-70 Addition', weight_30)
cv2.waitKey(0)
cv2.destroyAllWindows()