-
Notifications
You must be signed in to change notification settings - Fork 0
/
gputrial.py
252 lines (203 loc) · 8 KB
/
gputrial.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
from numba import jit, prange, cuda
import matplotlib.pyplot as plt
import numpy as np
import math
import cv2
import scipy.ndimage.filters as filters
import scipy.ndimage as ndimage
import time
# ---------------------------------------------------------------------------------------- #
# Step 1: read image
r_dim = 200
theta_dim = 300
theta_max = 1.0 * math.pi
outer_hough_space = np.zeros((r_dim, theta_dim))
def read_img(img_path):
img = cv2.imread(img_path) # pentagon.png
# print('image shape: ', img.shape)
# plt.imshow(img, )
# plt.savefig("image1.png", bbox_inches='tight')
# plt.close()
return img
# ----------------------------------------------------------------------------------------#
# Step 2: Hough Space
# @jit(nopython=True, parallel=True, fastmath=True)
@cuda.jit
def create_hough_space_with_acc(img, hough_space, r_max):
# hough_space = np.zeros((r_dim, theta_dim))
i = cuda.grid(1)
if i < hough_space.shape[0]:
for x in range(x_max):
for y in range(y_max):
if img[x, y] == 255:
continue
for itheta in range(theta_dim):
theta = 1.0 * itheta * theta_max / theta_dim
r = x * math.cos(theta) + y * math.sin(theta)
ir = r_dim * (1.0 * r) / r_max
hough_space[int(ir), int(itheta)] = hough_space[int(ir), int(itheta)] + 1
#outer_hough_space.copy_to_host(hough_space)
def create_hough_space_without_acc(img):
x_max, y_max = img.shapea
theta_max = 1.0 * math.pi
r_max = math.hypot(x_max, y_max)
hough_space = np.zeros((r_dim, theta_dim))
for x in range(x_max):
for y in range(y_max):
if img[x, y] == 255:
continue
for itheta in range(theta_dim):
theta = 1.0 * itheta * theta_max / theta_dim
r = x * math.cos(theta) + y * math.sin(theta)
ir = r_dim * (1.0 * r) / r_max
hough_space[int(ir), int(itheta)] = hough_space[int(ir), int(itheta)] + 1
return hough_space, r_dim, r_max, theta_dim, theta_max, y_max
# Step 3: Find maximas 2
def find_maxima(hough_space):
neighborhood_size = 20
threshold = 140
data_max = filters.maximum_filter(hough_space, neighborhood_size)
maxima = (hough_space == data_max)
data_min = filters.minimum_filter(hough_space, neighborhood_size)
diff = ((data_max - data_min) > threshold)
maxima[diff == 0] = 0
labeled, num_objects = ndimage.label(maxima)
slices = ndimage.find_objects(labeled)
x, y = [], []
for dy, dx in slices:
x_center = (dx.start + dx.stop - 1) / 2
x.append(x_center)
y_center = (dy.start + dy.stop - 1) / 2
y.append(y_center)
return x, y
# plt.imshow(hough_space, origin='lower')
# plt.savefig('hough_space_i_j.png', bbox_inches='tight')
#
# plt.autoscale(False)
# plt.plot(x, y, 'ro')
# plt.savefig('hough_space_maximas.png', bbox_inches='tight')
#
# plt.close()
# ----------------------------------------------------------------------------------------#
# Step 4: Plot lines
def hough_transform_opencv(gray, output_path, img):
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=5)
lines = cv2.HoughLines(edges, 1, np.pi / 180, 100)
print(lines.shape)
for i in lines:
for rho, theta in i:
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * (a))
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * (a))
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv2.imwrite(output_path, img)
def plot_line(x, y, r_max, y_max, img, output_path):
line_index = 1
fig, ax = plt.subplots()
ax.imshow(img)
for i, j in zip(y, x):
r = round((1.0 * i * r_max) / r_dim, 1)
theta = round((1.0 * j * theta_max) / theta_dim, 1)
ax.autoscale(False)
px = []
py = []
for i in range(-y_max - 40, y_max + 40, 1):
px.append(math.cos(-theta) * i - math.sin(-theta) * r)
py.append(math.sin(-theta) * i + math.cos(-theta) * r)
ax.plot(px, py, linewidth=4)
line_index = line_index + 1
plt.axis('off')
plt.savefig(output_path, bbox_inches='tight')
plt.close()
if __name__ == '__main__':
img = read_img('image.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
hough_space = np.zeros((r_dim, theta_dim))
x_max, y_max = gray.shape
r_max = math.hypot(x_max, y_max)
# dev_hough_space = cuda.to_device(hough_space)
# dev_gray = cuda.to_device(gray)
# dev_r_max = cuda.to_device(r_max)
print("\nWITH JIT COMPILER ACCELERATION:\n")
print('\nFirst Iteration:')
start = time.time()
# threadsperblock = 32
# #print(img.size)
# blockspergrid = (img.shape[0] + (threadsperblock - 1)) // threadsperblock
threadsperblock = (16, 16)
blockspergrid_x = math.ceil(hough_space.shape[0] / threadsperblock[0])
blockspergrid_y = math.ceil(hough_space.shape[1] / threadsperblock[1])
blockspergrid = (blockspergrid_x, blockspergrid_y)
create_hough_space_with_acc[blockspergrid, threadsperblock](hough_space)
#result = dev_hough_space.copy_to_host()
# hough_space, r_max, y_max = create_hough_space_with_acc(gray)
# hough_end = time.time()
# x, y = find_maxima(hough_space)
maxima_end = time.time()
# plot_line(x, y, r_max, y_max, img, "images/acc_it1.png")
#
# # print('time taken for hough space: ', (hough_end - start))
# # print('time taken for finding maxima: ', (maxima_end - hough_end))
print('overall time: ', (maxima_end - start))
#
# print('\nSecond Iteration:')
# start = time.time()
# hough_space, r_max, y_max = create_hough_space_with_acc(gray)
# # hough_end = time.time()
# x, y = find_maxima(hough_space)
# maxima_end = time.time()
# plot_line(x, y, r_max, r_dim, theta_dim, theta_max, y_max, img, "images/acc_it2.png")
#
# # print('time taken for hough space: ', (hough_end - start))
# # print('time taken for finding maxima: ', (maxima_end - hough_end))
# print('overall time: ', (maxima_end - start))
#
# acc_time = maxima_end - start
#
# print("\n\nWITHOUT JIT COMPILER ACCELERATION:\n")
# print('\nFirst Iteration:')
# start = time.time()
# hough_space, r_dim, r_max, theta_dim, theta_max, y_max = create_hough_space_without_acc(gray)
# # hough_end = time.time()
# x, y = find_maxima(hough_space)
# maxima_end = time.time()
# plot_line(x, y, r_max, r_dim, theta_dim, theta_max, y_max, img, "images/normal_it1.png")
#
# # print('time taken for hough space: ', (hough_end - start))
# # print('time taken for finding maxima: ', (maxima_end - hough_end))
# print('overall time: ', (maxima_end - start))
#
# print('\nSecond Iteration:')
# start = time.time()
# hough_space, r_dim, r_max, theta_dim, theta_max, y_max = create_hough_space_without_acc(gray)
# # hough_end = time.time()
# x, y = find_maxima(hough_space)
# maxima_end = time.time()
# plot_line(x, y, r_max, r_dim, theta_dim, theta_max, y_max, img, "images/normal_it2.png")
#
# # print('time taken for hough space: ', (hough_end - start))
# # print('time taken for finding maxima: ', (maxima_end - hough_end))
# print('overall time: ', (maxima_end - start))
# without_acc_time = maxima_end - start
#
# print('\n\nSPEEDUP = ', (without_acc_time / acc_time))
#
# print("\n\nOpenCV Output:\n")
#
# print("\nFirst Iteration")
# start = time.time()
# hough_transform_opencv(gray, "images/ocv_it1.png", img)
# end = time.time()
# print("Time taken: ", (end - start))
#
# print("\nSecond Iteration")
# start = time.time()
# hough_transform_opencv(gray, "images/ocv_it2.png", img)
# end = time.time()
# print("Time taken: ", (end - start))