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
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or bpied warranties, including, but not limited to, the bpied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable
////////////////////////////////////////////////////////////////////////
// buildPointList
#define PIXELS_PER_THREAD 16
// TODO: add offset to support ROI
__kernel void buildPointList(__global const uchar* src,
int cols,
int rows,
int step,
__global unsigned int* list,
__global int* counter)
{
__local unsigned int s_queues[4][32 * PIXELS_PER_THREAD];
__local int s_qsize[4];
__local int s_globStart[4];
const int x = get_group_id(0) * get_local_size(0) * PIXELS_PER_THREAD + get_local_id(0);
const int y = get_global_id(1);
if (get_local_id(0) == 0)
s_qsize[get_local_id(1)] = 0;
barrier(CLK_LOCAL_MEM_FENCE);
if (y < rows)
{
// fill the queue
__global const uchar* srcRow = &src[y * step];
for (int i = 0, xx = x; i < PIXELS_PER_THREAD && xx < cols; ++i, xx += get_local_size(0))
{
if (srcRow[xx])
{
const unsigned int val = (y << 16) | xx;
const int qidx = atomic_add(&s_qsize[get_local_id(1)], 1);
s_queues[get_local_id(1)][qidx] = val;
}
}
}
barrier(CLK_LOCAL_MEM_FENCE);
// let one work-item reserve the space required in the global list
if (get_local_id(0) == 0 && get_local_id(1) == 0)
{
// find how many items are stored in each list
int totalSize = 0;
for (int i = 0; i < get_local_size(1); ++i)
{
s_globStart[i] = totalSize;
totalSize += s_qsize[i];
}
// calculate the offset in the global list
const int globalOffset = atomic_add(counter, totalSize);
for (int i = 0; i < get_local_size(1); ++i)
s_globStart[i] += globalOffset;
}
barrier(CLK_GLOBAL_MEM_FENCE);
// copy local queues to global queue
const int qsize = s_qsize[get_local_id(1)];
int gidx = s_globStart[get_local_id(1)] + get_local_id(0);
for(int i = get_local_id(0); i < qsize; i += get_local_size(0), gidx += get_local_size(0))
list[gidx] = s_queues[get_local_id(1)][i];
}
////////////////////////////////////////////////////////////////////////
// circlesAccumCenters
// TODO: add offset to support ROI
__kernel void circlesAccumCenters(__global const unsigned int* list,
const int count,
__global const int* dx,
const int dxStep,
__global const int* dy,
const int dyStep,
__global int* accum,
const int accumStep,
const int width,
const int height,
const int minRadius,
const int maxRadius,
const float idp)
{
const int dxStepInPixel = dxStep / sizeof(int);
const int dyStepInPixel = dyStep / sizeof(int);
const int accumStepInPixel = accumStep / sizeof(int);
const int SHIFT = 10;
const int ONE = 1 << SHIFT;
// const int tid = blockIdx.x * blockDim.x + threadIdx.x;
const int wid = get_global_id(0);
if (wid >= count)
return;
const unsigned int val = list[wid];
const int x = (val & 0xFFFF);
const int y = (val >> 16) & 0xFFFF;
const int vx = dx[mad24(y, dxStepInPixel, x)];
const int vy = dy[mad24(y, dyStepInPixel, x)];
if (vx == 0 && vy == 0)
return;
const float mag = sqrt(convert_float(vx * vx + vy * vy));
const int x0 = convert_int_rte((x * idp) * ONE);
const int y0 = convert_int_rte((y * idp) * ONE);
int sx = convert_int_rte((vx * idp) * ONE / mag);
int sy = convert_int_rte((vy * idp) * ONE / mag);
// Step from minRadius to maxRadius in both directions of the gradient
for (int k1 = 0; k1 < 2; ++k1)
{
int x1 = x0 + minRadius * sx;
int y1 = y0 + minRadius * sy;
for (int r = minRadius; r <= maxRadius; x1 += sx, y1 += sy, ++r)
{
const int x2 = x1 >> SHIFT;
const int y2 = y1 >> SHIFT;
if (x2 < 0 || x2 >= width || y2 < 0 || y2 >= height)
break;
atomic_add(&accum[mad24(y2+1, accumStepInPixel, x2+1)], 1);
}
sx = -sx;
sy = -sy;
}
}
// ////////////////////////////////////////////////////////////////////////
// // buildCentersList
// TODO: add offset to support ROI
__kernel void buildCentersList(__global const int* accum,
const int accumCols,
const int accumRows,
const int accumStep,
__global unsigned int* centers,
const int threshold,
__global int* counter)
{
const int accumStepInPixel = accumStep/sizeof(int);
const int x = get_global_id(0);
const int y = get_global_id(1);
if (x < accumCols - 2 && y < accumRows - 2)
{
const int top = accum[mad24(y, accumStepInPixel, x + 1)];
const int left = accum[mad24(y + 1, accumStepInPixel, x)];
const int cur = accum[mad24(y + 1, accumStepInPixel, x + 1)];
const int right = accum[mad24(y + 1, accumStepInPixel, x + 2)];
const int bottom = accum[mad24(y + 2, accumStepInPixel, x + 1)];;
if (cur > threshold && cur > top && cur >= bottom && cur > left && cur >= right)
{
const unsigned int val = (y << 16) | x;
const int idx = atomic_add(counter, 1);
centers[idx] = val;
}
}
}
// ////////////////////////////////////////////////////////////////////////
// // circlesAccumRadius
// TODO: add offset to support ROI
__kernel void circlesAccumRadius(__global const unsigned int* centers,
__global const unsigned int* list, const int count,
__global float4* circles, const int maxCircles,
const float dp,
const int minRadius, const int maxRadius,
const int histSize,
const int threshold,
__local int* smem,
__global int* counter)
{
for (int i = get_local_id(0); i < histSize + 2; i += get_local_size(0))
smem[i] = 0;
barrier(CLK_LOCAL_MEM_FENCE);
unsigned int val = centers[get_group_id(0)];
float cx = convert_float(val & 0xFFFF);
float cy = convert_float((val >> 16) & 0xFFFF);
cx = (cx + 0.5f) * dp;
cy = (cy + 0.5f) * dp;
for (int i = get_local_id(0); i < count; i += get_local_size(0))
{
val = list[i];
const int x = (val & 0xFFFF);
const int y = (val >> 16) & 0xFFFF;
const float rad = sqrt((cx - x) * (cx - x) + (cy - y) * (cy - y));
if (rad >= minRadius && rad <= maxRadius)
{
const int r = convert_int_rte(rad - minRadius);
atomic_add(&smem[r + 1], 1);
}
}
barrier(CLK_LOCAL_MEM_FENCE);
for (int i = get_local_id(0); i < histSize; i += get_local_size(0))
{
const int curVotes = smem[i + 1];
if (curVotes >= threshold && curVotes > smem[i] && curVotes >= smem[i + 2])
{
const int ind = atomic_add(counter, 1);
if (ind < maxCircles)
{
circles[ind] = (float4)(cx, cy, convert_float(i + minRadius), 0.0f);
}
}
}
}