-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGPUDiscreteGaussianGradientImageFilter.cl
196 lines (166 loc) · 6.29 KB
/
GPUDiscreteGaussianGradientImageFilter.cl
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
/*=========================================================================
Ibis Neuronav
Copyright (c) Simon Drouin, Anna Kochanowska, Louis Collins.
All rights reserved.
See Copyright.txt or http://ibisneuronav.org/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// Thanks to Dante De Nigris for writing this class
// Modified on Oct 18, 2021 by Houssem Gueziri:
// - unify kernels to compute image gradient
#define INTYPE float
#define OUTTYPE float
#define MASKTYPE unsigned char
#define OPTYPE float
#define OUTTYPE4 float4
#define OUTTYPE2 float2
#ifdef DIM_3
__kernel void SeparableNeighborOperatorFilterWithMask(const __global INTYPE * in,
__global OUTTYPE4 * out,
const __global MASKTYPE * mask,
__constant OPTYPE * opx, __constant OPTYPE * opy, __constant OPTYPE * opz,
int radiusx, int radiusy, int radiusz,
int width, int height, int depth,
OPTYPE spx, OPTYPE spy, OPTYPE spz)
{
int gix = get_global_id(0);
int giy = get_global_id(1);
int giz = get_global_id(2);
unsigned int gidx = width * (giz * height + giy) + gix;
OPTYPE sumx = 0;
OPTYPE sumy = 0;
OPTYPE sumz = 0;
OUTTYPE norm = (OUTTYPE)0.0f;
bool maskBool = true;
unsigned int opIdx = 0;
bool isValid = true;
if( gix < 0 || gix >= width ) isValid = false;
if( giy < 0 || giy >= height ) isValid = false;
if( giz < 0 || giz >= depth ) isValid = false;
if( isValid )
{
if( mask[gidx] > 0.0f )
{
float4 gradient = (float4)(0.0f, 0.0f, 0.0f, 0.0f);
for( int x = gix - radiusx; x <= gix + radiusx; x++ )
{
unsigned int cidx = width * (giz * height + giy) + (unsigned int)(min(max(0, x), width - 1));
sumx += (OPTYPE)in[cidx] * (OPTYPE)opx[opIdx];
if( in[cidx] < THRESHOLD ) maskBool = false;
opIdx++;
}
gradient.x = (OUTTYPE)(sumx / spx);
opIdx = 0;
for( int y = giy - radiusy; y <= giy + radiusy; y++ )
{
unsigned int yid = (unsigned int)(min(max(0, y), height - 1));
unsigned int cidx = width * (giz * height + yid) + gix;
sumy += (OPTYPE)in[cidx] * (OPTYPE)opy[opIdx];
if( in[cidx] < THRESHOLD ) maskBool = false;
opIdx++;
}
gradient.y = (OUTTYPE)(sumy / spy);
opIdx = 0;
for( int z = giz - radiusz; z <= giz + radiusz; z++ )
{
unsigned int zid = (unsigned int)(min(max(0, z), depth - 1));
unsigned int cidx = width * (zid * height + giy) + gix;
sumz += (OPTYPE)in[cidx] * (OPTYPE)opz[opIdx];
if( in[cidx] < THRESHOLD ) maskBool = false;
opIdx++;
}
gradient.z = (OUTTYPE)(sumz / spz);
if( maskBool == true ) gradient.w = 1.0f;
out[gidx] = gradient;
}
else
{
out[gidx] = (float4)(0.0f, 0.0f, 0.0f, -1.0f);
}
}
}
#endif
#ifdef DIM_2
__kernel void SeparableNeighborOperatorFilter(const __global INTYPE * in,
__global OUTTYPE2 * out,
__constant OPTYPE * opx, __constant OPTYPE * opy,
int radiusx, int radiusy,
int width, int height,
OPTYPE spx, OPTYPE spy)
{
int gix = get_global_id(0);
int giy = get_global_id(1);
unsigned int gidx = width * giy + gix;
OPTYPE sumx = 0;
OPTYPE sumy = 0;
unsigned int opIdx = 0;
bool isValid = true;
if( gix < 0 || gix >= width ) isValid = false;
if( giy < 0 || giy >= height ) isValid = false;
if( isValid )
{
OUTTYPE2 gradient = (OUTTYPE2)(0.0f, 0.0f);
for( int x = gix - radiusx; x <= gix + radiusx; x++ )
{
unsigned int cidx = width * giy + (unsigned int)(min(max(0, x), width - 1));
sumx += (OPTYPE)in[cidx] * (OPTYPE)opx[opIdx];
opIdx++;
}
gradient.x = (OUTTYPE)(sumx / spx);
opIdx = 0;
for( int y = giy - radiusy; y <= giy + radiusy; y++ )
{
unsigned int yid = (unsigned int)(min(max(0, y), height - 1));
unsigned int cidx = width * yid + gix;
sumy += (OPTYPE)in[cidx] * (OPTYPE)opy[opIdx];
opIdx++;
}
gradient.y = (OUTTYPE)(sumy / spy);
out[gidx] = gradient;
}
}
#endif
#ifdef DIM_2_ALL
__kernel void SeparableNeighborOperatorFilter(const __global INTYPE * in,
__global OUTTYPE2 * out,
__constant OPTYPE * opx, __constant OPTYPE * opy,
int radiusx, int radiusy,
int width, int height, int depth,
OPTYPE spx, OPTYPE spy)
{
int gix = get_global_id(0);
int giy = get_global_id(1);
int giz = get_global_id(2);
unsigned int gidx = width * (giz * height + giy) + gix;
OPTYPE sumx = 0;
OPTYPE sumy = 0;
unsigned int opIdx = 0;
bool isValid = true;
if( gix < 0 || gix >= width ) isValid = false;
if( giy < 0 || giy >= height ) isValid = false;
if( giz < 0 || giz >= depth ) isValid = false;
if( isValid )
{
OUTTYPE2 gradient = (OUTTYPE2)(0.0f, 0.0f);
for( int x = gix - radiusx; x <= gix + radiusx; x++ )
{
unsigned int cidx = width * giz * height + width * giy + (unsigned int)(min(max(0, x), width - 1));
sumx += (OPTYPE)in[cidx] * (OPTYPE)opx[opIdx];
opIdx++;
}
gradient.x = (OUTTYPE)(sumx / spx);
opIdx = 0;
for( int y = giy - radiusy; y <= giy + radiusy; y++ )
{
unsigned int yid = (unsigned int)(min(max(0, y), height - 1));
unsigned int cidx = width * giz * height + width * yid + gix;
sumy += (OPTYPE)in[cidx] * (OPTYPE)opy[opIdx];
opIdx++;
}
gradient.y = (OUTTYPE)(sumy / spy);
out[gidx] = gradient;
}
}
#endif