-
Notifications
You must be signed in to change notification settings - Fork 0
/
pycl_t.py
294 lines (198 loc) · 6.62 KB
/
pycl_t.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
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
import numpy as np
import pyopencl as cl
import pyopencl.array
import cv2
import sys
import time
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
prg = cl.Program(ctx, """
__kernel void ds(__global const uchar *img_g,
const int width,
const int height,
const int out_width,
const int out_height,
__global uchar *out_g) {
int gid = get_global_id(0);
int col = gid % width;
int row = gid / width;
if ((col >= width) || (row >= height)) {
return;
}
if (col < 0) {
return;
}
int new_row = row/2;
int new_col = col/2;
if ((new_col >= out_width) || (new_row >= out_height)) {
return;
}
if (new_col < 0) {
return;
}
int k = new_row*out_width + new_col;
if (row % 2 == 0 && col % 2 == 0) {
uchar c = img_g[gid];
uchar r = img_g[gid+1];
uchar b = img_g[gid+width];
uchar b_r = img_g[gid+width+1];
uchar val = (c + r + b + b_r) / 4;
//out_g[k] = img_g[gid];
out_g[k] = val;
}
}
__kernel void transform(__global const uchar *img_g,
const int width,
const int height,
const float angle,
const float Tx,
const float Ty,
const int out_width,
const int out_height,
__global uchar *out_g) {
int gid = get_global_id(0);
int col = gid % width;
int row = gid / width;
if ((col >= width) || (row >= height)) {
return;
}
if (col < 0) {
return;
}
//
float c = cos(angle);
float s = sin(angle);
// new position
int new_col = c * col - s * row + Tx;
int new_row = s * col + c * row + Ty;
if ((new_col >= out_width) || (new_row >= out_height)) {
return;
}
if (new_col < 0) {
return;
}
int k = new_row*out_width + new_col;
out_g[k] = img_g[gid];
}
""").build()
img = cv2.imread(sys.argv[1], cv2.CV_LOAD_IMAGE_GRAYSCALE)
width, height = (img.shape[0], img.shape[1])
# R, Tx, Ty = [0.24, 500, 500]
R, Tx, Ty = [-8.07505519252e-05, 43.31216948, -85.5063475025]
# print "R {0}, Tx, Ty: {1}, {2}".format(R, Tx, Ty)
c = np.cos(R)
s = np.sin(R)
# Find the output's height and width
height, width = img.shape
points = [[0, 0], [0, height - 1], [width - 1, 0], [width - 1, height - 1]]
min_x, min_y, max_x, max_y = [ sys.maxint, sys.maxint, -sys.maxint, -sys.maxint ]
for point in points:
# shift the point
# point[0] += start_point[0]
# point[1] += start_point[1]
new_x = c * point[0] - s * point[1] + Tx
new_y = s * point[0] + c * point[1] + Ty
min_x = min(min_x, new_x)
min_y = min(min_y, new_y)
max_x = max(max_x, new_x)
max_y = max(max_y, new_y)
# print min_x, min_y, max_x, max_y
# M = np.float32([ [c, -s, Tx - min_x],[ s, c, Ty - min_y] ])
Tx2 = Tx #- min_x
Ty2 = Ty #- min_y
# print Tx2, Ty2
new_width = int(max_x - min_x)
new_height = int(max_y - min_y)
# if (new_width % 2 != 0):
# new_width += 1
# if (new_height % 2 != 0):
# new_height +=1
new_size = (new_width, new_height)
print new_size
# out_arr = cv2.warpAffine(img, M, new_size)
# print new_size
output = np.zeros(new_size, dtype=img.dtype)
img_s = img.ravel()
out_s = output.ravel()
# How many levels do I have?
# nLevels = 0;
# width = new_size[0] / 2
# out_pyr_g = []
# mf = cl.mem_flags
# while (width > 512):
# if( nLevels == 0 ):
# # TODO If run on CPU, does COPY_HOST_PTR make a copy of the data in main memory?
# out_pyr_g.append(cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=img_s))
# else:
# out_pyr_g.append(cl.Buffer(ctx, mf.READ_WRITE | mf.ALLOC_HOST_PTR, width*width))
# width /= 2
# nLevels+=1;
# print out_pyr_g
# sys.exit()
# Upload first to gpu
# img_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=img_s)
# out_g = cl.Buffer(ctx, mf.READ_WRITE, img_s.nbytes)
def transform(ctx, queue, img_s, width, height, angle, Tx, Ty, out_width, out_height, out_s):
mf = cl.mem_flags
img_g = cl.Buffer(ctx, mf.READ_ONLY | mf.USE_HOST_PTR, hostbuf=img_s)
out_g = cl.Buffer(ctx, mf.READ_WRITE, out_s.nbytes)
# prg.ds(queue, img_s.shape, None, img_g, np.int32(width), out_g)
# print img_s.shape, width*height
prg.transform(queue, (width*height,), None, img_g, np.int32(width), np.int32(height), np.float32(angle), np.float32(Tx), np. float32(Ty), np.int32(out_width), np.int32(out_height), out_g)
cl.enqueue_copy(queue, out_s, out_g).wait()
# print img_s[0:5], out_s[0:5]
# print img_s.shape, out_s.shape
return out_s
# Memory allocation for all pyramid levels
transformed_bytes = transform(ctx, queue, img_s, width, height, R, Tx2, Ty2, new_size[0], new_size[1], out_s)
print 'transformed!', R, Tx, Ty
# img_r = transformed_bytes.reshape(new_size[0], new_size[1])
# cv2.imwrite('/tmp/test.jpg', img_r)
# sys.exit(1)
def ds(ctx, queue, img_s, width, height, new_width, new_height, out_s):
mf = cl.mem_flags
img_g = cl.Buffer(ctx, mf.READ_ONLY | mf.USE_HOST_PTR, hostbuf=img_s)
out_g = cl.Buffer(ctx, mf.WRITE_ONLY, (new_width*new_height))
print 'call', width, new_width, new_height
prg.ds(queue, (width*height,), None, img_g, np.int32(width), np.int32(height), np.int32(new_width), np.int32(new_height), out_g)
# prg.transform(queue, img_s.shape, None, img_g, np.int32(width), np.float32(angle), np.float32(Tx), np. float32(Ty), np.int32(out_width), out_g)
print 'done'
cl.enqueue_copy(queue, out_s, out_g).wait()
print 'got it'
# print img_s[0:5], out_s[0:5]
# print img_s.shape, out_s.shape
return out_s
width = new_size[0]
height = new_size[1]
# if (new_size[0] % 2 != 0):
# # width is odd
# width += 1
# if (new_size[1] % 2 != 0):
# # height is odd
# height += 1
# print transformed_bytes.shape
# if (width*height != new_size[0]*new_size[1]):
# print 'extending input array'
# transformed_bytes = np.hstack((transformed_bytes, [0]*(width*height - new_size[0]*new_size[1])))
# print transformed_bytes.shape
k = 0
new_width = int(width / 2)
new_height = int(height / 2)
# if (new_width % 2 !=0):
# new_width += 1
# if (new_height % 2 != 0):
# new_height += 1
# while (width > 512):
sys.exit()
print 'downsampling', width, height, width*height, 'n', new_width, new_height, new_width*new_height
k+=1
downsampled = np.zeros((new_width*new_height), dtype=transformed_bytes.dtype)
downsampled = ds(ctx, queue, transformed_bytes, width, height, new_width, new_height, downsampled)
# if k==5:
print 'storing'
outimg = downsampled.reshape(new_width, new_height)
cv2.imwrite('/tmp/trans.jpg', outimg)
width = new_width
new_width /= 2
new_height /= 2
transformed_bytes = downsampled