/
funcImageDomain.cu
284 lines (220 loc) · 8.82 KB
/
funcImageDomain.cu
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
/*******************************************************************
c* Multimodal Deformable Image Registration *
c* via Mutual Information or Bhattacharyya Distantce *
c* Version: 1.0 *
c* Language: C, CUDA *
c* *
c* Developer: Yifei Lou *
c* Email: yifei.lou@ece.gatech.edu *
c* *
c* School of Electrical and Computer Engineering *
c* Georgia Institute of Technology *
c* Atlanta, GA, 30318 *
c* Website: http://groups.bme.gatech.edu/groups/bil/ *
c* *
c* Copyright (c) 2011 *
c* All rights reserved. *
c* *
c* Permission to use, copy, or modify this code and its *
c* documentation for scientific purpose is hereby granted *
c* without fee, provided that this copyright notice appear in *
c* all copies and that both that copyright notice and this *
c* permission notice appear in supporting documentation. The use *
c* for commercial purposes is prohibited without permission. *
c* *
c* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND *
c* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, *
c* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF *
c* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
c* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR *
c* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
c* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES INCLUDING, BUT NOT *
c* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF*
c* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
c* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
c* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING *
c* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF *
c* THE POSSIBILITY OF SUCH DAMAGE. *
c* *
c******************************************************************/
/*******************************************************************
c* Short discription *
c* Supporting functions in the image domain *
c* such as gradient, force computation, flow propagation, etc*
c******************************************************************/
#ifndef _FUN_IMAGE_DOMAIN_CU_
#define _FUN_IMAGE_DOMAIN_CU_
__global__ void forceComp(float *d_im_out, float *d_im_static, float *d_Likelihood, float *d_v_x, float *d_v_y, float *d_v_z, int NX, int NY, int NZ)
{
const int tid = (blockIdx.y*NBLOCKX + blockIdx.x)*blockDim.x + threadIdx.x;
if (tid<NX*NY*NZ)
{
float x = d_im_out[tid]*(nBin-1.0);
float y = d_im_static[tid]*(nBin-1.0);
int xmin = x;
int xmax = (xmin == x)? xmin : xmin+1;
int ymin = y;
int ymax = (ymin == y)? ymin : ymin+1;
float dLx = d_Likelihood[ymin*nBin + xmin] * (1.0f-(x-xmin)) * (1.0f-(y-ymin))
+ d_Likelihood[ymax*nBin + xmin] * (1.0f-(x-xmin)) * (y-ymin)
+ d_Likelihood[ymin*nBin + xmax] * (x-xmin) * (1.0f-(y-ymin))
+ d_Likelihood[ymax*nBin + xmax] * (x-xmin) * (y-ymin);
// compute image gradient, save as x, y and z
float z;
int zmin = tid/(NX*NY);
ymin = (tid%(NX*NY))/NX;
xmin = tid%NX;
if(xmin+1 < NX && xmin-1>=0)
x = ImageGradient(d_im_out[zmin*NX*NY + ymin*NX + (xmin-1) ], d_im_out[zmin*NX*NY +ymin*NX + xmin], d_im_out[zmin*NX*NY +ymin*NX + (xmin+1)]);
else x = 0;
if(ymin+1 < NY && ymin-1>=0)
y = ImageGradient(d_im_out[zmin*NX*NY +(ymin-1)*NX + xmin], d_im_out[zmin*NX*NY +ymin*NX+ xmin], d_im_out[zmin*NX*NY +(ymin+1)*NX+xmin]);
else y = 0;
if(zmin+1 <NZ && zmin-1>=0)
z = ImageGradient(d_im_out[(zmin-1)*NX*NY + ymin*NX + xmin], d_im_out[zmin*NX*NY + ymin*NX + xmin],d_im_out[(zmin+1)*NX*NY + ymin*NX + xmin]);
else z = 0;
d_v_x[tid] = -ALPHA*dLx*x;
d_v_y[tid] = -ALPHA*dLx*y;
d_v_z[tid] = -ALPHA*dLx*z;
}
}
__global__ void flowComp(float *d_mv_x, float *d_mv_y, float *d_mv_z, float *d_v_x, float *d_v_y, float *d_v_z, float *jacobian, float *flow, int NX, int NY, int NZ)
{
const int tid = (blockIdx.y*NBLOCKX + blockIdx.x)*blockDim.x + threadIdx.x;
if (tid<NX*NY*NZ)
{
float u1x, u1y, u1z;
float u2x, u2y, u2z;
float u3x, u3y, u3z;
int z = tid/(NX*NY);
int y = (tid%(NX*NY))/NX;
int x = tid%NX;
if(x+1<NX && x-1>=0)
{
u1x = ImageGradient(d_mv_x[z*NX*NY+y*NX+x-1], d_mv_x[z*NX*NY+y*NX+x], d_mv_x[z*NX*NY+y*NX+x+1]);
u2x = ImageGradient(d_mv_y[z*NX*NY+y*NX+x-1], d_mv_y[z*NX*NY+y*NX+x], d_mv_y[z*NX*NY+y*NX+x+1]);
u3x = ImageGradient(d_mv_z[z*NX*NY+y*NX+x-1], d_mv_z[z*NX*NY+y*NX+x], d_mv_y[z*NX*NY+y*NX+x+1]);
}
else
{
u1x = 0;
u2x = 0;
u3x = 0;
}
if(y+1<NY && y-1>=0)
{
u1y = ImageGradient(d_mv_x[z*NX*NY+(y-1)*NX+x], d_mv_x[z*NX*NY+y*NX+x], d_mv_x[z*NX*NY+(y+1)*NX+x]);
u2y = ImageGradient(d_mv_y[z*NX*NY+(y-1)*NX+x], d_mv_y[z*NX*NY+y*NX+x], d_mv_y[z*NX*NY+(y+1)*NX+x]);
u3y = ImageGradient(d_mv_z[z*NX*NY+(y-1)*NX+x], d_mv_z[z*NX*NY+y*NX+x], d_mv_z[z*NX*NY+(y+1)*NX+x]);
}
else
{
u1y = 0;
u2y = 0;
u3y = 0;
}
if(z+1<NZ && z-1>=0)
{
u1z = ImageGradient(d_mv_x[(z-1)*NX*NY+y*NX+x], d_mv_x[z*NX*NY+y*NX+x], d_mv_x[(z+1)*NX*NY+y*NX+x]);
u2z = ImageGradient(d_mv_y[(z-1)*NX*NY+y*NX+x], d_mv_y[z*NX*NY+y*NX+x], d_mv_y[(z+1)*NX*NY+y*NX+x]);
u3z = ImageGradient(d_mv_z[(z-1)*NX*NY+y*NX+x], d_mv_z[z*NX*NY+y*NX+x], d_mv_z[(z+1)*NX*NY+y*NX+x]);
}
else
{
u1z = 0;
u2z = 0;
u3z = 0;
}
float R1 = d_v_x[tid] - d_v_x[tid]*u1x - d_v_y[tid]*u1y - d_v_z[tid]*u1z;
float R2 = d_v_y[tid] - d_v_x[tid]*u2x - d_v_y[tid]*u2y - d_v_z[tid]*u2z;
float R3 = d_v_z[tid] - d_v_x[tid]*u3x - d_v_y[tid]*u3y - d_v_z[tid]*u3z;
float jaco = (1.0f-u1x)*(1.0f-u2y)*(1.0f-u3z)-u3x*u1y*u2z - u2x*u3y*u1z
-(1.0f-u1x)*u3y*u2z - u3x*(1.0f-u2y)*u1z - u2x*u1y*(1.0f-u3z);
jacobian[tid] = (fabs(jaco)<= threshJaco) ? 1.0f : 0.0f;
flow[tid] = sqrtf(R1 * R1 + R2 * R2 + R3 * R3);
// d_v_x, d_v_y, d_v_z become displacement
d_v_x[tid] = R1;
d_v_y[tid] = R2;
d_v_z[tid] = R3;
}
}
__global__ void flowUpdate(float *d_mv_x, float *d_mv_y, float *d_mv_z, float *d_disp_x, float *d_disp_y, float *d_disp_z, float dt, int NX, int NY, int NZ)
{
const int tid = (blockIdx.y*NBLOCKX + blockIdx.x)*blockDim.x + threadIdx.x;
if (tid<NX*NY*NZ)
{
d_mv_x[tid] += d_disp_x[tid]*dt;
d_mv_y[tid] += d_disp_y[tid]*dt;
d_mv_z[tid] += d_disp_z[tid]*dt;
}
}
__device__ float ImageGradient(float Im, float I, float Ip)
{
float xp = Ip-I;
float xm = I - Im;
return minmod(xp, xm);
}
__global__ void ImageWarp(float *mv_x, float *mv_y, float *mv_z, float *dest, int NX, int NY, int NZ)
{
const int tid = (blockIdx.y*NBLOCKX + blockIdx.x)*blockDim.x + threadIdx.x;
// obtain current id on
if(tid < NX*NY*NZ)
{
int z = tid/(NX*NY);
int y = (tid%(NX*NY))/NX;
int x = tid%NX;
float v_x = mv_x[tid];
float v_y = mv_y[tid];
float v_z = mv_z[tid];
dest[tid] = tex3D(d_im_move_tex, x-v_x+0.5, y-v_y+0.5, z-v_z+0.5);
}
}
__global__ void ImageWarp_mv(float *mv_x, float *mv_y, float *mv_z, int NX, int NY, int NZ)
{
const int tid = (blockIdx.y*NBLOCKX + blockIdx.x)*blockDim.x + threadIdx.x;
// obtain current id on
if(tid < NX*NY*NZ)
{
int z = tid/(NX*NY);
int y = (tid%(NX*NY))/NX;
int x = tid%NX;
float v_x = mv_x[tid];
float v_y = mv_y[tid];
float v_z = mv_z[tid];
mv_x[tid] += tex3D(d_mv_x_tex, x-v_x+0.5, y-v_y+0.5,z-v_z+0.5) ;
mv_y[tid] += tex3D(d_mv_y_tex, x-v_x+0.5, y-v_y+0.5,z-v_z+0.5) ;
mv_z[tid] += tex3D(d_mv_z_tex, x-v_x+0.5, y-v_y+0.5,z-v_z+0.5) ;
}
}
__global__ void ImageWarp_final(float *mv_x, float *mv_y, float *mv_z, float *dest, int NX, int NY, int NZ)
{
const int tid = (blockIdx.y*NBLOCKX + blockIdx.x)*blockDim.x + threadIdx.x;
// obtain current id on
if(tid < NX*NY*NZ)
{
int z = tid/(NX*NY);
int y = (tid%(NX*NY))/NX;
int x = tid%NX;
mv_x[tid] = tex3D(d_mv_x_tex, x, y, z) ;
mv_y[tid] = tex3D(d_mv_y_tex, x, y, z) ;
mv_z[tid] = tex3D(d_mv_z_tex, x, y, z) ;
dest[tid] = tex3D(d_im_move_tex, x-mv_x[tid]+0.5, y-mv_y[tid]+0.5, z-mv_z[tid]+0.5);
}
}
__host__ __device__ float minmod(float x, float y)
{
if (x*y<=0)
return 0;
if (x>0)
{
if(x>y)
return y;
else return x;
}
else
{
if(x>y) return x;
else return y;
}
}
#endif