-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutilities.py
134 lines (108 loc) · 4.61 KB
/
utilities.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
import numpy as np
import cv2
def opticalFlowDense(image_current, image_next):
"""
input: image_current, image_next (RGB images)
calculates optical flow magnitude and angle and places it into HSV image
* Set the saturation to the saturation value of image_next
* Set the hue to the angles returned from computing the flow params
* set the value to the magnitude returned from computing the flow params
* Convert from HSV to RGB and return RGB image with same size as original image
"""
gray_current = cv2.cvtColor(image_current, cv2.COLOR_RGB2GRAY)
gray_next = cv2.cvtColor(image_next, cv2.COLOR_RGB2GRAY)
hsv = np.zeros((image_current.shape))
# set saturation
hsv[:, :, 1] = cv2.cvtColor(image_next, cv2.COLOR_RGB2HSV)[:, :, 1]
# Flow Parameters
# flow_mat = cv2.CV_32FC2
flow_mat = None
image_scale = 0.5
nb_images = 1
win_size = 15
nb_iterations = 2
deg_expansion = 5
STD = 1.3
extra = 0
# obtain dense optical flow paramters
flow = cv2.calcOpticalFlowFarneback(gray_current, gray_next,
flow_mat,
image_scale,
nb_images,
win_size,
nb_iterations,
deg_expansion,
STD,
0)
# convert from cartesian to polar
mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
# hue corresponds to direction
hsv[:, :, 0] = ang * (180 / np.pi / 2)
# value corresponds to magnitude
hsv[:, :, 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
# convert HSV to int32's
hsv = np.asarray(hsv, dtype=np.float32)
rgb_flow = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)
return rgb_flow
# Given the lines coming from the Hough transform result, this function draws them over a given image
def drawHoughTransformLines(img, lines):
if lines is None:
return img
a, b, c = lines.shape
for i in range(a):
cv2.line(img, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (255, 255, 255), 3,
cv2.LINE_AA)
return img
# This function applies a given brightness and contrast to an input image
def apply_brightness_contrast(input_img, brightness = 0, contrast = 0):
if brightness != 0:
if brightness > 0:
shadow = brightness
highlight = 255
else:
shadow = 0
highlight = 255 + brightness
alpha_b = (highlight - shadow)/255
gamma_b = shadow
buf = cv2.addWeighted(input_img, alpha_b, input_img, 0, gamma_b)
else:
buf = input_img.copy()
if contrast != 0:
f = 131*(contrast + 127)/(127*(131-contrast))
alpha_c = f
gamma_c = 127*(1-f)
buf = cv2.addWeighted(buf, alpha_c, buf, 0, gamma_c)
return buf
# This function
def thresholdWhiteAndYellow(image):
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
img_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Get yellow from HSV version of our image
lower_yellow = np.array([20, 100, 100], dtype="uint8")
upper_yellow = np.array([30, 255, 255], dtype="uint8")
mask_yellow = cv2.inRange(img_hsv, lower_yellow, upper_yellow)
# Get white from greyscale version of our image
mask_white = cv2.inRange(gray_image, 200, 255)
# Combine and apply filters
mask_yw = cv2.bitwise_or(mask_white, mask_yellow)
mask_yw_image = cv2.bitwise_and(gray_image, mask_yw)
return mask_yw_image
# This method draws the optical flow onto img with a given step (distance between one arrow origin and the other)
def draw_flow(img, flow, step=16):
h, w = img.shape[:2]
y, x = np.mgrid[step/2:h:step, step/2:w:step].reshape(2,-1).astype(int)
fx, fy = flow[y,x].T
lines = np.vstack([x, y, x+fx, y+fy]).T.reshape(-1, 2, 2)
lines = np.int32(lines + 0.5)
vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
cv2.polylines(vis, lines, 0, (0, 255, 0))
for (x1, y1), (x2, y2) in lines:
cv2.circle(vis, (x1, y1), 1, (0, 255, 0), -1)
return vis
# This method cuts top and bottom portions of the frame (which are only black areas of the car's dashboard or sky)
def cutTopAndBottom(img, top, bottom):
height, width = img.shape
heightBeginning = 20
heightEnd = height - 30
crop_img = img[top : bottom, 0 : width]
return crop_img