-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathimageProcessing.py
More file actions
348 lines (273 loc) · 10.4 KB
/
imageProcessing.py
File metadata and controls
348 lines (273 loc) · 10.4 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import pickle
import cv2
import os
import numpy as np
import math
import helper as aux
class Processing:
def __init__(self):
calibrationFile = 'calibration_data.p'
if not os.path.isfile(calibrationFile):
self.camMtx = None
self.distortionCoeffs = None
else:
with open(calibrationFile, mode='rb') as f:
data = pickle.load(f)
self.camMtx = data['cameraMatrix']
self.distortionCoeffs = data['distCoeffs']
def undistort(self, src):
"""
Distortion correction using available camera matrix and distortion coefficients
:param src: source image captured with the same camera as available matrix and distortion
coefficients for
:return: image with corrected distortion
"""
if self.camMtx is not None and self.distortionCoeffs is not None:
return cv2.undistort(src, self.camMtx, self.distortionCoeffs, None, self.camMtx)
else:
return None
@staticmethod
def histEq(src):
"""
Histogram equalization
:param src: gray or rgb image. The latter to be converted to HLS and histogram
equalization applied to Luminance space, then converted back to RGB
:return: histogram-equalized image
"""
if len(src.shape) > 2:
src = Thresholding.hls(src)
src[:, :, 1] = cv2.equalizeHist(src[:, :, 1])
return cv2.cvtColor(src, cv2.COLOR_HLS2RGB)
else:
return cv2.equalizeHist(src)
@staticmethod
def resize(src, ratio, interpolation=cv2.INTER_AREA):
"""
Convenience wrapper for OpenCV resize function
:param src:
:param ratio:
:param interpolation:
:return:
"""
return cv2.resize(src=src, dsize=(0, 0), fx=ratio, fy=ratio, interpolation=interpolation)
class Thresholding:
def __init__(self):
pass
@staticmethod
def gradAbsolute(img, orient='x', sobelKernel=3, threshold=(0, 255)):
"""
absolute gradient with sobel operator
:param img: source image
:param orient: direction of the gradient
:param sobelKernel:
:param threshold:
:return: binary imaged obtained by applying Sobel with given params
"""
if len(img.shape) > 2:
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
else:
gray = img
dx = orient == 'x'
dy = not dx
sobel = cv2.Sobel(gray, cv2.CV_64F, dx, dy, ksize=sobelKernel)
absSobel = np.absolute(sobel)
scaledSobel = np.uint8(255 * absSobel / np.max(absSobel))
sBinary = np.zeros_like(gray)
sBinary[(scaledSobel > threshold[0]) & (scaledSobel < threshold[1])] = 1
return sBinary
@staticmethod
def gradMagnitude(img, sobelKernel=3, threshold=(0, 255)):
"""
overall magnitude of the gradient, in both x and y.
:param img:
:param sobelKernel:
:param threshold:
:return:
"""
if len(img.shape) > 2:
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
else:
gray = img
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobelKernel)
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobelKernel)
sobelAbs = np.sqrt(np.square(sobelx) + np.square(sobely))
scaledSobel = np.uint8(255 * sobelAbs / np.max(sobelAbs))
sBinary = np.zeros_like(gray)
sBinary[(scaledSobel > threshold[0]) & (scaledSobel < threshold[1])] = 1
return sBinary
@staticmethod
def gradDirectional(img, sobel_kernel=3, threshold=(0, np.pi / 2)):
"""
Directional gradient using Sobel
:param img:
:param sobel_kernel:
:param threshold:
:return:
"""
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
sobelX = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
sobelY = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
absSobelX = np.absolute(sobelX)
absSobelY = np.absolute(sobelY)
gradDir = np.arctan2(absSobelY, absSobelX)
sBinary = np.zeros_like(gradDir)
sBinary[(gradDir > threshold[0]) & (gradDir < threshold[1])] = 1
return sBinary
@staticmethod
def binaryImage(gray, threshold=(0, 255)):
"""
Converts single channel to binary according to given thresholding
:param gray: single-channel image
:param threshold: thresholds to be applied for binary clamping
:return: binary image
"""
binary = np.zeros_like(gray)
binary[(gray > threshold[0]) & (gray <= threshold[1])] = 1
return binary
@staticmethod
def hls(src):
"""
Converts RGB to HLS
:param src: source image
:return: image in HLS color space
"""
return cv2.cvtColor(src, cv2.COLOR_RGB2HLS)
@staticmethod
def yuv(src):
"""
Converts RGB to YUV
:param src:
:return:
"""
return cv2.cvtColor(src, cv2.COLOR_RGB2YUV)
@staticmethod
def combiThreshold(src, redThrsd=(200, 255), satThrsd=(90, 255),
absThrsd=(20, 255), orient='x'):
"""
Returns combined binary image according to rule:
(redBinary & saturationBinary | abs binary)
:param src: source image
:param redThrsd: red channel thresholds
:param satThrsd: HL(S)aturation channel thresholds
:param absThrsd: absolute sobel thresholds
:param orient: sobel orientation
:return: combined binary thresholded image
"""
hlsImg = Thresholding.hls(src=src)
rChannel = src[:, :, 0]
sChannel = hlsImg[:, :, 2]
rBinary = Thresholding.binaryImage(gray=rChannel, threshold=redThrsd)
sBinary = Thresholding.binaryImage(gray=sChannel, threshold=satThrsd)
absBinary = Thresholding.gradAbsolute(img=src, orient=orient, threshold=absThrsd)
combined = np.zeros_like(rBinary)
combined[((rBinary == 1) & (sBinary == 1) | absBinary == 1)] = 1
return combined
class Warping:
def __init__(self):
"""
0--3
/ \
1______2
"""
self.src = np.array([[596, 450],
[204, 719],
[1108, 719],
[684, 450]], dtype=np.float32)
self.dst = np.array([[204, 0],
[204, 719],
[1108, 719],
[1108, 0]], dtype=np.float32)
self.M = cv2.getPerspectiveTransform(self.src, self.dst)
self.mInv = cv2.getPerspectiveTransform(self.dst, self.src)
@staticmethod
def warp(srcImg, matrix):
"""
Wrapper for warper:)
:param srcImg:
:param matrix:
:return:
"""
return cv2.warpPerspective(srcImg, matrix, (srcImg.shape[1], srcImg.shape[0]),
flags=cv2.INTER_LINEAR)
def birdEye(self, img, leftShift=0):
"""
perspective to bird-eye projection
:param img:
:param leftShift:
:return:
"""
if leftShift == 0:
return self.warp(srcImg=img, matrix=self.M)
else:
src = self.src.copy()
src[:, 0] = src[:, 0] + leftShift
dst = self.dst.copy()
dst[:, 0] = dst[:, 0] + leftShift
mInv = cv2.getPerspectiveTransform(src, dst)
return self.warp(srcImg=img, matrix=mInv)
def perspective(self, img, leftShift=0):
"""
the inverse of bird-eye.
Could be re-factored to reuse birdEye(), but that won't add clarity in this case
:param img:
:param leftShift:
:return:
"""
if leftShift == 0:
return self.warp(srcImg=img, matrix=self.mInv)
else:
src = self.src.copy()
src[:, 0] = src[:, 0] + leftShift
dst = self.dst.copy()
dst[:, 0] = dst[:, 0] + leftShift
mInv = cv2.getPerspectiveTransform(dst, src)
return self.warp(srcImg=img, matrix=mInv)
class Drawing:
@staticmethod
def addPolygon(srcShape, lFit, rFit, stepCount=10, color=(0, 255, 0)):
"""
Adds polygon to empty image with a shape of a source
:param srcShape: shape of a source image
:param lFit: left 2-nd order polynomial line params
:param rFit: right 2-nd order polynomial line params
:return: image with polygon between lines
:param stepCount: number of steps
:param color:
"""
imgH = srcShape[0]
imgW = srcShape[1]
cL = lFit[2]
cR = rFit[2]
mask = np.zeros(shape=srcShape, dtype=np.uint8)
rOutstand = max(0, (int(math.ceil(cR)) - (imgW - 1)))
if rOutstand > 0:
r_filler = np.zeros((imgH, rOutstand), dtype=np.uint8)
mask = np.hstack((mask, r_filler))
lOutstand = abs(min(0, int(math.floor(cL))))
if lOutstand > 0:
lFiller = np.zeros((imgH, lOutstand), dtype=np.uint8)
mask = np.hstack((lFiller, mask))
outImg = np.dstack((mask, mask, mask))
y = np.linspace(0, imgH - 1, stepCount)
xl = aux.funcSpace(argSpace=y, fitParams=lFit)
xr = aux.funcSpace(argSpace=y, fitParams=rFit)
leftLinePoints = np.array([np.transpose(np.vstack([xl + lOutstand, y]))])
rightLinePoints = np.array([np.flipud(np.transpose(np.vstack([xr + lOutstand, y])))])
points = np.hstack((leftLinePoints, rightLinePoints))
cv2.fillPoly(img=outImg, pts=np.int_(points), color=color)
return outImg, lOutstand, rOutstand
@staticmethod
def addLine(src, fit, color, stepCount=10, thickness=2):
if fit is not None:
imgH = src.shape[0]
y = np.linspace(0, imgH - 1, stepCount)
x = aux.funcSpace(argSpace=y, fitParams=fit)
for i in range(stepCount - 1):
x1 = int(round(x[i], 0))
y1 = int(round(y[i], 0))
x2 = int(round(x[i + 1], 0))
y2 = int(round(y[i + 1], 0))
pt1 = (x1, y1)
pt2 = (x2, y2)
src = cv2.line(img=src, pt1=pt1, pt2=pt2, color=color, thickness=thickness)
return src