From c6772a8f5ded16f63e29c03d077a1d9ad22aa30d Mon Sep 17 00:00:00 2001 From: Tetragramm Date: Fri, 21 Oct 2016 19:54:42 -0500 Subject: [PATCH 1/2] Fix CLAHE distribution. Fix CLAHE for 16-bit images. --- modules/imgproc/src/clahe.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/modules/imgproc/src/clahe.cpp b/modules/imgproc/src/clahe.cpp index 342b393577..fa571f0318 100644 --- a/modules/imgproc/src/clahe.cpp +++ b/modules/imgproc/src/clahe.cpp @@ -212,8 +212,12 @@ namespace for (int i = 0; i < histSize; ++i) tileHist[i] += redistBatch; - for (int i = 0; i < residual; ++i) - tileHist[i]++; + if (residual != 0) + { + int residualStep = MAX(histSize / residual, 1); + for (int i = 0; i < histSize; i += residualStep) + tileHist[i]++; + } } // calc Lut @@ -359,7 +363,7 @@ namespace bool useOpenCL = cv::ocl::useOpenCL() && _src.isUMat() && _src.dims()<=2 && _src.type() == CV_8UC1; #endif - int histSize = _src.type() == CV_8UC1 ? 256 : 4096; + int histSize = _src.type() == CV_8UC1 ? 256 : 65536; cv::Size tileSize; cv::_InputArray _srcForLut; @@ -416,7 +420,7 @@ namespace if (_src.type() == CV_8UC1) calcLutBody = cv::makePtr >(srcForLut, lut_, tileSize, tilesX_, clipLimit, lutScale); else if (_src.type() == CV_16UC1) - calcLutBody = cv::makePtr >(srcForLut, lut_, tileSize, tilesX_, clipLimit, lutScale); + calcLutBody = cv::makePtr >(srcForLut, lut_, tileSize, tilesX_, clipLimit, lutScale); else CV_Error( CV_StsBadArg, "Unsupported type" ); @@ -426,7 +430,7 @@ namespace if (_src.type() == CV_8UC1) interpolationBody = cv::makePtr >(src, dst, lut_, tileSize, tilesX_, tilesY_); else if (_src.type() == CV_16UC1) - interpolationBody = cv::makePtr >(src, dst, lut_, tileSize, tilesX_, tilesY_); + interpolationBody = cv::makePtr >(src, dst, lut_, tileSize, tilesX_, tilesY_); cv::parallel_for_(cv::Range(0, src.rows), *interpolationBody); } From 17df65e666c5e49e2a5ca5a38106e90411c92c90 Mon Sep 17 00:00:00 2001 From: Tetragramm Date: Thu, 3 Nov 2016 20:41:16 -0500 Subject: [PATCH 2/2] Fix the OpenCL portion to match the c++ code. Fix an undiscovered bug in the c++ code. --- modules/imgproc/src/clahe.cpp | 2 +- modules/imgproc/src/opencl/clahe.cl | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) 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)