1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
5 // Copyright (C) 2014, Itseez, Inc., all rights reserved.
6 // Third party copyrights are property of their respective owners.
8 #define ACCUM(ptr) *((__global int*)(ptr))
10 #ifdef MAKE_POINTS_LIST
12 __kernel void make_point_list(__global const uchar * src_ptr, int src_step, int src_offset, int src_rows, int src_cols,
13 __global uchar * list_ptr, int list_step, int list_offset, __global int* global_offset)
15 int x = get_local_id(0);
16 int y = get_group_id(1);
18 __local int l_index, l_offset;
19 __local int l_points[LOCAL_SIZE];
20 __global const uchar * src = src_ptr + mad24(y, src_step, src_offset);
21 __global int * list = (__global int*)(list_ptr + list_offset);
26 barrier(CLK_LOCAL_MEM_FENCE);
32 for (int i=x; i < src_cols; i+=GROUP_SIZE)
37 int index = atomic_inc(&l_index);
38 l_points[index] = val;
43 barrier(CLK_LOCAL_MEM_FENCE);
46 l_offset = atomic_add(global_offset, l_index);
48 barrier(CLK_LOCAL_MEM_FENCE);
51 for (int i=x; i < l_index; i+=GROUP_SIZE)
53 list[i] = l_points[i];
57 #elif defined FILL_ACCUM_GLOBAL
59 __kernel void fill_accum_global(__global const uchar * list_ptr, int list_step, int list_offset,
60 __global uchar * accum_ptr, int accum_step, int accum_offset,
61 int total_points, float irho, float theta, int numrho, int numangle)
63 int theta_idx = get_global_id(1);
64 int count_idx = get_global_id(0);
65 int glob_size = get_global_size(0);
67 float sinVal = sincos(theta * ((float)theta_idx), &cosVal);
71 __global const int * list = (__global const int*)(list_ptr + list_offset);
72 __global int* accum = (__global int*)(accum_ptr + mad24(theta_idx + 1, accum_step, accum_offset));
73 const int shift = (numrho - 1) / 2;
75 if (theta_idx < numangle)
77 for (int i = count_idx; i < total_points; i += glob_size)
79 const int val = list[i];
80 const int x = (val & 0xFFFF);
81 const int y = (val >> 16) & 0xFFFF;
83 int r = convert_int_rte(mad(x, cosVal, y * sinVal)) + shift;
84 atomic_inc(accum + r + 1);
89 #elif defined FILL_ACCUM_LOCAL
91 __kernel void fill_accum_local(__global const uchar * list_ptr, int list_step, int list_offset,
92 __global uchar * accum_ptr, int accum_step, int accum_offset,
93 int total_points, float irho, float theta, int numrho, int numangle)
95 int theta_idx = get_group_id(1);
96 int count_idx = get_local_id(0);
98 if (theta_idx > 0 && theta_idx < numangle + 1)
101 float sinVal = sincos(theta * (float) (theta_idx-1), &cosVal);
105 __local int l_accum[BUFFER_SIZE];
106 for (int i=count_idx; i<BUFFER_SIZE; i+=LOCAL_SIZE)
109 barrier(CLK_LOCAL_MEM_FENCE);
111 __global const int * list = (__global const int*)(list_ptr + list_offset);
112 const int shift = (numrho - 1) / 2;
114 for (int i = count_idx; i < total_points; i += LOCAL_SIZE)
116 const int point = list[i];
117 const int x = (point & 0xFFFF);
118 const int y = point >> 16;
120 int r = convert_int_rte(mad(x, cosVal, y * sinVal)) + shift;
121 atomic_inc(l_accum + r + 1);
124 barrier(CLK_LOCAL_MEM_FENCE);
126 __global int* accum = (__global int*)(accum_ptr + mad24(theta_idx, accum_step, accum_offset));
127 for (int i=count_idx; i<BUFFER_SIZE; i+=LOCAL_SIZE)
128 accum[i] = l_accum[i];
130 else if (theta_idx < numangle + 2)
132 __global int* accum = (__global int*)(accum_ptr + mad24(theta_idx, accum_step, accum_offset));
133 for (int i=count_idx; i<BUFFER_SIZE; i+=LOCAL_SIZE)
138 #elif defined GET_LINES
140 __kernel void get_lines(__global uchar * accum_ptr, int accum_step, int accum_offset, int accum_rows, int accum_cols,
141 __global uchar * lines_ptr, int lines_step, int lines_offset, __global int* lines_index_ptr,
142 int linesMax, int threshold, float rho, float theta)
144 int x0 = get_global_id(0);
145 int y = get_global_id(1);
146 int glob_size = get_global_size(0);
148 if (y < accum_rows-2)
150 __global uchar* accum = accum_ptr + mad24(y+1, accum_step, mad24(x0+1, (int) sizeof(int), accum_offset));
151 __global float2* lines = (__global float2*)(lines_ptr + lines_offset);
152 __global int* lines_index = lines_index_ptr + 1;
154 for (int x=x0; x<accum_cols-2; x+=glob_size)
156 int curVote = ACCUM(accum);
158 if (curVote > threshold && curVote > ACCUM(accum - sizeof(int)) && curVote >= ACCUM(accum + sizeof(int)) &&
159 curVote > ACCUM(accum - accum_step) && curVote >= ACCUM(accum + accum_step))
161 int index = atomic_inc(lines_index);
163 if (index < linesMax)
165 float radius = (x - (accum_cols - 3) * 0.5f) * rho;
166 float angle = y * theta;
168 lines[index] = (float2)(radius, angle);
172 accum += glob_size * (int) sizeof(int);
177 #elif GET_LINES_PROBABOLISTIC
179 __kernel void get_lines(__global const uchar * accum_ptr, int accum_step, int accum_offset, int accum_rows, int accum_cols,
180 __global const uchar * src_ptr, int src_step, int src_offset, int src_rows, int src_cols,
181 __global uchar * lines_ptr, int lines_step, int lines_offset, __global int* lines_index_ptr,
182 int linesMax, int threshold, int lineLength, int lineGap, float rho, float theta)
184 int x = get_global_id(0);
185 int y = get_global_id(1);
187 if (y < accum_rows-2)
189 __global uchar* accum = accum_ptr + mad24(y+1, accum_step, mad24(x+1, (int) sizeof(int), accum_offset));
190 __global int4* lines = (__global int4*)(lines_ptr + lines_offset);
191 __global int* lines_index = lines_index_ptr + 1;
193 int curVote = ACCUM(accum);
195 if (curVote >= threshold &&
196 curVote > ACCUM(accum - accum_step - sizeof(int)) &&
197 curVote > ACCUM(accum - accum_step) &&
198 curVote > ACCUM(accum - accum_step + sizeof(int)) &&
199 curVote > ACCUM(accum - sizeof(int)) &&
200 curVote > ACCUM(accum + sizeof(int)) &&
201 curVote > ACCUM(accum + accum_step - sizeof(int)) &&
202 curVote > ACCUM(accum + accum_step) &&
203 curVote > ACCUM(accum + accum_step + sizeof(int)))
205 const float radius = (x - (accum_cols - 2 - 1) * 0.5f) * rho;
206 const float angle = y * theta;
209 float sina = sincos(angle, &cosa);
211 float2 p0 = (float2)(cosa * radius, sina * radius);
212 float2 dir = (float2)(-sina, cosa);
214 float2 pb[4] = { (float2)(-1, -1), (float2)(-1, -1), (float2)(-1, -1), (float2)(-1, -1) };
221 pb[0].y = p0.y + a * dir.y;
223 a = (src_cols - 1 - p0.x) / dir.x;
224 pb[1].x = src_cols - 1;
225 pb[1].y = p0.y + a * dir.y;
231 pb[2].x = p0.x + a * dir.x;
234 a = (src_rows - 1 - p0.y) / dir.y;
235 pb[3].x = p0.x + a * dir.x;
236 pb[3].y = src_rows - 1;
239 if (pb[0].x == 0 && (pb[0].y >= 0 && pb[0].y < src_rows))
245 else if (pb[1].x == src_cols - 1 && (pb[1].y >= 0 && pb[1].y < src_rows))
251 else if (pb[2].y == 0 && (pb[2].x >= 0 && pb[2].x < src_cols))
257 else if (pb[3].y == src_rows - 1 && (pb[3].x >= 0 && pb[3].x < src_cols))
264 dir /= max(fabs(dir.x), fabs(dir.y));
270 if (p0.x < 0 || p0.x >= src_cols || p0.y < 0 || p0.y >= src_rows)
275 if (*(src_ptr + mad24(p0.y, src_step, p0.x + src_offset)))
294 bool good_line = fabs(line_end[1].x - line_end[0].x) >= lineLength ||
295 fabs(line_end[1].y - line_end[0].y) >= lineLength;
299 int index = atomic_inc(lines_index);
300 if (index < linesMax)
301 lines[index] = (int4)(line_end[0].x, line_end[0].y, line_end[1].x, line_end[1].y);
310 if (p0.x < 0 || p0.x >= src_cols || p0.y < 0 || p0.y >= src_rows)
314 bool good_line = fabs(line_end[1].x - line_end[0].x) >= lineLength ||
315 fabs(line_end[1].y - line_end[0].y) >= lineLength;
319 int index = atomic_inc(lines_index);
320 if (index < linesMax)
321 lines[index] = (int4)(line_end[0].x, line_end[0].y, line_end[1].x, line_end[1].y);