diff --git a/modules/imgproc/src/opencl/canny.cl b/modules/imgproc/src/opencl/canny.cl index caa7969032e320b3bfc6d6b52439d2d89d30d9ec..7a531e1b46f1d8f15f39c4a3bb394adf43790605 100644 --- a/modules/imgproc/src/opencl/canny.cl +++ b/modules/imgproc/src/opencl/canny.cl @@ -430,7 +430,12 @@ __kernel void stage2_hysteresis(__global uchar *map_ptr, int map_step, int map_o for (int i = 0; i < pix_per_thr; ++i) { - ushort2 pos = l_stack[ atomic_dec(&l_counter) - 1 ]; + int index = atomic_dec(&l_counter) - 1; + if (index < 0) { + atomic_inc(&l_counter); + continue; + } + ushort2 pos = l_stack[ index ]; #pragma unroll for (int j = 0; j < 8; ++j)