1
/*M///////////////////////////////////////////////////////////////////////////////////////
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11
// For Open Source Computer Vision Library
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
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.
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.
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 Intel Corporation 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.
43
#include "precomp.hpp"
46
using namespace cv::cuda;
48
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
50
void cv::cuda::calcHist(InputArray, OutputArray, Stream&) { throw_no_cuda(); }
52
void cv::cuda::equalizeHist(InputArray, OutputArray, Stream&) { throw_no_cuda(); }
54
cv::Ptr<cv::cuda::CLAHE> cv::cuda::createCLAHE(double, cv::Size) { throw_no_cuda(); return cv::Ptr<cv::cuda::CLAHE>(); }
56
void cv::cuda::evenLevels(OutputArray, int, int, int, Stream&) { throw_no_cuda(); }
58
void cv::cuda::histEven(InputArray, OutputArray, int, int, int, Stream&) { throw_no_cuda(); }
59
void cv::cuda::histEven(InputArray, GpuMat*, int*, int*, int*, Stream&) { throw_no_cuda(); }
61
void cv::cuda::histRange(InputArray, OutputArray, InputArray, Stream&) { throw_no_cuda(); }
62
void cv::cuda::histRange(InputArray, GpuMat*, const GpuMat*, Stream&) { throw_no_cuda(); }
64
#else /* !defined (HAVE_CUDA) */
66
////////////////////////////////////////////////////////////////////////
71
void histogram256(PtrStepSzb src, int* hist, cudaStream_t stream);
74
void cv::cuda::calcHist(InputArray _src, OutputArray _hist, Stream& stream)
76
GpuMat src = _src.getGpuMat();
78
CV_Assert( src.type() == CV_8UC1 );
80
_hist.create(1, 256, CV_32SC1);
81
GpuMat hist = _hist.getGpuMat();
83
hist.setTo(Scalar::all(0), stream);
85
hist::histogram256(src, hist.ptr<int>(), StreamAccessor::getStream(stream));
88
////////////////////////////////////////////////////////////////////////
93
void equalizeHist(PtrStepSzb src, PtrStepSzb dst, const int* lut, cudaStream_t stream);
96
void cv::cuda::equalizeHist(InputArray _src, OutputArray _dst, Stream& _stream)
98
GpuMat src = _src.getGpuMat();
100
CV_Assert( src.type() == CV_8UC1 );
102
_dst.create(src.size(), src.type());
103
GpuMat dst = _dst.getGpuMat();
106
nppSafeCall( nppsIntegralGetBufferSize_32s(256, &intBufSize) );
108
size_t bufSize = intBufSize + 2 * 256 * sizeof(int);
110
BufferPool pool(_stream);
111
GpuMat buf = pool.getBuffer(1, static_cast<int>(bufSize), CV_8UC1);
113
GpuMat hist(1, 256, CV_32SC1, buf.data);
114
GpuMat lut(1, 256, CV_32SC1, buf.data + 256 * sizeof(int));
115
GpuMat intBuf(1, intBufSize, CV_8UC1, buf.data + 2 * 256 * sizeof(int));
117
cuda::calcHist(src, hist, _stream);
119
cudaStream_t stream = StreamAccessor::getStream(_stream);
120
NppStreamHandler h(stream);
122
nppSafeCall( nppsIntegral_32s(hist.ptr<Npp32s>(), lut.ptr<Npp32s>(), 256, intBuf.ptr<Npp8u>()) );
124
hist::equalizeHist(src, dst, lut.ptr<int>(), stream);
127
////////////////////////////////////////////////////////////////////////
132
void calcLut(PtrStepSzb src, PtrStepb lut, int tilesX, int tilesY, int2 tileSize, int clipLimit, float lutScale, cudaStream_t stream);
133
void transform(PtrStepSzb src, PtrStepSzb dst, PtrStepb lut, int tilesX, int tilesY, int2 tileSize, cudaStream_t stream);
138
class CLAHE_Impl : public cv::cuda::CLAHE
141
CLAHE_Impl(double clipLimit = 40.0, int tilesX = 8, int tilesY = 8);
143
void apply(cv::InputArray src, cv::OutputArray dst);
144
void apply(InputArray src, OutputArray dst, Stream& stream);
146
void setClipLimit(double clipLimit);
147
double getClipLimit() const;
149
void setTilesGridSize(cv::Size tileGridSize);
150
cv::Size getTilesGridSize() const;
152
void collectGarbage();
163
CLAHE_Impl::CLAHE_Impl(double clipLimit, int tilesX, int tilesY) :
164
clipLimit_(clipLimit), tilesX_(tilesX), tilesY_(tilesY)
168
void CLAHE_Impl::apply(cv::InputArray _src, cv::OutputArray _dst)
170
apply(_src, _dst, Stream::Null());
173
void CLAHE_Impl::apply(InputArray _src, OutputArray _dst, Stream& s)
175
GpuMat src = _src.getGpuMat();
177
CV_Assert( src.type() == CV_8UC1 );
179
_dst.create( src.size(), src.type() );
180
GpuMat dst = _dst.getGpuMat();
182
const int histSize = 256;
184
ensureSizeIsEnough(tilesX_ * tilesY_, histSize, CV_8UC1, lut_);
186
cudaStream_t stream = StreamAccessor::getStream(s);
191
if (src.cols % tilesX_ == 0 && src.rows % tilesY_ == 0)
193
tileSize = cv::Size(src.cols / tilesX_, src.rows / tilesY_);
198
#ifndef HAVE_OPENCV_CUDAARITHM
201
cv::cuda::copyMakeBorder(src, srcExt_, 0, tilesY_ - (src.rows % tilesY_), 0, tilesX_ - (src.cols % tilesX_), cv::BORDER_REFLECT_101, cv::Scalar(), s);
204
tileSize = cv::Size(srcExt_.cols / tilesX_, srcExt_.rows / tilesY_);
208
const int tileSizeTotal = tileSize.area();
209
const float lutScale = static_cast<float>(histSize - 1) / tileSizeTotal;
212
if (clipLimit_ > 0.0)
214
clipLimit = static_cast<int>(clipLimit_ * tileSizeTotal / histSize);
215
clipLimit = std::max(clipLimit, 1);
218
clahe::calcLut(srcForLut, lut_, tilesX_, tilesY_, make_int2(tileSize.width, tileSize.height), clipLimit, lutScale, stream);
220
clahe::transform(src, dst, lut_, tilesX_, tilesY_, make_int2(tileSize.width, tileSize.height), stream);
223
void CLAHE_Impl::setClipLimit(double clipLimit)
225
clipLimit_ = clipLimit;
228
double CLAHE_Impl::getClipLimit() const
233
void CLAHE_Impl::setTilesGridSize(cv::Size tileGridSize)
235
tilesX_ = tileGridSize.width;
236
tilesY_ = tileGridSize.height;
239
cv::Size CLAHE_Impl::getTilesGridSize() const
241
return cv::Size(tilesX_, tilesY_);
244
void CLAHE_Impl::collectGarbage()
251
cv::Ptr<cv::cuda::CLAHE> cv::cuda::createCLAHE(double clipLimit, cv::Size tileGridSize)
253
return makePtr<CLAHE_Impl>(clipLimit, tileGridSize.width, tileGridSize.height);
256
////////////////////////////////////////////////////////////////////////
261
typedef NppStatus (*get_buf_size_c1_t)(NppiSize oSizeROI, int nLevels, int* hpBufferSize);
262
typedef NppStatus (*get_buf_size_c4_t)(NppiSize oSizeROI, int nLevels[], int* hpBufferSize);
264
template<int SDEPTH> struct NppHistogramEvenFuncC1
266
typedef typename NPPTypeTraits<SDEPTH>::npp_type src_t;
268
typedef NppStatus (*func_ptr)(const src_t* pSrc, int nSrcStep, NppiSize oSizeROI, Npp32s * pHist,
269
int nLevels, Npp32s nLowerLevel, Npp32s nUpperLevel, Npp8u * pBuffer);
271
template<int SDEPTH> struct NppHistogramEvenFuncC4
273
typedef typename NPPTypeTraits<SDEPTH>::npp_type src_t;
275
typedef NppStatus (*func_ptr)(const src_t* pSrc, int nSrcStep, NppiSize oSizeROI,
276
Npp32s * pHist[4], int nLevels[4], Npp32s nLowerLevel[4], Npp32s nUpperLevel[4], Npp8u * pBuffer);
279
template<int SDEPTH, typename NppHistogramEvenFuncC1<SDEPTH>::func_ptr func, get_buf_size_c1_t get_buf_size>
280
struct NppHistogramEvenC1
282
typedef typename NppHistogramEvenFuncC1<SDEPTH>::src_t src_t;
284
static void hist(const GpuMat& src, OutputArray _hist, int histSize, int lowerLevel, int upperLevel, Stream& stream)
286
const int levels = histSize + 1;
288
_hist.create(1, histSize, CV_32S);
289
GpuMat hist = _hist.getGpuMat();
293
sz.height = src.rows;
296
get_buf_size(sz, levels, &buf_size);
298
BufferPool pool(stream);
299
GpuMat buf = pool.getBuffer(1, buf_size, CV_8UC1);
301
NppStreamHandler h(stream);
303
nppSafeCall( func(src.ptr<src_t>(), static_cast<int>(src.step), sz, hist.ptr<Npp32s>(), levels,
304
lowerLevel, upperLevel, buf.ptr<Npp8u>()) );
307
cudaSafeCall( cudaDeviceSynchronize() );
310
template<int SDEPTH, typename NppHistogramEvenFuncC4<SDEPTH>::func_ptr func, get_buf_size_c4_t get_buf_size>
311
struct NppHistogramEvenC4
313
typedef typename NppHistogramEvenFuncC4<SDEPTH>::src_t src_t;
315
static void hist(const GpuMat& src, GpuMat hist[4], int histSize[4], int lowerLevel[4], int upperLevel[4], Stream& stream)
317
int levels[] = {histSize[0] + 1, histSize[1] + 1, histSize[2] + 1, histSize[3] + 1};
318
hist[0].create(1, histSize[0], CV_32S);
319
hist[1].create(1, histSize[1], CV_32S);
320
hist[2].create(1, histSize[2], CV_32S);
321
hist[3].create(1, histSize[3], CV_32S);
325
sz.height = src.rows;
327
Npp32s* pHist[] = {hist[0].ptr<Npp32s>(), hist[1].ptr<Npp32s>(), hist[2].ptr<Npp32s>(), hist[3].ptr<Npp32s>()};
330
get_buf_size(sz, levels, &buf_size);
332
BufferPool pool(stream);
333
GpuMat buf = pool.getBuffer(1, buf_size, CV_8UC1);
335
NppStreamHandler h(stream);
337
nppSafeCall( func(src.ptr<src_t>(), static_cast<int>(src.step), sz, pHist, levels, lowerLevel, upperLevel, buf.ptr<Npp8u>()) );
340
cudaSafeCall( cudaDeviceSynchronize() );
344
template<int SDEPTH> struct NppHistogramRangeFuncC1
346
typedef typename NPPTypeTraits<SDEPTH>::npp_type src_t;
347
typedef Npp32s level_t;
348
enum {LEVEL_TYPE_CODE=CV_32SC1};
350
typedef NppStatus (*func_ptr)(const src_t* pSrc, int nSrcStep, NppiSize oSizeROI, Npp32s* pHist,
351
const Npp32s* pLevels, int nLevels, Npp8u* pBuffer);
353
template<> struct NppHistogramRangeFuncC1<CV_32F>
355
typedef Npp32f src_t;
356
typedef Npp32f level_t;
357
enum {LEVEL_TYPE_CODE=CV_32FC1};
359
typedef NppStatus (*func_ptr)(const Npp32f* pSrc, int nSrcStep, NppiSize oSizeROI, Npp32s* pHist,
360
const Npp32f* pLevels, int nLevels, Npp8u* pBuffer);
362
template<int SDEPTH> struct NppHistogramRangeFuncC4
364
typedef typename NPPTypeTraits<SDEPTH>::npp_type src_t;
365
typedef Npp32s level_t;
366
enum {LEVEL_TYPE_CODE=CV_32SC1};
368
typedef NppStatus (*func_ptr)(const src_t* pSrc, int nSrcStep, NppiSize oSizeROI, Npp32s* pHist[4],
369
const Npp32s* pLevels[4], int nLevels[4], Npp8u* pBuffer);
371
template<> struct NppHistogramRangeFuncC4<CV_32F>
373
typedef Npp32f src_t;
374
typedef Npp32f level_t;
375
enum {LEVEL_TYPE_CODE=CV_32FC1};
377
typedef NppStatus (*func_ptr)(const Npp32f* pSrc, int nSrcStep, NppiSize oSizeROI, Npp32s* pHist[4],
378
const Npp32f* pLevels[4], int nLevels[4], Npp8u* pBuffer);
381
template<int SDEPTH, typename NppHistogramRangeFuncC1<SDEPTH>::func_ptr func, get_buf_size_c1_t get_buf_size>
382
struct NppHistogramRangeC1
384
typedef typename NppHistogramRangeFuncC1<SDEPTH>::src_t src_t;
385
typedef typename NppHistogramRangeFuncC1<SDEPTH>::level_t level_t;
386
enum {LEVEL_TYPE_CODE=NppHistogramRangeFuncC1<SDEPTH>::LEVEL_TYPE_CODE};
388
static void hist(const GpuMat& src, OutputArray _hist, const GpuMat& levels, Stream& stream)
390
CV_Assert( levels.type() == LEVEL_TYPE_CODE && levels.rows == 1 );
392
_hist.create(1, levels.cols - 1, CV_32S);
393
GpuMat hist = _hist.getGpuMat();
397
sz.height = src.rows;
400
get_buf_size(sz, levels.cols, &buf_size);
402
BufferPool pool(stream);
403
GpuMat buf = pool.getBuffer(1, buf_size, CV_8UC1);
405
NppStreamHandler h(stream);
407
nppSafeCall( func(src.ptr<src_t>(), static_cast<int>(src.step), sz, hist.ptr<Npp32s>(), levels.ptr<level_t>(), levels.cols, buf.ptr<Npp8u>()) );
410
cudaSafeCall( cudaDeviceSynchronize() );
413
template<int SDEPTH, typename NppHistogramRangeFuncC4<SDEPTH>::func_ptr func, get_buf_size_c4_t get_buf_size>
414
struct NppHistogramRangeC4
416
typedef typename NppHistogramRangeFuncC4<SDEPTH>::src_t src_t;
417
typedef typename NppHistogramRangeFuncC1<SDEPTH>::level_t level_t;
418
enum {LEVEL_TYPE_CODE=NppHistogramRangeFuncC1<SDEPTH>::LEVEL_TYPE_CODE};
420
static void hist(const GpuMat& src, GpuMat hist[4], const GpuMat levels[4], Stream& stream)
422
CV_Assert( levels[0].type() == LEVEL_TYPE_CODE && levels[0].rows == 1 );
423
CV_Assert( levels[1].type() == LEVEL_TYPE_CODE && levels[1].rows == 1 );
424
CV_Assert( levels[2].type() == LEVEL_TYPE_CODE && levels[2].rows == 1 );
425
CV_Assert( levels[3].type() == LEVEL_TYPE_CODE && levels[3].rows == 1 );
427
hist[0].create(1, levels[0].cols - 1, CV_32S);
428
hist[1].create(1, levels[1].cols - 1, CV_32S);
429
hist[2].create(1, levels[2].cols - 1, CV_32S);
430
hist[3].create(1, levels[3].cols - 1, CV_32S);
432
Npp32s* pHist[] = {hist[0].ptr<Npp32s>(), hist[1].ptr<Npp32s>(), hist[2].ptr<Npp32s>(), hist[3].ptr<Npp32s>()};
433
int nLevels[] = {levels[0].cols, levels[1].cols, levels[2].cols, levels[3].cols};
434
const level_t* pLevels[] = {levels[0].ptr<level_t>(), levels[1].ptr<level_t>(), levels[2].ptr<level_t>(), levels[3].ptr<level_t>()};
438
sz.height = src.rows;
441
get_buf_size(sz, nLevels, &buf_size);
443
BufferPool pool(stream);
444
GpuMat buf = pool.getBuffer(1, buf_size, CV_8UC1);
446
NppStreamHandler h(stream);
448
nppSafeCall( func(src.ptr<src_t>(), static_cast<int>(src.step), sz, pHist, pLevels, nLevels, buf.ptr<Npp8u>()) );
451
cudaSafeCall( cudaDeviceSynchronize() );
456
void cv::cuda::evenLevels(OutputArray _levels, int nLevels, int lowerLevel, int upperLevel, Stream& stream)
458
const int kind = _levels.kind();
460
_levels.create(1, nLevels, CV_32SC1);
463
if (kind == _InputArray::CUDA_GPU_MAT)
464
host_levels.create(1, nLevels, CV_32SC1);
466
host_levels = _levels.getMat();
468
nppSafeCall( nppiEvenLevelsHost_32s(host_levels.ptr<Npp32s>(), nLevels, lowerLevel, upperLevel) );
470
if (kind == _InputArray::CUDA_GPU_MAT)
471
_levels.getGpuMatRef().upload(host_levels, stream);
476
void histEven8u(PtrStepSzb src, int* hist, int binCount, int lowerLevel, int upperLevel, cudaStream_t stream);
481
void histEven8u(const GpuMat& src, GpuMat& hist, int histSize, int lowerLevel, int upperLevel, cudaStream_t stream)
483
hist.create(1, histSize, CV_32S);
484
cudaSafeCall( cudaMemsetAsync(hist.data, 0, histSize * sizeof(int), stream) );
485
hist::histEven8u(src, hist.ptr<int>(), histSize, lowerLevel, upperLevel, stream);
489
void cv::cuda::histEven(InputArray _src, OutputArray hist, int histSize, int lowerLevel, int upperLevel, Stream& stream)
491
typedef void (*hist_t)(const GpuMat& src, OutputArray hist, int levels, int lowerLevel, int upperLevel, Stream& stream);
492
static const hist_t hist_callers[] =
494
NppHistogramEvenC1<CV_8U , nppiHistogramEven_8u_C1R , nppiHistogramEvenGetBufferSize_8u_C1R >::hist,
496
NppHistogramEvenC1<CV_16U, nppiHistogramEven_16u_C1R, nppiHistogramEvenGetBufferSize_16u_C1R>::hist,
497
NppHistogramEvenC1<CV_16S, nppiHistogramEven_16s_C1R, nppiHistogramEvenGetBufferSize_16s_C1R>::hist
500
GpuMat src = _src.getGpuMat();
502
if (src.depth() == CV_8U && deviceSupports(FEATURE_SET_COMPUTE_30))
504
histEven8u(src, hist.getGpuMatRef(), histSize, lowerLevel, upperLevel, StreamAccessor::getStream(stream));
508
CV_Assert( src.type() == CV_8UC1 || src.type() == CV_16UC1 || src.type() == CV_16SC1 );
510
hist_callers[src.depth()](src, hist, histSize, lowerLevel, upperLevel, stream);
513
void cv::cuda::histEven(InputArray _src, GpuMat hist[4], int histSize[4], int lowerLevel[4], int upperLevel[4], Stream& stream)
515
typedef void (*hist_t)(const GpuMat& src, GpuMat hist[4], int levels[4], int lowerLevel[4], int upperLevel[4], Stream& stream);
516
static const hist_t hist_callers[] =
518
NppHistogramEvenC4<CV_8U , nppiHistogramEven_8u_C4R , nppiHistogramEvenGetBufferSize_8u_C4R >::hist,
520
NppHistogramEvenC4<CV_16U, nppiHistogramEven_16u_C4R, nppiHistogramEvenGetBufferSize_16u_C4R>::hist,
521
NppHistogramEvenC4<CV_16S, nppiHistogramEven_16s_C4R, nppiHistogramEvenGetBufferSize_16s_C4R>::hist
524
GpuMat src = _src.getGpuMat();
526
CV_Assert( src.type() == CV_8UC4 || src.type() == CV_16UC4 || src.type() == CV_16SC4 );
528
hist_callers[src.depth()](src, hist, histSize, lowerLevel, upperLevel, stream);
531
void cv::cuda::histRange(InputArray _src, OutputArray hist, InputArray _levels, Stream& stream)
533
typedef void (*hist_t)(const GpuMat& src, OutputArray hist, const GpuMat& levels, Stream& stream);
534
static const hist_t hist_callers[] =
536
NppHistogramRangeC1<CV_8U , nppiHistogramRange_8u_C1R , nppiHistogramRangeGetBufferSize_8u_C1R >::hist,
538
NppHistogramRangeC1<CV_16U, nppiHistogramRange_16u_C1R, nppiHistogramRangeGetBufferSize_16u_C1R>::hist,
539
NppHistogramRangeC1<CV_16S, nppiHistogramRange_16s_C1R, nppiHistogramRangeGetBufferSize_16s_C1R>::hist,
541
NppHistogramRangeC1<CV_32F, nppiHistogramRange_32f_C1R, nppiHistogramRangeGetBufferSize_32f_C1R>::hist
544
GpuMat src = _src.getGpuMat();
545
GpuMat levels = _levels.getGpuMat();
547
CV_Assert( src.type() == CV_8UC1 || src.type() == CV_16UC1 || src.type() == CV_16SC1 || src.type() == CV_32FC1 );
549
hist_callers[src.depth()](src, hist, levels, stream);
552
void cv::cuda::histRange(InputArray _src, GpuMat hist[4], const GpuMat levels[4], Stream& stream)
554
typedef void (*hist_t)(const GpuMat& src, GpuMat hist[4], const GpuMat levels[4], Stream& stream);
555
static const hist_t hist_callers[] =
557
NppHistogramRangeC4<CV_8U , nppiHistogramRange_8u_C4R , nppiHistogramRangeGetBufferSize_8u_C4R >::hist,
559
NppHistogramRangeC4<CV_16U, nppiHistogramRange_16u_C4R, nppiHistogramRangeGetBufferSize_16u_C4R>::hist,
560
NppHistogramRangeC4<CV_16S, nppiHistogramRange_16s_C4R, nppiHistogramRangeGetBufferSize_16s_C4R>::hist,
562
NppHistogramRangeC4<CV_32F, nppiHistogramRange_32f_C4R, nppiHistogramRangeGetBufferSize_32f_C4R>::hist
565
GpuMat src = _src.getGpuMat();
567
CV_Assert( src.type() == CV_8UC4 || src.type() == CV_16UC4 || src.type() == CV_16SC4 || src.type() == CV_32FC4 );
569
hist_callers[src.depth()](src, hist, levels, stream);
572
#endif /* !defined (HAVE_CUDA) */