Skip to content

Commit

Permalink
Merge pull request opencv#2073 from KonstantinMatskevich:ocl_tapi_clahe
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Pavlenko authored and OpenCV Buildbot committed Dec 30, 2013
2 parents 8151571 + 55634c1 commit 8a456c5
Show file tree
Hide file tree
Showing 4 changed files with 375 additions and 15 deletions.
1 change: 1 addition & 0 deletions modules/core/include/opencv2/core/ocl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ class CV_EXPORTS Kernel
bool runTask(bool sync, const Queue& q=Queue());

size_t workGroupSize() const;
size_t preferedWorkGroupSizeMultiple() const;
bool compileWorkGroupSize(size_t wsz[]) const;
size_t localMemSize() const;

Expand Down
10 changes: 10 additions & 0 deletions modules/core/src/ocl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2817,6 +2817,16 @@ size_t Kernel::workGroupSize() const
sizeof(val), &val, &retsz) >= 0 ? val : 0;
}

size_t Kernel::preferedWorkGroupSizeMultiple() const
{
if(!p)
return 0;
size_t val = 0, retsz = 0;
cl_device_id dev = (cl_device_id)Device::getDefault().ptr();
return clGetKernelWorkGroupInfo(p->handle, dev, CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE,
sizeof(val), &val, &retsz) >= 0 ? val : 0;
}

bool Kernel::compileWorkGroupSize(size_t wsz[]) const
{
if(!p || !wsz)
Expand Down
127 changes: 112 additions & 15 deletions modules/imgproc/src/clahe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,90 @@
//M*/

#include "precomp.hpp"
#include "opencl_kernels.hpp"

// ----------------------------------------------------------------------
// CLAHE

namespace clahe
{
static bool calcLut(cv::InputArray _src, cv::OutputArray _dst,
const int tilesX, const int tilesY, const cv::Size tileSize,
const int clipLimit, const float lutScale)
{
cv::ocl::Kernel _k("calcLut", cv::ocl::imgproc::clahe_oclsrc);

bool is_cpu = cv::ocl::Device::getDefault().type() == cv::ocl::Device::TYPE_CPU;
cv::String opts;
if(is_cpu)
opts = "-D CPU ";
else
opts = cv::format("-D WAVE_SIZE=%d", _k.preferedWorkGroupSizeMultiple());

cv::ocl::Kernel k("calcLut", cv::ocl::imgproc::clahe_oclsrc, opts);
if(k.empty())
return false;

cv::UMat src = _src.getUMat();
_dst.create(tilesX * tilesY, 256, CV_8UC1);
cv::UMat dst = _dst.getUMat();

int tile_size[2];
tile_size[0] = tileSize.width;
tile_size[1] = tileSize.height;

size_t localThreads[3] = { 32, 8, 1 };
size_t globalThreads[3] = { tilesX * localThreads[0], tilesY * localThreads[1], 1 };

int idx = 0;
idx = k.set(idx, cv::ocl::KernelArg::ReadOnlyNoSize(src));
idx = k.set(idx, cv::ocl::KernelArg::WriteOnlyNoSize(dst));
idx = k.set(idx, tile_size);
idx = k.set(idx, tilesX);
idx = k.set(idx, clipLimit);
idx = k.set(idx, lutScale);

if (!k.run(2, globalThreads, localThreads, false))
return false;
return true;
}

static bool transform(const cv::InputArray _src, cv::OutputArray _dst, const cv::InputArray _lut,
const int tilesX, const int tilesY, const cv::Size & tileSize)
{

cv::ocl::Kernel k("transform", cv::ocl::imgproc::clahe_oclsrc);
if(k.empty())
return false;

int tile_size[2];
tile_size[0] = tileSize.width;
tile_size[1] = tileSize.height;

cv::UMat src = _src.getUMat();
_dst.create(src.size(), src.type());
cv::UMat dst = _dst.getUMat();
cv::UMat lut = _lut.getUMat();

size_t localThreads[3] = { 32, 8, 1 };
size_t globalThreads[3] = { src.cols, src.rows, 1 };

int idx = 0;
idx = k.set(idx, cv::ocl::KernelArg::ReadOnlyNoSize(src));
idx = k.set(idx, cv::ocl::KernelArg::WriteOnlyNoSize(dst));
idx = k.set(idx, cv::ocl::KernelArg::ReadOnlyNoSize(lut));
idx = k.set(idx, src.cols);
idx = k.set(idx, src.rows);
idx = k.set(idx, tile_size);
idx = k.set(idx, tilesX);
idx = k.set(idx, tilesY);

if (!k.run(2, globalThreads, localThreads, false))
return false;
return true;
}
}

namespace
{
class CLAHE_CalcLut_Body : public cv::ParallelLoopBody
Expand Down Expand Up @@ -241,7 +321,9 @@ namespace
int tilesY_;

cv::Mat srcExt_;
cv::UMat usrcExt_;
cv::Mat lut_;
cv::UMat ulut_;
};

CLAHE_Impl::CLAHE_Impl(double clipLimit, int tilesX, int tilesY) :
Expand All @@ -256,31 +338,34 @@ namespace

void CLAHE_Impl::apply(cv::InputArray _src, cv::OutputArray _dst)
{
cv::Mat src = _src.getMat();

CV_Assert( src.type() == CV_8UC1 );
CV_Assert( _src.type() == CV_8UC1 );

_dst.create( src.size(), src.type() );
cv::Mat dst = _dst.getMat();
bool useOpenCL = cv::ocl::useOpenCL() && _src.isUMat() && _src.dims()<=2;

const int histSize = 256;

lut_.create(tilesX_ * tilesY_, histSize, CV_8UC1);

cv::Size tileSize;
cv::Mat srcForLut;
cv::_InputArray _srcForLut;

if (src.cols % tilesX_ == 0 && src.rows % tilesY_ == 0)
if (_src.size().width % tilesX_ == 0 && _src.size().height % tilesY_ == 0)
{
tileSize = cv::Size(src.cols / tilesX_, src.rows / tilesY_);
srcForLut = src;
tileSize = cv::Size(_src.size().width / tilesX_, _src.size().height / tilesY_);
_srcForLut = _src;
}
else
{
cv::copyMakeBorder(src, srcExt_, 0, tilesY_ - (src.rows % tilesY_), 0, tilesX_ - (src.cols % tilesX_), cv::BORDER_REFLECT_101);

tileSize = cv::Size(srcExt_.cols / tilesX_, srcExt_.rows / tilesY_);
srcForLut = srcExt_;
if(useOpenCL)
{
cv::copyMakeBorder(_src, usrcExt_, 0, tilesY_ - (_src.size().height % tilesY_), 0, tilesX_ - (_src.size().width % tilesX_), cv::BORDER_REFLECT_101);
tileSize = cv::Size(usrcExt_.size().width / tilesX_, usrcExt_.size().height / tilesY_);
_srcForLut = usrcExt_;
}
else
{
cv::copyMakeBorder(_src, srcExt_, 0, tilesY_ - (_src.size().height % tilesY_), 0, tilesX_ - (_src.size().width % tilesX_), cv::BORDER_REFLECT_101);
tileSize = cv::Size(srcExt_.size().width / tilesX_, srcExt_.size().height / tilesY_);
_srcForLut = srcExt_;
}
}

const int tileSizeTotal = tileSize.area();
Expand All @@ -293,6 +378,16 @@ namespace
clipLimit = std::max(clipLimit, 1);
}

if(useOpenCL && clahe::calcLut(_srcForLut, ulut_, tilesX_, tilesY_, tileSize, clipLimit, lutScale) )
if( clahe::transform(_src, _dst, ulut_, tilesX_, tilesY_, tileSize) )
return;

cv::Mat src = _src.getMat();
_dst.create( src.size(), src.type() );
cv::Mat dst = _dst.getMat();
cv::Mat srcForLut = _srcForLut.getMat();
lut_.create(tilesX_ * tilesY_, histSize, CV_8UC1);

CLAHE_CalcLut_Body calcLutBody(srcForLut, lut_, tileSize, tilesX_, tilesY_, clipLimit, lutScale);
cv::parallel_for_(cv::Range(0, tilesX_ * tilesY_), calcLutBody);

Expand Down Expand Up @@ -325,6 +420,8 @@ namespace
{
srcExt_.release();
lut_.release();
usrcExt_.release();
ulut_.release();
}
}

Expand Down
Loading

0 comments on commit 8a456c5

Please sign in to comment.