Merge pull request #2700 from ilya-lavrenov:tapi_cvtColor

This commit is contained in:
Alexander Alekhin
2014-05-08 17:01:37 +04:00
committed by OpenCV Buildbot
4 changed files with 299 additions and 33 deletions

View File

@@ -2749,12 +2749,13 @@ static bool ocl_cvtColor( InputArray _src, OutputArray _dst, int code, int dcn )
ocl::Device dev = ocl::Device::getDefault();
int pxPerWIy = 1;
if (dev.isIntel() && (dev.type() & ocl::Device::TYPE_GPU))
{
if (dev.isIntel() && (dev.type() & ocl::Device::TYPE_GPU) &&
!(code == CV_BGR2Luv || code == CV_RGB2Luv || code == CV_LBGR2Luv || code == CV_LRGB2Luv ||
code == CV_Luv2BGR || code == CV_Luv2RGB || code == CV_Luv2LBGR || code == CV_Luv2LRGB))
pxPerWIy = 4;
}
globalsize[1] = DIVUP(globalsize[1], pxPerWIy);
opts += format("-D PIX_PER_WI_Y=%d ", pxPerWIy);
opts += format("-D PIX_PER_WI_Y=%d ", pxPerWIy);
switch (code)
{
@@ -3084,16 +3085,20 @@ static bool ocl_cvtColor( InputArray _src, OutputArray _dst, int code, int dcn )
break;
}
case CV_BGR2Lab: case CV_RGB2Lab: case CV_LBGR2Lab: case CV_LRGB2Lab:
case CV_BGR2Luv: case CV_RGB2Luv: case CV_LBGR2Luv: case CV_LRGB2Luv:
{
CV_Assert( (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) );
bidx = code == CV_BGR2Lab || code == CV_LBGR2Lab ? 0 : 2;
bool srgb = code == CV_BGR2Lab || code == CV_RGB2Lab;
bidx = code == CV_BGR2Lab || code == CV_LBGR2Lab || code == CV_BGR2Luv || code == CV_LBGR2Luv ? 0 : 2;
bool srgb = code == CV_BGR2Lab || code == CV_RGB2Lab || code == CV_RGB2Luv || code == CV_BGR2Luv;
bool lab = code == CV_BGR2Lab || code == CV_RGB2Lab || code == CV_LBGR2Lab || code == CV_LRGB2Lab;
float un, vn;
dcn = 3;
k.create("BGR2Lab", ocl::imgproc::cvtcolor_oclsrc,
opts + format("-D dcn=3 -D bidx=%d%s",
bidx, srgb ? " -D SRGB" : ""));
k.create(format("BGR2%s", lab ? "Lab" : "Luv").c_str(),
ocl::imgproc::cvtcolor_oclsrc,
opts + format("-D dcn=%d -D bidx=%d%s",
dcn, bidx, srgb ? " -D SRGB" : ""));
if (k.empty())
return false;
@@ -3105,7 +3110,7 @@ static bool ocl_cvtColor( InputArray _src, OutputArray _dst, int code, int dcn )
ocl::KernelArg srcarg = ocl::KernelArg::ReadOnlyNoSize(src),
dstarg = ocl::KernelArg::WriteOnly(dst);
if (depth == CV_8U)
if (depth == CV_8U && lab)
{
static UMat usRGBGammaTab, ulinearGammaTab, uLabCbrtTab, ucoeffs;
@@ -3148,10 +3153,12 @@ static bool ocl_cvtColor( InputArray _src, OutputArray _dst, int code, int dcn )
}
else
{
static UMat usRGBGammaTab, ucoeffs;
static UMat usRGBGammaTab, ucoeffs, uLabCbrtTab;
if (srgb && usRGBGammaTab.empty())
Mat(1, GAMMA_TAB_SIZE * 4, CV_32FC1, sRGBGammaTab).copyTo(usRGBGammaTab);
if (!lab && uLabCbrtTab.empty())
Mat(1, LAB_CBRT_TAB_SIZE * 4, CV_32FC1, LabCbrtTab).copyTo(uLabCbrtTab);
{
float coeffs[9];
@@ -3161,39 +3168,59 @@ static bool ocl_cvtColor( InputArray _src, OutputArray _dst, int code, int dcn )
for (int i = 0; i < 3; i++)
{
int j = i * 3;
coeffs[j + (bidx ^ 2)] = _coeffs[j] * scale[i];
coeffs[j + 1] = _coeffs[j + 1] * scale[i];
coeffs[j + bidx] = _coeffs[j + 2] * scale[i];
coeffs[j + (bidx ^ 2)] = _coeffs[j] * (lab ? scale[i] : 1);
coeffs[j + 1] = _coeffs[j + 1] * (lab ? scale[i] : 1);
coeffs[j + bidx] = _coeffs[j + 2] * (lab ? scale[i] : 1);
CV_Assert( coeffs[j] >= 0 && coeffs[j + 1] >= 0 && coeffs[j + 2] >= 0 &&
coeffs[j] + coeffs[j + 1] + coeffs[j + 2] < 1.5f*LabCbrtTabScale );
coeffs[j] + coeffs[j + 1] + coeffs[j + 2] < 1.5f*(lab ? LabCbrtTabScale : 1) );
}
float d = 1.f/(_whitept[0] + _whitept[1]*15 + _whitept[2]*3);
un = 13*4*_whitept[0]*d;
vn = 13*9*_whitept[1]*d;
Mat(1, 9, CV_32FC1, coeffs).copyTo(ucoeffs);
}
float _1_3 = 1.0f / 3.0f, _a = 16.0f / 116.0f;
ocl::KernelArg ucoeffsarg = ocl::KernelArg::PtrReadOnly(ucoeffs);
if (srgb)
k.args(srcarg, dstarg, ocl::KernelArg::PtrReadOnly(usRGBGammaTab),
ucoeffsarg, _1_3, _a);
if (lab)
{
if (srgb)
k.args(srcarg, dstarg, ocl::KernelArg::PtrReadOnly(usRGBGammaTab),
ucoeffsarg, _1_3, _a);
else
k.args(srcarg, dstarg, ucoeffsarg, _1_3, _a);
}
else
k.args(srcarg, dstarg, ucoeffsarg, _1_3, _a);
{
ocl::KernelArg LabCbrtTabarg = ocl::KernelArg::PtrReadOnly(uLabCbrtTab);
if (srgb)
k.args(srcarg, dstarg, ocl::KernelArg::PtrReadOnly(usRGBGammaTab),
LabCbrtTabarg, ucoeffsarg, un, vn);
else
k.args(srcarg, dstarg, LabCbrtTabarg, ucoeffsarg, un, vn);
}
}
return k.run(dims, globalsize, NULL, false);
}
case CV_Lab2BGR: case CV_Lab2RGB: case CV_Lab2LBGR: case CV_Lab2LRGB:
case CV_Luv2BGR: case CV_Luv2RGB: case CV_Luv2LBGR: case CV_Luv2LRGB:
{
if( dcn <= 0 )
dcn = 3;
CV_Assert( scn == 3 && (dcn == 3 || dcn == 4) && (depth == CV_8U || depth == CV_32F) );
bidx = code == CV_Lab2BGR || code == CV_Lab2LBGR ? 0 : 2;
bool srgb = code == CV_Lab2BGR || code == CV_Lab2RGB;
bidx = code == CV_Lab2BGR || code == CV_Lab2LBGR || code == CV_Luv2BGR || code == CV_Luv2LBGR ? 0 : 2;
bool srgb = code == CV_Lab2BGR || code == CV_Lab2RGB || code == CV_Luv2BGR || code == CV_Luv2RGB;
bool lab = code == CV_Lab2BGR || code == CV_Lab2RGB || code == CV_Lab2LBGR || code == CV_Lab2LRGB;
float un, vn;
k.create("Lab2BGR", ocl::imgproc::cvtcolor_oclsrc,
k.create(format("%s2BGR", lab ? "Lab" : "Luv").c_str(),
ocl::imgproc::cvtcolor_oclsrc,
opts + format("-D dcn=%d -D bidx=%d%s",
dcn, bidx, srgb ? " -D SRGB" : ""));
if (k.empty())
@@ -3211,11 +3238,15 @@ static bool ocl_cvtColor( InputArray _src, OutputArray _dst, int code, int dcn )
for( int i = 0; i < 3; i++ )
{
coeffs[i+(bidx^2)*3] = _coeffs[i]*_whitept[i];
coeffs[i+3] = _coeffs[i+3]*_whitept[i];
coeffs[i+bidx*3] = _coeffs[i+6]*_whitept[i];
coeffs[i+(bidx^2)*3] = _coeffs[i] * (lab ? _whitept[i] : 1);
coeffs[i+3] = _coeffs[i+3] * (lab ? _whitept[i] : 1);
coeffs[i+bidx*3] = _coeffs[i+6] * (lab ? _whitept[i] : 1);
}
float d = 1.f/(_whitept[0] + _whitept[1]*15 + _whitept[2]*3);
un = 4*_whitept[0]*d;
vn = 9*_whitept[1]*d;
Mat(1, 9, CV_32FC1, coeffs).copyTo(ucoeffs);
}
@@ -3229,11 +3260,22 @@ static bool ocl_cvtColor( InputArray _src, OutputArray _dst, int code, int dcn )
dstarg = ocl::KernelArg::WriteOnly(dst),
coeffsarg = ocl::KernelArg::PtrReadOnly(ucoeffs);
if (srgb)
k.args(srcarg, dstarg, ocl::KernelArg::PtrReadOnly(usRGBInvGammaTab),
coeffsarg, lThresh, fThresh);
if (lab)
{
if (srgb)
k.args(srcarg, dstarg, ocl::KernelArg::PtrReadOnly(usRGBInvGammaTab),
coeffsarg, lThresh, fThresh);
else
k.args(srcarg, dstarg, coeffsarg, lThresh, fThresh);
}
else
k.args(srcarg, dstarg, coeffsarg, lThresh, fThresh);
{
if (srgb)
k.args(srcarg, dstarg, ocl::KernelArg::PtrReadOnly(usRGBInvGammaTab),
coeffsarg, un, vn);
else
k.args(srcarg, dstarg, coeffsarg, un, vn);
}
return k.run(dims, globalsize, NULL, false);
}
@@ -3264,7 +3306,7 @@ void cv::cvtColor( InputArray _src, OutputArray _dst, int code, int dcn )
int stype = _src.type();
int scn = CV_MAT_CN(stype), depth = CV_MAT_DEPTH(stype), bidx;
CV_OCL_RUN( _src.dims() <= 2 && _dst.isUMat(),
CV_OCL_RUN( _src.dims() <= 2 && _dst.isUMat() && !(depth == CV_8U && (code == CV_Luv2BGR || code == CV_Luv2RGB)),
ocl_cvtColor(_src, _dst, code, dcn) )
Mat src = _src.getMat(), dst;