-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathOpenCV_Image_Processing.py
230 lines (148 loc) · 5.14 KB
/
OpenCV_Image_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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""
OpenCV Python Tutorial
# IMAGE PROCESSING WITH OPENCV PYTHON
Read the story here:
https://analyticsindiamag.com/image-processing-with-opencv-in-python/
"""
import cv2
import os
os.chdir('C:/Users/Nandhini/Python2021')
# read an image in grayscale
img = cv2.imread('daria.jpg', 0)
img = cv2.resize(img, (320,225))
# apply various thresholds
val, th1 = cv2.threshold(img, 110, 255, cv2.THRESH_BINARY)
val, th2 = cv2.threshold(img, 110, 255, cv2.THRESH_BINARY_INV)
val, th3 = cv2.threshold(img, 110, 255, cv2.THRESH_TRUNC)
val, th4 = cv2.threshold(img, 110, 255, cv2.THRESH_TOZERO)
val, th5 = cv2.threshold(img, 110, 255, cv2.THRESH_TOZERO_INV)
# display the images
cv2.imshow('Original', img)
cv2.imshow('THRESH_BINARY', th1)
cv2.imshow('THRESH_BINARY_INV', th2)
cv2.imshow('THRESH_TRUNC', th3)
cv2.imshow('THRESH_TOZERO', th4)
cv2.imshow('THRESH_TOZERO_INV', th5)
cv2.waitKey(0)
cv2.destroyAllWindows()
# --------------------------------------------------------------
img = cv2.imread('daria.jpg', 0)
img = cv2.resize(img, (320,225))
# apply various adaptive thresholds
th1 = cv2.adaptiveThreshold(img, 255, \
cv2.ADAPTIVE_THRESH_MEAN_C, \
cv2.THRESH_BINARY, 7, 4)
th2 = cv2.adaptiveThreshold(img, 255, \
cv2.ADAPTIVE_THRESH_GAUSSIAN_C, \
cv2.THRESH_BINARY, 7, 4)
# display the images
cv2.imshow('ADAPTIVE_THRESHOLD_MEAN', th1)
cv2.imshow('ADAPTIVE_THRESHOLD_GAUSSIAN', th2)
cv2.waitKey(0)
cv2.destroyAllWindows()
# --------------------------------------------------------------
img = cv2.imread('tree.png', 0)
# Apply median blur
img1 = cv2.medianBlur(img,3)
# display the images
cv2.imshow('Original', img)
cv2.imshow('Median', img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
# --------------------------------------------------------------
img = cv2.imread('sharon.jpg', 1)
img = cv2.resize(img, (300,300))
# Apply blur
img1 = cv2.blur(img,(3,3))
# display the images
cv2.imshow('Original', img)
cv2.imshow('Blur', img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
# --------------------------------------------------------------
img = cv2.imread('keiron.jpg', 1)
img = cv2.resize(img, (320,210))
# Apply Gaussian blur
img1 = cv2.GaussianBlur(img,(5,5),2)
# display the images
cv2.imshow('Original', img)
cv2.imshow('Gaussian', img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
# --------------------------------------------------------------
img = cv2.imread('keiron.jpg', 1)
img = cv2.resize(img, (320,210))
# Apply Bilateral Filter
img1 = cv2.bilateralFilter(img,7,100,100)
# display the images
cv2.imshow('Original', img)
cv2.imshow('Bilateral', img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
# --------------------------------------------------------------
import numpy as np
img = cv2.imread('chessboard.jpg', 0)
img = cv2.resize(img, (300,200))
# Laplacian image gradient
lap = np.uint8(np.absolute(cv2.Laplacian(img,cv2.CV_64F, ksize=1)))
# display the images
cv2.imshow('Original', img)
cv2.imshow('Lpalacian', lap)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread('chessboard.jpg', 0)
img = cv2.resize(img, (300,200))
# Sobel image gradient
vertical = np.uint8(np.absolute(cv2.Sobel(img,cv2.CV_64F, 1,0, ksize=1)))
horizon = np.uint8(np.absolute(cv2.Sobel(img,cv2.CV_64F, 0,1, ksize=1)))
# display the images
cv2.imshow('Vertical', vertical)
cv2.imshow('Horizontal', horizon)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sobel = cv2.bitwise_or(vertical, horizon)
cv2.imshow('Sobel', Sobel)
cv2.waitKey(0)
cv2.destroyAllWindows()
# --------------------------------------------------------------
img = cv2.imread('chessboard.jpg', 0)
img = cv2.resize(img, (450,300))
def null(x):
pass
# create trackbars to control threshold values
cv2.namedWindow('Canny')
cv2.resizeWindow('Canny', (450,300))
cv2.createTrackbar('MIN', 'Canny', 80,255, null)
cv2.createTrackbar('MAX', 'Canny', 120,255, null)
while True:
# get Trackbar position
a = cv2.getTrackbarPos('MIN', 'Canny')
b = cv2.getTrackbarPos('MAX', 'Canny')
# Canny Edge detection
# arguments: image, min_val, max_val
canny = cv2.Canny(img,a,b)
# display the images
cv2.imshow('Canny', canny)
k = cv2.waitKey(1) & 0xFF
if k == ord('q'):
break
cv2.destroyAllWindows()
# --------------------------------------------------------------
img = cv2.imread('valerie.jpg', 1)
img = cv2.resize(img, (320,480))
# show original image
cv2.imshow('Original', img)
# binary thresholding
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
val,th = cv2.threshold(gray, 127,255,cv2.THRESH_BINARY)
# find contours
contours,_ = cv2.findContours(th,
cv2.RETR_TREE,
cv2.CHAIN_APPROX_NONE)
# draw contours on original image
face = contours[455:465]
cv2.drawContours(img, face, -1, (0,0,255),1)
cv2.imshow('Contour', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(len(contours))