| 1 | /*M/////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. |
| 4 | // |
| 5 | // By downloading, copying, installing or using the software you agree to this license. |
| 6 | // If you do not agree to this license, do not download, install, |
| 7 | // copy or use the software. |
| 8 | // |
| 9 | // |
| 10 | // License Agreement |
| 11 | // For Open Source Computer Vision Library |
| 12 | // |
| 13 | // Copyright (C) 2013, NVIDIA Corporation, all rights reserved. |
| 14 | // Copyright (C) 2014, Itseez Inc., all rights reserved. |
| 15 | // Third party copyrights are property of their respective owners. |
| 16 | // |
| 17 | // Redistribution and use in source and binary forms, with or without modification, |
| 18 | // are permitted provided that the following conditions are met: |
| 19 | // |
| 20 | // * Redistribution's of source code must retain the above copyright notice, |
| 21 | // this list of conditions and the following disclaimer. |
| 22 | // |
| 23 | // * Redistribution's in binary form must reproduce the above copyright notice, |
| 24 | // this list of conditions and the following disclaimer in the documentation |
| 25 | // and/or other materials provided with the distribution. |
| 26 | // |
| 27 | // * The name of the copyright holders may not be used to endorse or promote products |
| 28 | // derived from this software without specific prior written permission. |
| 29 | // |
| 30 | // This software is provided by the copyright holders and contributors "as is" and |
| 31 | // any express or implied warranties, including, but not limited to, the implied |
| 32 | // warranties of merchantability and fitness for a particular purpose are disclaimed. |
| 33 | // In no event shall the copyright holders or contributors be liable for any direct, |
| 34 | // indirect, incidental, special, exemplary, or consequential damages |
| 35 | // (including, but not limited to, procurement of substitute goods or services; |
| 36 | // loss of use, data, or profits; or business interruption) however caused |
| 37 | // and on any theory of liability, whether in contract, strict liability, |
| 38 | // or tort (including negligence or otherwise) arising in any way out of |
| 39 | // the use of this software, even if advised of the possibility of such damage. |
| 40 | // |
| 41 | //M*/ |
| 42 | |
| 43 | #include "precomp.hpp" |
| 44 | #include "opencl_kernels_imgproc.hpp" |
| 45 | |
| 46 | // ---------------------------------------------------------------------- |
| 47 | // CLAHE |
| 48 | |
| 49 | #ifdef HAVE_OPENCL |
| 50 | |
| 51 | namespace clahe |
| 52 | { |
| 53 | static bool calcLut(cv::InputArray _src, cv::OutputArray _dst, |
| 54 | const int tilesX, const int tilesY, const cv::Size tileSize, |
| 55 | const int clipLimit, const float lutScale) |
| 56 | { |
| 57 | cv::ocl::Kernel k("calcLut" , cv::ocl::imgproc::clahe_oclsrc); |
| 58 | if(k.empty()) |
| 59 | return false; |
| 60 | |
| 61 | cv::UMat src = _src.getUMat(); |
| 62 | _dst.create(rows: tilesX * tilesY, cols: 256, CV_8UC1); |
| 63 | cv::UMat dst = _dst.getUMat(); |
| 64 | |
| 65 | int tile_size[2]; |
| 66 | tile_size[0] = tileSize.width; |
| 67 | tile_size[1] = tileSize.height; |
| 68 | |
| 69 | size_t localThreads[3] = { 32, 8, 1 }; |
| 70 | size_t globalThreads[3] = { tilesX * localThreads[0], tilesY * localThreads[1], 1 }; |
| 71 | |
| 72 | int idx = 0; |
| 73 | idx = k.set(i: idx, arg: cv::ocl::KernelArg::ReadOnlyNoSize(m: src)); |
| 74 | idx = k.set(i: idx, arg: cv::ocl::KernelArg::WriteOnlyNoSize(m: dst)); |
| 75 | idx = k.set(idx, tile_size); |
| 76 | idx = k.set(idx, tilesX); |
| 77 | idx = k.set(idx, clipLimit); |
| 78 | k.set(idx, lutScale); |
| 79 | |
| 80 | return k.run(dims: 2, globalsize: globalThreads, localsize: localThreads, sync: false); |
| 81 | } |
| 82 | |
| 83 | static bool transform(cv::InputArray _src, cv::OutputArray _dst, cv::InputArray _lut, |
| 84 | const int tilesX, const int tilesY, const cv::Size & tileSize) |
| 85 | { |
| 86 | |
| 87 | cv::ocl::Kernel k("transform" , cv::ocl::imgproc::clahe_oclsrc); |
| 88 | if(k.empty()) |
| 89 | return false; |
| 90 | |
| 91 | int tile_size[2]; |
| 92 | tile_size[0] = tileSize.width; |
| 93 | tile_size[1] = tileSize.height; |
| 94 | |
| 95 | cv::UMat src = _src.getUMat(); |
| 96 | _dst.create(sz: src.size(), type: src.type()); |
| 97 | cv::UMat dst = _dst.getUMat(); |
| 98 | cv::UMat lut = _lut.getUMat(); |
| 99 | |
| 100 | size_t localThreads[3] = { 32, 8, 1 }; |
| 101 | size_t globalThreads[3] = { (size_t)src.cols, (size_t)src.rows, 1 }; |
| 102 | |
| 103 | int idx = 0; |
| 104 | idx = k.set(i: idx, arg: cv::ocl::KernelArg::ReadOnlyNoSize(m: src)); |
| 105 | idx = k.set(i: idx, arg: cv::ocl::KernelArg::WriteOnlyNoSize(m: dst)); |
| 106 | idx = k.set(i: idx, arg: cv::ocl::KernelArg::ReadOnlyNoSize(m: lut)); |
| 107 | idx = k.set(idx, src.cols); |
| 108 | idx = k.set(idx, src.rows); |
| 109 | idx = k.set(idx, tile_size); |
| 110 | idx = k.set(idx, tilesX); |
| 111 | k.set(idx, tilesY); |
| 112 | |
| 113 | return k.run(dims: 2, globalsize: globalThreads, localsize: localThreads, sync: false); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | #endif |
| 118 | |
| 119 | namespace |
| 120 | { |
| 121 | template <class T, int histSize, int shift> |
| 122 | class CLAHE_CalcLut_Body : public cv::ParallelLoopBody |
| 123 | { |
| 124 | public: |
| 125 | CLAHE_CalcLut_Body(const cv::Mat& src, const cv::Mat& lut, const cv::Size& tileSize, const int& tilesX, const int& clipLimit, const float& lutScale) : |
| 126 | src_(src), lut_(lut), tileSize_(tileSize), tilesX_(tilesX), clipLimit_(clipLimit), lutScale_(lutScale) |
| 127 | { |
| 128 | } |
| 129 | |
| 130 | void operator ()(const cv::Range& range) const CV_OVERRIDE; |
| 131 | |
| 132 | private: |
| 133 | cv::Mat src_; |
| 134 | mutable cv::Mat lut_; |
| 135 | |
| 136 | cv::Size tileSize_; |
| 137 | int tilesX_; |
| 138 | int clipLimit_; |
| 139 | float lutScale_; |
| 140 | }; |
| 141 | |
| 142 | template <class T, int histSize, int shift> |
| 143 | void CLAHE_CalcLut_Body<T,histSize,shift>::operator ()(const cv::Range& range) const |
| 144 | { |
| 145 | T* tileLut = lut_.ptr<T>(range.start); |
| 146 | const size_t lut_step = lut_.step / sizeof(T); |
| 147 | |
| 148 | for (int k = range.start; k < range.end; ++k, tileLut += lut_step) |
| 149 | { |
| 150 | const int ty = k / tilesX_; |
| 151 | const int tx = k % tilesX_; |
| 152 | |
| 153 | // retrieve tile submatrix |
| 154 | |
| 155 | cv::Rect tileROI; |
| 156 | tileROI.x = tx * tileSize_.width; |
| 157 | tileROI.y = ty * tileSize_.height; |
| 158 | tileROI.width = tileSize_.width; |
| 159 | tileROI.height = tileSize_.height; |
| 160 | |
| 161 | const cv::Mat tile = src_(tileROI); |
| 162 | |
| 163 | // calc histogram |
| 164 | |
| 165 | cv::AutoBuffer<int> _tileHist(histSize); |
| 166 | int* tileHist = _tileHist.data(); |
| 167 | std::fill(tileHist, tileHist + histSize, 0); |
| 168 | |
| 169 | int height = tileROI.height; |
| 170 | const size_t sstep = src_.step / sizeof(T); |
| 171 | for (const T* ptr = tile.ptr<T>(0); height--; ptr += sstep) |
| 172 | { |
| 173 | int x = 0; |
| 174 | for (; x <= tileROI.width - 4; x += 4) |
| 175 | { |
| 176 | int t0 = ptr[x], t1 = ptr[x+1]; |
| 177 | tileHist[t0 >> shift]++; tileHist[t1 >> shift]++; |
| 178 | t0 = ptr[x+2]; t1 = ptr[x+3]; |
| 179 | tileHist[t0 >> shift]++; tileHist[t1 >> shift]++; |
| 180 | } |
| 181 | |
| 182 | for (; x < tileROI.width; ++x) |
| 183 | tileHist[ptr[x] >> shift]++; |
| 184 | } |
| 185 | |
| 186 | // clip histogram |
| 187 | |
| 188 | if (clipLimit_ > 0) |
| 189 | { |
| 190 | // how many pixels were clipped |
| 191 | int clipped = 0; |
| 192 | for (int i = 0; i < histSize; ++i) |
| 193 | { |
| 194 | if (tileHist[i] > clipLimit_) |
| 195 | { |
| 196 | clipped += tileHist[i] - clipLimit_; |
| 197 | tileHist[i] = clipLimit_; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // redistribute clipped pixels |
| 202 | int redistBatch = clipped / histSize; |
| 203 | int residual = clipped - redistBatch * histSize; |
| 204 | |
| 205 | for (int i = 0; i < histSize; ++i) |
| 206 | tileHist[i] += redistBatch; |
| 207 | |
| 208 | if (residual != 0) |
| 209 | { |
| 210 | int residualStep = MAX(histSize / residual, 1); |
| 211 | for (int i = 0; i < histSize && residual > 0; i += residualStep, residual--) |
| 212 | tileHist[i]++; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // calc Lut |
| 217 | |
| 218 | int sum = 0; |
| 219 | for (int i = 0; i < histSize; ++i) |
| 220 | { |
| 221 | sum += tileHist[i]; |
| 222 | tileLut[i] = cv::saturate_cast<T>(sum * lutScale_); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | template <class T, int shift> |
| 228 | class CLAHE_Interpolation_Body : public cv::ParallelLoopBody |
| 229 | { |
| 230 | public: |
| 231 | CLAHE_Interpolation_Body(const cv::Mat& src, const cv::Mat& dst, const cv::Mat& lut, const cv::Size& tileSize, const int& tilesX, const int& tilesY) : |
| 232 | src_(src), dst_(dst), lut_(lut), tileSize_(tileSize), tilesX_(tilesX), tilesY_(tilesY) |
| 233 | { |
| 234 | buf.allocate(size: src.cols << 2); |
| 235 | ind1_p = buf.data(); |
| 236 | ind2_p = ind1_p + src.cols; |
| 237 | xa_p = (float *)(ind2_p + src.cols); |
| 238 | xa1_p = xa_p + src.cols; |
| 239 | |
| 240 | int lut_step = static_cast<int>(lut_.step / sizeof(T)); |
| 241 | float inv_tw = 1.0f / tileSize_.width; |
| 242 | |
| 243 | for (int x = 0; x < src.cols; ++x) |
| 244 | { |
| 245 | float txf = x * inv_tw - 0.5f; |
| 246 | |
| 247 | int tx1 = cvFloor(value: txf); |
| 248 | int tx2 = tx1 + 1; |
| 249 | |
| 250 | xa_p[x] = txf - tx1; |
| 251 | xa1_p[x] = 1.0f - xa_p[x]; |
| 252 | |
| 253 | tx1 = std::max(a: tx1, b: 0); |
| 254 | tx2 = std::min(tx2, tilesX_ - 1); |
| 255 | |
| 256 | ind1_p[x] = tx1 * lut_step; |
| 257 | ind2_p[x] = tx2 * lut_step; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void operator ()(const cv::Range& range) const CV_OVERRIDE; |
| 262 | |
| 263 | private: |
| 264 | cv::Mat src_; |
| 265 | mutable cv::Mat dst_; |
| 266 | cv::Mat lut_; |
| 267 | |
| 268 | cv::Size tileSize_; |
| 269 | int tilesX_; |
| 270 | int tilesY_; |
| 271 | |
| 272 | cv::AutoBuffer<int> buf; |
| 273 | int * ind1_p, * ind2_p; |
| 274 | float * xa_p, * xa1_p; |
| 275 | }; |
| 276 | |
| 277 | template <class T, int shift> |
| 278 | void CLAHE_Interpolation_Body<T, shift>::operator ()(const cv::Range& range) const |
| 279 | { |
| 280 | float inv_th = 1.0f / tileSize_.height; |
| 281 | |
| 282 | for (int y = range.start; y < range.end; ++y) |
| 283 | { |
| 284 | const T* srcRow = src_.ptr<T>(y); |
| 285 | T* dstRow = dst_.ptr<T>(y); |
| 286 | |
| 287 | float tyf = y * inv_th - 0.5f; |
| 288 | |
| 289 | int ty1 = cvFloor(value: tyf); |
| 290 | int ty2 = ty1 + 1; |
| 291 | |
| 292 | float ya = tyf - ty1, ya1 = 1.0f - ya; |
| 293 | |
| 294 | ty1 = std::max(a: ty1, b: 0); |
| 295 | ty2 = std::min(ty2, tilesY_ - 1); |
| 296 | |
| 297 | const T* lutPlane1 = lut_.ptr<T>(ty1 * tilesX_); |
| 298 | const T* lutPlane2 = lut_.ptr<T>(ty2 * tilesX_); |
| 299 | |
| 300 | for (int x = 0; x < src_.cols; ++x) |
| 301 | { |
| 302 | int srcVal = srcRow[x] >> shift; |
| 303 | |
| 304 | int ind1 = ind1_p[x] + srcVal; |
| 305 | int ind2 = ind2_p[x] + srcVal; |
| 306 | |
| 307 | float res = (lutPlane1[ind1] * xa1_p[x] + lutPlane1[ind2] * xa_p[x]) * ya1 + |
| 308 | (lutPlane2[ind1] * xa1_p[x] + lutPlane2[ind2] * xa_p[x]) * ya; |
| 309 | |
| 310 | dstRow[x] = cv::saturate_cast<T>(res) << shift; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | class CLAHE_Impl CV_FINAL : public cv::CLAHE |
| 316 | { |
| 317 | public: |
| 318 | CLAHE_Impl(double clipLimit = 40.0, int tilesX = 8, int tilesY = 8); |
| 319 | |
| 320 | void apply(cv::InputArray src, cv::OutputArray dst) CV_OVERRIDE; |
| 321 | |
| 322 | void setClipLimit(double clipLimit) CV_OVERRIDE; |
| 323 | double getClipLimit() const CV_OVERRIDE; |
| 324 | |
| 325 | void setTilesGridSize(cv::Size tileGridSize) CV_OVERRIDE; |
| 326 | cv::Size getTilesGridSize() const CV_OVERRIDE; |
| 327 | |
| 328 | void collectGarbage() CV_OVERRIDE; |
| 329 | |
| 330 | private: |
| 331 | double clipLimit_; |
| 332 | int tilesX_; |
| 333 | int tilesY_; |
| 334 | |
| 335 | cv::Mat srcExt_; |
| 336 | cv::Mat lut_; |
| 337 | |
| 338 | #ifdef HAVE_OPENCL |
| 339 | cv::UMat usrcExt_; |
| 340 | cv::UMat ulut_; |
| 341 | #endif |
| 342 | }; |
| 343 | |
| 344 | CLAHE_Impl::CLAHE_Impl(double clipLimit, int tilesX, int tilesY) : |
| 345 | clipLimit_(clipLimit), tilesX_(tilesX), tilesY_(tilesY) |
| 346 | { |
| 347 | } |
| 348 | |
| 349 | void CLAHE_Impl::apply(cv::InputArray _src, cv::OutputArray _dst) |
| 350 | { |
| 351 | CV_INSTRUMENT_REGION(); |
| 352 | |
| 353 | CV_Assert( _src.type() == CV_8UC1 || _src.type() == CV_16UC1 ); |
| 354 | |
| 355 | #ifdef HAVE_OPENCL |
| 356 | bool useOpenCL = cv::ocl::isOpenCLActivated() && _src.isUMat() && _src.dims()<=2 && _src.type() == CV_8UC1; |
| 357 | #endif |
| 358 | |
| 359 | int histSize = _src.type() == CV_8UC1 ? 256 : 65536; |
| 360 | |
| 361 | cv::Size tileSize; |
| 362 | cv::_InputArray _srcForLut; |
| 363 | |
| 364 | if (_src.size().width % tilesX_ == 0 && _src.size().height % tilesY_ == 0) |
| 365 | { |
| 366 | tileSize = cv::Size(_src.size().width / tilesX_, _src.size().height / tilesY_); |
| 367 | _srcForLut = _src; |
| 368 | } |
| 369 | else |
| 370 | { |
| 371 | #ifdef HAVE_OPENCL |
| 372 | if(useOpenCL) |
| 373 | { |
| 374 | cv::copyMakeBorder(src: _src, dst: usrcExt_, top: 0, bottom: tilesY_ - (_src.size().height % tilesY_), left: 0, right: tilesX_ - (_src.size().width % tilesX_), borderType: cv::BORDER_REFLECT_101); |
| 375 | tileSize = cv::Size(usrcExt_.size().width / tilesX_, usrcExt_.size().height / tilesY_); |
| 376 | _srcForLut = usrcExt_; |
| 377 | } |
| 378 | else |
| 379 | #endif |
| 380 | { |
| 381 | cv::copyMakeBorder(src: _src, dst: srcExt_, top: 0, bottom: tilesY_ - (_src.size().height % tilesY_), left: 0, right: tilesX_ - (_src.size().width % tilesX_), borderType: cv::BORDER_REFLECT_101); |
| 382 | tileSize = cv::Size(srcExt_.size().width / tilesX_, srcExt_.size().height / tilesY_); |
| 383 | _srcForLut = srcExt_; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | const int tileSizeTotal = tileSize.area(); |
| 388 | const float lutScale = static_cast<float>(histSize - 1) / tileSizeTotal; |
| 389 | |
| 390 | int clipLimit = 0; |
| 391 | if (clipLimit_ > 0.0) |
| 392 | { |
| 393 | clipLimit = static_cast<int>(clipLimit_ * tileSizeTotal / histSize); |
| 394 | clipLimit = std::max(a: clipLimit, b: 1); |
| 395 | } |
| 396 | |
| 397 | #ifdef HAVE_OPENCL |
| 398 | if (useOpenCL && clahe::calcLut(src: _srcForLut, dst: ulut_, tilesX: tilesX_, tilesY: tilesY_, tileSize, clipLimit, lutScale) ) |
| 399 | if( clahe::transform(_src, _dst, lut: ulut_, tilesX: tilesX_, tilesY: tilesY_, tileSize) ) |
| 400 | { |
| 401 | CV_IMPL_ADD(CV_IMPL_OCL); |
| 402 | return; |
| 403 | } |
| 404 | #endif |
| 405 | |
| 406 | cv::Mat src = _src.getMat(); |
| 407 | _dst.create( sz: src.size(), type: src.type() ); |
| 408 | cv::Mat dst = _dst.getMat(); |
| 409 | cv::Mat srcForLut = _srcForLut.getMat(); |
| 410 | lut_.create(rows: tilesX_ * tilesY_, cols: histSize, type: _src.type()); |
| 411 | |
| 412 | cv::Ptr<cv::ParallelLoopBody> calcLutBody; |
| 413 | if (_src.type() == CV_8UC1) |
| 414 | calcLutBody = cv::makePtr<CLAHE_CalcLut_Body<uchar, 256, 0> >(a1: srcForLut, a1: lut_, a1: tileSize, a1: tilesX_, a1: clipLimit, a1: lutScale); |
| 415 | else if (_src.type() == CV_16UC1) |
| 416 | calcLutBody = cv::makePtr<CLAHE_CalcLut_Body<ushort, 65536, 0> >(a1: srcForLut, a1: lut_, a1: tileSize, a1: tilesX_, a1: clipLimit, a1: lutScale); |
| 417 | else |
| 418 | CV_Error( cv::Error::StsBadArg, "Unsupported type" ); |
| 419 | |
| 420 | cv::parallel_for_(range: cv::Range(0, tilesX_ * tilesY_), body: *calcLutBody); |
| 421 | |
| 422 | cv::Ptr<cv::ParallelLoopBody> interpolationBody; |
| 423 | if (_src.type() == CV_8UC1) |
| 424 | interpolationBody = cv::makePtr<CLAHE_Interpolation_Body<uchar, 0> >(a1: src, a1: dst, a1: lut_, a1: tileSize, a1: tilesX_, a1: tilesY_); |
| 425 | else if (_src.type() == CV_16UC1) |
| 426 | interpolationBody = cv::makePtr<CLAHE_Interpolation_Body<ushort, 0> >(a1: src, a1: dst, a1: lut_, a1: tileSize, a1: tilesX_, a1: tilesY_); |
| 427 | |
| 428 | cv::parallel_for_(range: cv::Range(0, src.rows), body: *interpolationBody); |
| 429 | } |
| 430 | |
| 431 | void CLAHE_Impl::setClipLimit(double clipLimit) |
| 432 | { |
| 433 | clipLimit_ = clipLimit; |
| 434 | } |
| 435 | |
| 436 | double CLAHE_Impl::getClipLimit() const |
| 437 | { |
| 438 | return clipLimit_; |
| 439 | } |
| 440 | |
| 441 | void CLAHE_Impl::setTilesGridSize(cv::Size tileGridSize) |
| 442 | { |
| 443 | tilesX_ = tileGridSize.width; |
| 444 | tilesY_ = tileGridSize.height; |
| 445 | } |
| 446 | |
| 447 | cv::Size CLAHE_Impl::getTilesGridSize() const |
| 448 | { |
| 449 | return cv::Size(tilesX_, tilesY_); |
| 450 | } |
| 451 | |
| 452 | void CLAHE_Impl::collectGarbage() |
| 453 | { |
| 454 | srcExt_.release(); |
| 455 | lut_.release(); |
| 456 | #ifdef HAVE_OPENCL |
| 457 | usrcExt_.release(); |
| 458 | ulut_.release(); |
| 459 | #endif |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | cv::Ptr<cv::CLAHE> cv::createCLAHE(double clipLimit, cv::Size tileGridSize) |
| 464 | { |
| 465 | return makePtr<CLAHE_Impl>(a1: clipLimit, a1: tileGridSize.width, a1: tileGridSize.height); |
| 466 | } |
| 467 | |