diff --git a/modules/imgproc/src/clahe.cpp b/modules/imgproc/src/clahe.cpp index fa571f0318..1bec0387d4 100644 --- a/modules/imgproc/src/clahe.cpp +++ b/modules/imgproc/src/clahe.cpp @@ -215,7 +215,7 @@ namespace if (residual != 0) { int residualStep = MAX(histSize / residual, 1); - for (int i = 0; i < histSize; i += residualStep) + for (int i = 0; i < histSize && residual > 0; i += residualStep, residual--) tileHist[i]++; } } diff --git a/modules/imgproc/src/opencl/clahe.cl b/modules/imgproc/src/opencl/clahe.cl index 9f88b20bfd..187933ce0c 100644 --- a/modules/imgproc/src/opencl/clahe.cl +++ b/modules/imgproc/src/opencl/clahe.cl @@ -201,7 +201,10 @@ __kernel void calcLut(__global __const uchar * src, const int srcStep, tHistVal += redistBatch; int residual = totalClipped - redistBatch * 256; - if (tid < residual) + int rStep = 256 / residual; + if (rStep < 1) + rStep = 1; + if (tid%rStep == 0 && (tid/rStep)