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
349
350
351
352
353
354
355
356
357
358
359
|
/*
-----------------------------------------------------------------------
Copyright: 2010-2016, iMinds-Vision Lab, University of Antwerp
2014-2016, CWI, Amsterdam
Contact: astra@astra-toolbox.com
Website: http://www.astra-toolbox.com/
This file is part of the ASTRA Toolbox.
The ASTRA Toolbox is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The ASTRA Toolbox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>.
-----------------------------------------------------------------------
*/
#include "util.h"
#include "darthelper.h"
#include <cassert>
namespace astraCUDA {
// CUDA function for the selection of ROI
__global__ void devRoiSelect(float* in, float radius, unsigned int pitch, unsigned int width, unsigned int height)
{
float x = (float)(threadIdx.x + 16*blockIdx.x);
float y = (float)(threadIdx.y + 16*blockIdx.y);
float w = (width-1.0f)*0.5f;
float h = (height-1.0f)*0.5f;
if ((x-w)*(x-w) + (y-h)*(y-h) > radius * radius * 0.25f)
{
float* d = (float*)in;
unsigned int o = y*pitch+x;
d[o] = 0.0f;
}
}
void roiSelect(float* out, float radius, unsigned int width, unsigned int height)
{
float* D_data;
unsigned int pitch;
// We abuse dims here...
SDimensions dims;
dims.iVolWidth = width;
dims.iVolHeight = height;
allocateVolumeData(D_data, pitch, dims);
copyVolumeToDevice(out, width, dims, D_data, pitch);
dim3 blockSize(16,16);
dim3 gridSize((width+15)/16, (height+15)/16);
devRoiSelect<<<gridSize, blockSize>>>(D_data, radius, pitch, width, height);
copyVolumeFromDevice(out, width, dims, D_data, pitch);
cudaFree(D_data);
}
// CUDA function for the masking of DART with a radius == 1
__global__ void devDartMask(float* mask, const float* in, unsigned int conn, unsigned int pitch, unsigned int width, unsigned int height)
{
unsigned int x = threadIdx.x + 16*blockIdx.x;
unsigned int y = threadIdx.y + 16*blockIdx.y;
// Sacrifice the border pixels to simplify the implementation.
if (x > 0 && x < width - 1 && y > 0 && y < height - 1) {
float* d = (float*)in;
float* m = (float*)mask;
unsigned int o2 = y*pitch+x; // On this row.
unsigned int o1 = o2 - pitch; // On previous row.
unsigned int o3 = o2 + pitch; // On next row.
if ((conn == 8 && // 8-connected
(d[o1 - 1] != d[o2] || d[o1] != d[o2] || d[o1 + 1] != d[o2] ||
d[o2 - 1] != d[o2] || d[o2 + 1] != d[o2] ||
d[o3 - 1] != d[o2] || d[o3] != d[o2] || d[o3 + 1] != d[o2] ))
||
(conn == 4 && // 4-connected
( d[o1] != d[o2] ||
d[o2 - 1] != d[o2] || d[o3 + 1] != d[o2] ||
d[o3] != d[o2] )))
{
m[o2] = 1.0f;
}
}
}
// CUDA function for the masking of DART with a radius > 1
__global__ void devDartMaskRadius(float* mask, const float* in, unsigned int conn, unsigned int radius, unsigned int pitch, unsigned int width, unsigned int height)
{
unsigned int x = threadIdx.x + 16*blockIdx.x;
unsigned int y = threadIdx.y + 16*blockIdx.y;
// Sacrifice the border pixels to simplify the implementation.
if (x > radius-1 && x < width - radius && y > radius-1 && y < height - radius)
{
float* d = (float*)in;
float* m = (float*)mask;
int r = radius;
// o2: index of the current center pixel
int o2 = y*pitch+x;
if (conn == 8) // 8-connected
{
for (int row = -r; row <= r; row++)
{
int o1 = (y+row)*pitch+x;
for (int col = -r; col <= r; col++)
{
if (d[o1 + col] != d[o2]) {m[o2] = 1.0f; return;}
}
}
}
else if (conn == 4) // 4-connected
{
// horizontal
unsigned int o1 = y*pitch+x;
for (int col = -r; col <= r; col++)
{
if (d[o1 + col] != d[o2]) {m[o2] = 1.0f; return;}
}
// vertical
for (int row = -r; row <= r; row++)
{
unsigned int o1 = (y+row)*pitch+x;
if (d[o1] != d[o2]) {m[o2] = 1.0f; return;}
}
}
}
}
// CUDA function for the masking of ADART with a radius == 1
__global__ void devADartMask(float* mask, const float* in, unsigned int conn, unsigned int threshold, unsigned int pitch, unsigned int width, unsigned int height)
{
unsigned int x = threadIdx.x + 16*blockIdx.x;
unsigned int y = threadIdx.y + 16*blockIdx.y;
// Sacrifice the border pixels to simplify the implementation.
if (x > 0 && x < width - 1 && y > 0 && y < height - 1) {
float* d = (float*)in;
float* m = (float*)mask;
unsigned int o2 = y*pitch+x; // On this row.
unsigned int o1 = o2 - pitch; // On previous row.
unsigned int o3 = o2 + pitch; // On next row.
if (conn == 8)
{
if (d[o1 - 1] != d[o2] && --threshold == 0) {m[o2] = 1.0f; return;}
if (d[o1 ] != d[o2] && --threshold == 0) {m[o2] = 1.0f; return;}
if (d[o1 + 1] != d[o2] && --threshold == 0) {m[o2] = 1.0f; return;}
if (d[o2 - 1] != d[o2] && --threshold == 0) {m[o2] = 1.0f; return;}
if (d[o2 + 1] != d[o2] && --threshold == 0) {m[o2] = 1.0f; return;}
if (d[o3 - 1] != d[o2] && --threshold == 0) {m[o2] = 1.0f; return;}
if (d[o3 ] != d[o2] && --threshold == 0) {m[o2] = 1.0f; return;}
if (d[o3 + 1] != d[o2] && --threshold == 0) {m[o2] = 1.0f; return;}
}
else if (conn == 4)
{
if (d[o1 ] != d[o2] && --threshold == 0) {m[o2] = 1.0f; return;}
if (d[o2 - 1] != d[o2] && --threshold == 0) {m[o2] = 1.0f; return;}
if (d[o2 + 1] != d[o2] && --threshold == 0) {m[o2] = 1.0f; return;}
if (d[o3 ] != d[o2] && --threshold == 0) {m[o2] = 1.0f; return;}
}
}
}
// CUDA function for the masking of ADART with a radius > 1
__global__ void devADartMaskRadius(float* mask, const float* in, unsigned int conn, unsigned int radius, unsigned int threshold, unsigned int pitch, unsigned int width, unsigned int height)
{
unsigned int x = threadIdx.x + 16*blockIdx.x;
unsigned int y = threadIdx.y + 16*blockIdx.y;
// Sacrifice the border pixels to simplify the implementation.
if (x > radius-1 && x < width - radius && y > radius-1 && y < height - radius)
{
float* d = (float*)in;
float* m = (float*)mask;
int r = radius;
unsigned int o2 = y*pitch+x; // On this row.
if (conn == 8)
{
for (int row = -r; row <= r; row++)
{
unsigned int o1 = (y+row)*pitch+x;
for (int col = -r; col <= r; col++)
{
if (d[o1+col] != d[o2] && --threshold == 0) {m[o2] = 1.0f; return;}
}
}
}
else if (conn == 4)
{
// horizontal
for (int col = -r; col <= r; col++)
{
if (d[o2+col] != d[o2] && --threshold == 0) {m[o2] = 1.0f; return;}
}
// vertical
for (int row = -r; row <= r; row++)
{
unsigned int o1 = (y+row)*pitch+x;
if (d[o1] != d[o2] && --threshold == 0) {m[o2] = 1.0f; return;}
}
}
}
}
void dartMask(float* mask, const float* segmentation, unsigned int conn, unsigned int radius, unsigned int threshold, unsigned int width, unsigned int height)
{
float* D_segmentationData;
float* D_maskData;
unsigned int pitch;
// We abuse dims here...
SDimensions dims;
dims.iVolWidth = width;
dims.iVolHeight = height;
allocateVolumeData(D_segmentationData, pitch, dims);
copyVolumeToDevice(segmentation, width, dims, D_segmentationData, pitch);
allocateVolumeData(D_maskData, pitch, dims);
zeroVolumeData(D_maskData, pitch, dims);
dim3 blockSize(16,16);
dim3 gridSize((width+15)/16, (height+15)/16);
if (threshold == 1 && radius == 1)
devDartMask<<<gridSize, blockSize>>>(D_maskData, D_segmentationData, conn, pitch, width, height);
else if (threshold > 1 && radius == 1)
devADartMask<<<gridSize, blockSize>>>(D_maskData, D_segmentationData, conn, threshold, pitch, width, height);
else if (threshold == 1 && radius > 1)
devDartMaskRadius<<<gridSize, blockSize>>>(D_maskData, D_segmentationData, conn, radius, pitch, width, height);
else
devADartMaskRadius<<<gridSize, blockSize>>>(D_maskData, D_segmentationData, conn, radius, threshold, pitch, width, height);
copyVolumeFromDevice(mask, width, dims, D_maskData, pitch);
cudaFree(D_segmentationData);
cudaFree(D_maskData);
}
__global__ void devDartSmoothingRadius(float* out, const float* in, float b, unsigned int radius, unsigned int pitch, unsigned int width, unsigned int height)
{
unsigned int x = threadIdx.x + 16*blockIdx.x;
unsigned int y = threadIdx.y + 16*blockIdx.y;
// Sacrifice the border pixels to simplify the implementation.
if (x > radius-1 && x < width - radius && y > radius-1 && y < height - radius)
{
float* d = (float*)in;
float* m = (float*)out;
unsigned int o2 = y*pitch+x;
int r = radius;
float count = 4*r*(r+1);
float res = -d[o2];
for (int row = -r; row <= r; row++)
{
unsigned int o1 = (y+row)*pitch+x;
for (int col = -r; col <= r; col++)
{
res += d[o1+col];
}
}
res *= b / count;
res += (1.0f-b) * d[o2];
m[o2] = res;
}
}
__global__ void devDartSmoothing(float* out, const float* in, float b, unsigned int pitch, unsigned int width, unsigned int height)
{
unsigned int x = threadIdx.x + 16*blockIdx.x;
unsigned int y = threadIdx.y + 16*blockIdx.y;
// Sacrifice the border pixels to simplify the implementation.
if (x > 0 && x < width - 1 && y > 0 && y < height - 1) {
float* d = (float*)in;
float* m = (float*)out;
unsigned int o2 = y*pitch+x; // On this row.
unsigned int o1 = o2 - pitch; // On previous row.
unsigned int o3 = o2 + pitch; // On next row.
m[o2] = (1.0f-b) * d[o2] + b * 0.125f * (d[o1 - 1] + d[o1] + d[o1 + 1] + d[o2 - 1] + d[o2 + 1] + d[o3 - 1] + d[o3] + d[o3 + 1]);
}
}
void dartSmoothing(float* out, const float* in, float b, unsigned int radius, unsigned int width, unsigned int height)
{
float* D_inData;
float* D_outData;
unsigned int pitch;
// We abuse dims here...
SDimensions dims;
dims.iVolWidth = width;
dims.iVolHeight = height;
allocateVolumeData(D_inData, pitch, dims);
copyVolumeToDevice(in, width, dims, D_inData, pitch);
allocateVolumeData(D_outData, pitch, dims);
zeroVolumeData(D_outData, pitch, dims);
dim3 blockSize(16,16);
dim3 gridSize((width+15)/16, (height+15)/16);
if (radius == 1)
devDartSmoothing<<<gridSize, blockSize>>>(D_outData, D_inData, b, pitch, width, height);
else
devDartSmoothingRadius<<<gridSize, blockSize>>>(D_outData, D_inData, b, radius, pitch, width, height);
copyVolumeFromDevice(out, width, dims, D_outData, pitch);
cudaFree(D_outData);
cudaFree(D_inData);
}
}
|