2012-09-17 09:48:34 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
|
|
|
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other GpuMaterials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or bpied warranties, including, but not limited to, the bpied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
2012-09-24 20:28:35 +08:00
|
|
|
#include "mcwutil.hpp"
|
2012-09-17 09:48:34 +08:00
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
using namespace cv::ocl;
|
|
|
|
|
|
|
|
#if !defined (HAVE_OPENCL)
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
void cv::ocl::PyrLKOpticalFlow::sparse(const oclMat &, const oclMat &, const oclMat &, oclMat &, oclMat &, oclMat *) { }
|
|
|
|
void cv::ocl::PyrLKOpticalFlow::dense(const oclMat &, const oclMat &, oclMat &, oclMat &, oclMat *) { }
|
2012-09-17 09:48:34 +08:00
|
|
|
|
|
|
|
#else /* !defined (HAVE_OPENCL) */
|
|
|
|
|
|
|
|
namespace cv
|
|
|
|
{
|
|
|
|
namespace ocl
|
|
|
|
{
|
|
|
|
///////////////////////////OpenCL kernel strings///////////////////////////
|
|
|
|
extern const char *pyrlk;
|
2012-09-24 20:28:35 +08:00
|
|
|
extern const char *operator_setTo;
|
|
|
|
extern const char *operator_convertTo;
|
2012-10-22 15:14:22 +08:00
|
|
|
extern const char *operator_copyToM;
|
2012-09-24 20:28:35 +08:00
|
|
|
extern const char *arithm_mul;
|
|
|
|
extern const char *pyr_down;
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dim3
|
|
|
|
{
|
|
|
|
unsigned int x, y, z;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct float2
|
|
|
|
{
|
|
|
|
float x, y;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct int2
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
void calcPatchSize(cv::Size winSize, int cn, dim3 &block, dim3 &patch, bool isDeviceArch11)
|
2012-09-17 09:48:34 +08:00
|
|
|
{
|
|
|
|
winSize.width *= cn;
|
|
|
|
|
|
|
|
if (winSize.width > 32 && winSize.width > 2 * winSize.height)
|
|
|
|
{
|
|
|
|
block.x = isDeviceArch11 ? 16 : 32;
|
|
|
|
block.y = 8;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
block.x = 16;
|
|
|
|
block.y = isDeviceArch11 ? 8 : 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
patch.x = (winSize.width + block.x - 1) / block.x;
|
|
|
|
patch.y = (winSize.height + block.y - 1) / block.y;
|
|
|
|
|
|
|
|
block.z = patch.z = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-24 20:28:35 +08:00
|
|
|
inline int divUp(int total, int grain)
|
2012-09-17 09:48:34 +08:00
|
|
|
{
|
2012-09-24 20:28:35 +08:00
|
|
|
return (total + grain - 1) / grain;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////// ConvertTo ////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
void convert_run_cus(const oclMat &src, oclMat &dst, double alpha, double beta)
|
|
|
|
{
|
|
|
|
string kernelName = "convert_to_S";
|
|
|
|
stringstream idxStr;
|
|
|
|
idxStr << src.depth();
|
|
|
|
kernelName += idxStr.str();
|
|
|
|
float alpha_f = (float)alpha, beta_f = (float)beta;
|
|
|
|
CV_DbgAssert(src.rows == dst.rows && src.cols == dst.cols);
|
|
|
|
vector<pair<size_t , const void *> > args;
|
|
|
|
size_t localThreads[3] = {16, 16, 1};
|
|
|
|
size_t globalThreads[3];
|
|
|
|
globalThreads[0] = (dst.cols + localThreads[0] - 1) / localThreads[0] * localThreads[0];
|
|
|
|
globalThreads[1] = (dst.rows + localThreads[1] - 1) / localThreads[1] * localThreads[1];
|
|
|
|
globalThreads[2] = 1;
|
|
|
|
int dststep_in_pixel = dst.step / dst.elemSize(), dstoffset_in_pixel = dst.offset / dst.elemSize();
|
|
|
|
int srcstep_in_pixel = src.step / src.elemSize(), srcoffset_in_pixel = src.offset / src.elemSize();
|
|
|
|
if(dst.type() == CV_8UC1)
|
2012-09-17 09:48:34 +08:00
|
|
|
{
|
2012-09-24 20:28:35 +08:00
|
|
|
globalThreads[0] = ((dst.cols + 4) / 4 + localThreads[0]) / localThreads[0] * localThreads[0];
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
2012-09-24 20:28:35 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&src.rows ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&srcstep_in_pixel ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&srcoffset_in_pixel ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&dststep_in_pixel ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&dstoffset_in_pixel ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_float) , (void *)&alpha_f ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_float) , (void *)&beta_f ));
|
|
|
|
openCLExecuteKernel2(dst.clCxt , &operator_convertTo, kernelName, globalThreads,
|
2012-10-11 16:22:47 +08:00
|
|
|
localThreads, args, dst.oclchannels(), dst.depth(), CLFLUSH);
|
2012-09-24 20:28:35 +08:00
|
|
|
}
|
|
|
|
void convertTo( const oclMat &src, oclMat &m, int rtype, double alpha = 1, double beta = 0 );
|
|
|
|
void convertTo( const oclMat &src, oclMat &dst, int rtype, double alpha, double beta )
|
|
|
|
{
|
|
|
|
//cout << "cv::ocl::oclMat::convertTo()" << endl;
|
|
|
|
|
|
|
|
bool noScale = fabs(alpha - 1) < std::numeric_limits<double>::epsilon()
|
|
|
|
&& fabs(beta) < std::numeric_limits<double>::epsilon();
|
|
|
|
|
|
|
|
if( rtype < 0 )
|
|
|
|
rtype = src.type();
|
|
|
|
else
|
2012-10-11 16:22:47 +08:00
|
|
|
rtype = CV_MAKETYPE(CV_MAT_DEPTH(rtype), src.oclchannels());
|
2012-09-17 09:48:34 +08:00
|
|
|
|
2012-09-24 20:28:35 +08:00
|
|
|
int sdepth = src.depth(), ddepth = CV_MAT_DEPTH(rtype);
|
|
|
|
if( sdepth == ddepth && noScale )
|
|
|
|
{
|
|
|
|
src.copyTo(dst);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
oclMat temp;
|
|
|
|
const oclMat *psrc = &src;
|
|
|
|
if( sdepth != ddepth && psrc == &dst )
|
|
|
|
psrc = &(temp = src);
|
|
|
|
|
|
|
|
dst.create( src.size(), rtype );
|
|
|
|
convert_run_cus(*psrc, dst, alpha, beta);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////// setTo ////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
//oclMat &operator = (const Scalar &s)
|
|
|
|
//{
|
|
|
|
// //cout << "cv::ocl::oclMat::=" << endl;
|
|
|
|
// setTo(s);
|
|
|
|
// return *this;
|
|
|
|
//}
|
|
|
|
void set_to_withoutmask_run_cus(const oclMat &dst, const Scalar &scalar, string kernelName)
|
2012-09-17 09:48:34 +08:00
|
|
|
{
|
2012-09-24 20:28:35 +08:00
|
|
|
vector<pair<size_t , const void *> > args;
|
|
|
|
|
|
|
|
size_t localThreads[3] = {16, 16, 1};
|
|
|
|
size_t globalThreads[3];
|
|
|
|
globalThreads[0] = (dst.cols + localThreads[0] - 1) / localThreads[0] * localThreads[0];
|
|
|
|
globalThreads[1] = (dst.rows + localThreads[1] - 1) / localThreads[1] * localThreads[1];
|
|
|
|
globalThreads[2] = 1;
|
|
|
|
int step_in_pixel = dst.step / dst.elemSize(), offset_in_pixel = dst.offset / dst.elemSize();
|
|
|
|
if(dst.type() == CV_8UC1)
|
|
|
|
{
|
|
|
|
globalThreads[0] = ((dst.cols + 4) / 4 + localThreads[0] - 1) / localThreads[0] * localThreads[0];
|
|
|
|
}
|
2012-10-11 16:22:47 +08:00
|
|
|
char compile_option[32];
|
|
|
|
union sc
|
|
|
|
{
|
|
|
|
cl_uchar4 uval;
|
|
|
|
cl_char4 cval;
|
|
|
|
cl_ushort4 usval;
|
|
|
|
cl_short4 shval;
|
|
|
|
cl_int4 ival;
|
|
|
|
cl_float4 fval;
|
|
|
|
cl_double4 dval;
|
|
|
|
} val;
|
2012-09-24 20:28:35 +08:00
|
|
|
switch(dst.depth())
|
|
|
|
{
|
|
|
|
case 0:
|
2012-10-11 16:22:47 +08:00
|
|
|
val.uval.s[0] = saturate_cast<uchar>(scalar.val[0]);
|
|
|
|
val.uval.s[1] = saturate_cast<uchar>(scalar.val[1]);
|
|
|
|
val.uval.s[2] = saturate_cast<uchar>(scalar.val[2]);
|
|
|
|
val.uval.s[3] = saturate_cast<uchar>(scalar.val[3]);
|
|
|
|
switch(dst.oclchannels())
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
sprintf(compile_option, "-D GENTYPE=uchar");
|
|
|
|
args.push_back( make_pair( sizeof(cl_uchar) , (void *)&val.uval.s[0] ));
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
sprintf(compile_option, "-D GENTYPE=uchar4");
|
|
|
|
args.push_back( make_pair( sizeof(cl_uchar4) , (void *)&val.uval ));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
|
|
|
|
}
|
2012-09-24 20:28:35 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
2012-10-11 16:22:47 +08:00
|
|
|
val.cval.s[0] = saturate_cast<char>(scalar.val[0]);
|
|
|
|
val.cval.s[1] = saturate_cast<char>(scalar.val[1]);
|
|
|
|
val.cval.s[2] = saturate_cast<char>(scalar.val[2]);
|
|
|
|
val.cval.s[3] = saturate_cast<char>(scalar.val[3]);
|
|
|
|
switch(dst.oclchannels())
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
sprintf(compile_option, "-D GENTYPE=char");
|
|
|
|
args.push_back( make_pair( sizeof(cl_char) , (void *)&val.cval.s[0] ));
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
sprintf(compile_option, "-D GENTYPE=char4");
|
|
|
|
args.push_back( make_pair( sizeof(cl_char4) , (void *)&val.cval ));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
|
|
|
|
}
|
2012-09-24 20:28:35 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
2012-10-11 16:22:47 +08:00
|
|
|
val.usval.s[0] = saturate_cast<ushort>(scalar.val[0]);
|
|
|
|
val.usval.s[1] = saturate_cast<ushort>(scalar.val[1]);
|
|
|
|
val.usval.s[2] = saturate_cast<ushort>(scalar.val[2]);
|
|
|
|
val.usval.s[3] = saturate_cast<ushort>(scalar.val[3]);
|
|
|
|
switch(dst.oclchannels())
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
sprintf(compile_option, "-D GENTYPE=ushort");
|
|
|
|
args.push_back( make_pair( sizeof(cl_ushort) , (void *)&val.usval.s[0] ));
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
sprintf(compile_option, "-D GENTYPE=ushort4");
|
|
|
|
args.push_back( make_pair( sizeof(cl_ushort4) , (void *)&val.usval ));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
|
|
|
|
}
|
2012-09-24 20:28:35 +08:00
|
|
|
break;
|
|
|
|
case 3:
|
2012-10-11 16:22:47 +08:00
|
|
|
val.shval.s[0] = saturate_cast<short>(scalar.val[0]);
|
|
|
|
val.shval.s[1] = saturate_cast<short>(scalar.val[1]);
|
|
|
|
val.shval.s[2] = saturate_cast<short>(scalar.val[2]);
|
|
|
|
val.shval.s[3] = saturate_cast<short>(scalar.val[3]);
|
|
|
|
switch(dst.oclchannels())
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
sprintf(compile_option, "-D GENTYPE=short");
|
|
|
|
args.push_back( make_pair( sizeof(cl_short) , (void *)&val.shval.s[0] ));
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
sprintf(compile_option, "-D GENTYPE=short4");
|
|
|
|
args.push_back( make_pair( sizeof(cl_short4) , (void *)&val.shval ));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
|
|
|
|
}
|
2012-09-24 20:28:35 +08:00
|
|
|
break;
|
|
|
|
case 4:
|
2012-10-11 16:22:47 +08:00
|
|
|
val.ival.s[0] = saturate_cast<int>(scalar.val[0]);
|
|
|
|
val.ival.s[1] = saturate_cast<int>(scalar.val[1]);
|
|
|
|
val.ival.s[2] = saturate_cast<int>(scalar.val[2]);
|
|
|
|
val.ival.s[3] = saturate_cast<int>(scalar.val[3]);
|
|
|
|
switch(dst.oclchannels())
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
sprintf(compile_option, "-D GENTYPE=int");
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&val.ival.s[0] ));
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
sprintf(compile_option, "-D GENTYPE=int2");
|
|
|
|
cl_int2 i2val;
|
|
|
|
i2val.s[0] = val.ival.s[0];
|
|
|
|
i2val.s[1] = val.ival.s[1];
|
|
|
|
args.push_back( make_pair( sizeof(cl_int2) , (void *)&i2val ));
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
sprintf(compile_option, "-D GENTYPE=int4");
|
|
|
|
args.push_back( make_pair( sizeof(cl_int4) , (void *)&val.ival ));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
|
|
|
|
}
|
2012-09-24 20:28:35 +08:00
|
|
|
break;
|
|
|
|
case 5:
|
2012-10-11 16:22:47 +08:00
|
|
|
val.fval.s[0] = (float)scalar.val[0];
|
|
|
|
val.fval.s[1] = (float)scalar.val[1];
|
|
|
|
val.fval.s[2] = (float)scalar.val[2];
|
|
|
|
val.fval.s[3] = (float)scalar.val[3];
|
|
|
|
switch(dst.oclchannels())
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
sprintf(compile_option, "-D GENTYPE=float");
|
|
|
|
args.push_back( make_pair( sizeof(cl_float) , (void *)&val.fval.s[0] ));
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
sprintf(compile_option, "-D GENTYPE=float4");
|
|
|
|
args.push_back( make_pair( sizeof(cl_float4) , (void *)&val.fval ));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
|
|
|
|
}
|
2012-09-24 20:28:35 +08:00
|
|
|
break;
|
|
|
|
case 6:
|
2012-10-11 16:22:47 +08:00
|
|
|
val.dval.s[0] = scalar.val[0];
|
|
|
|
val.dval.s[1] = scalar.val[1];
|
|
|
|
val.dval.s[2] = scalar.val[2];
|
|
|
|
val.dval.s[3] = scalar.val[3];
|
|
|
|
switch(dst.oclchannels())
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
sprintf(compile_option, "-D GENTYPE=double");
|
|
|
|
args.push_back( make_pair( sizeof(cl_double) , (void *)&val.dval.s[0] ));
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
sprintf(compile_option, "-D GENTYPE=double4");
|
|
|
|
args.push_back( make_pair( sizeof(cl_double4) , (void *)&val.dval ));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
|
|
|
|
}
|
2012-09-24 20:28:35 +08:00
|
|
|
break;
|
2012-10-11 16:22:47 +08:00
|
|
|
default:
|
|
|
|
CV_Error(CV_StsUnsupportedFormat, "unknown depth");
|
2012-09-24 20:28:35 +08:00
|
|
|
}
|
|
|
|
#if CL_VERSION_1_2
|
2012-10-11 16:22:47 +08:00
|
|
|
if(dst.offset == 0 && dst.cols == dst.wholecols)
|
|
|
|
{
|
|
|
|
clEnqueueFillBuffer(dst.clCxt->impl->clCmdQueue, (cl_mem)dst.data, args[0].second, args[0].first, 0, dst.step * dst.rows, 0, NULL, NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.rows ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&step_in_pixel ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&offset_in_pixel));
|
2012-09-24 20:28:35 +08:00
|
|
|
openCLExecuteKernel2(dst.clCxt , &operator_setTo, kernelName, globalThreads,
|
2012-10-11 16:22:47 +08:00
|
|
|
localThreads, args, -1, -1, compile_option, CLFLUSH);
|
|
|
|
}
|
2012-09-24 20:28:35 +08:00
|
|
|
#else
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.rows ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&step_in_pixel ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&offset_in_pixel));
|
|
|
|
openCLExecuteKernel2(dst.clCxt , &operator_setTo, kernelName, globalThreads,
|
2012-10-11 16:22:47 +08:00
|
|
|
localThreads, args, -1, -1, compile_option, CLFLUSH);
|
2012-09-24 20:28:35 +08:00
|
|
|
#endif
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
|
|
|
|
2012-09-24 20:28:35 +08:00
|
|
|
oclMat &setTo(oclMat &src, const Scalar &scalar)
|
2012-09-17 09:48:34 +08:00
|
|
|
{
|
2012-09-24 20:28:35 +08:00
|
|
|
CV_Assert( src.depth() >= 0 && src.depth() <= 6 );
|
|
|
|
CV_DbgAssert( !src.empty());
|
2012-09-17 09:48:34 +08:00
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
if(src.type() == CV_8UC1)
|
|
|
|
{
|
|
|
|
set_to_withoutmask_run_cus(src, scalar, "set_to_without_mask_C1_D0");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
set_to_withoutmask_run_cus(src, scalar, "set_to_without_mask");
|
|
|
|
}
|
2012-09-24 20:28:35 +08:00
|
|
|
|
|
|
|
return src;
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
|
|
|
|
2012-10-22 15:14:22 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////// CopyTo /////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
void copy_to_with_mask_cus(const oclMat &src, oclMat &dst, const oclMat &mask, string kernelName)
|
|
|
|
{
|
|
|
|
CV_DbgAssert( dst.rows == mask.rows && dst.cols == mask.cols &&
|
|
|
|
src.rows == dst.rows && src.cols == dst.cols
|
|
|
|
&& mask.type() == CV_8UC1);
|
|
|
|
|
|
|
|
vector<pair<size_t , const void *> > args;
|
|
|
|
|
|
|
|
std::string string_types[4][7] = {{"uchar", "char", "ushort", "short", "int", "float", "double"},
|
|
|
|
{"uchar2", "char2", "ushort2", "short2", "int2", "float2", "double2"},
|
|
|
|
{"uchar3", "char3", "ushort3", "short3", "int3", "float3", "double3"},
|
|
|
|
{"uchar4", "char4", "ushort4", "short4", "int4", "float4", "double4"}
|
|
|
|
};
|
|
|
|
char compile_option[32];
|
|
|
|
sprintf(compile_option, "-D GENTYPE=%s", string_types[dst.oclchannels() - 1][dst.depth()].c_str());
|
|
|
|
size_t localThreads[3] = {16, 16, 1};
|
|
|
|
size_t globalThreads[3];
|
|
|
|
|
|
|
|
globalThreads[0] = divUp(dst.cols, localThreads[0]) * localThreads[0];
|
|
|
|
globalThreads[1] = divUp(dst.rows, localThreads[1]) * localThreads[1];
|
|
|
|
globalThreads[2] = 1;
|
|
|
|
|
|
|
|
int dststep_in_pixel = dst.step / dst.elemSize(), dstoffset_in_pixel = dst.offset / dst.elemSize();
|
|
|
|
int srcstep_in_pixel = src.step / src.elemSize(), srcoffset_in_pixel = src.offset / src.elemSize();
|
|
|
|
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem) , (void *)&mask.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&src.rows ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&srcstep_in_pixel ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&srcoffset_in_pixel ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&dststep_in_pixel ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&dstoffset_in_pixel ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&mask.step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&mask.offset ));
|
|
|
|
|
|
|
|
openCLExecuteKernel2(dst.clCxt , &operator_copyToM, kernelName, globalThreads,
|
|
|
|
localThreads, args, -1, -1, compile_option, CLFLUSH);
|
|
|
|
}
|
|
|
|
|
|
|
|
void copyTo(const oclMat &src, oclMat &m )
|
|
|
|
{
|
|
|
|
CV_DbgAssert(!src.empty());
|
|
|
|
m.create(src.size(), src.type());
|
|
|
|
openCLCopyBuffer2D(src.clCxt, m.data, m.step, m.offset,
|
|
|
|
src.data, src.step, src.cols * src.elemSize(), src.rows, src.offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void copyTo(const oclMat &src, oclMat &mat, const oclMat &mask)
|
|
|
|
{
|
|
|
|
if (mask.empty())
|
|
|
|
{
|
|
|
|
copyTo(src, mat);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mat.create(src.size(), src.type());
|
|
|
|
copy_to_with_mask_cus(src, mat, mask, "copy_to_with_mask");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-24 20:28:35 +08:00
|
|
|
void arithmetic_run(const oclMat &src1, oclMat &dst, string kernelName, const char **kernelString, void *_scalar)
|
2012-09-17 09:48:34 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
if(src1.clCxt -> impl -> double_support == 0 && src1.type() == CV_64F)
|
2012-09-17 09:48:34 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
CV_Error(CV_GpuNotSupported, "Selected device don't support double\r\n");
|
2012-09-17 09:48:34 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-24 20:28:35 +08:00
|
|
|
//dst.create(src1.size(), src1.type());
|
|
|
|
//CV_Assert(src1.cols == src2.cols && src2.cols == dst.cols &&
|
|
|
|
// src1.rows == src2.rows && src2.rows == dst.rows);
|
2012-10-11 16:22:47 +08:00
|
|
|
CV_Assert(src1.cols == dst.cols &&
|
2012-09-24 20:28:35 +08:00
|
|
|
src1.rows == dst.rows);
|
2012-09-17 09:48:34 +08:00
|
|
|
|
2012-09-24 20:28:35 +08:00
|
|
|
CV_Assert(src1.type() == dst.type());
|
|
|
|
CV_Assert(src1.depth() != CV_8S);
|
2012-09-17 09:48:34 +08:00
|
|
|
|
2012-09-24 20:28:35 +08:00
|
|
|
Context *clCxt = src1.clCxt;
|
|
|
|
//int channels = dst.channels();
|
|
|
|
//int depth = dst.depth();
|
2012-09-17 09:48:34 +08:00
|
|
|
|
2012-09-24 20:28:35 +08:00
|
|
|
//int vector_lengths[4][7] = {{4, 0, 4, 4, 1, 1, 1},
|
|
|
|
// {4, 0, 4, 4, 1, 1, 1},
|
|
|
|
// {4, 0, 4, 4, 1, 1, 1},
|
|
|
|
// {4, 0, 4, 4, 1, 1, 1}
|
|
|
|
//};
|
|
|
|
|
|
|
|
//size_t vector_length = vector_lengths[channels-1][depth];
|
|
|
|
//int offset_cols = (dst.offset / dst.elemSize1()) & (vector_length - 1);
|
|
|
|
//int cols = divUp(dst.cols * channels + offset_cols, vector_length);
|
|
|
|
|
|
|
|
size_t localThreads[3] = { 16, 16, 1 };
|
2012-10-11 16:22:47 +08:00
|
|
|
//size_t globalThreads[3] = { divUp(cols, localThreads[0]) * localThreads[0],
|
|
|
|
// divUp(dst.rows, localThreads[1]) * localThreads[1],
|
|
|
|
// 1
|
|
|
|
// };
|
|
|
|
size_t globalThreads[3] = { src1.cols,
|
2012-09-24 20:28:35 +08:00
|
|
|
src1.rows,
|
|
|
|
1
|
|
|
|
};
|
|
|
|
|
|
|
|
int dst_step1 = dst.cols * dst.elemSize();
|
|
|
|
vector<pair<size_t , const void *> > args;
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src1.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1.step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1.offset ));
|
|
|
|
//args.push_back( make_pair( sizeof(cl_mem), (void *)&src2.data ));
|
|
|
|
//args.push_back( make_pair( sizeof(cl_int), (void *)&src2.step ));
|
|
|
|
//args.push_back( make_pair( sizeof(cl_int), (void *)&src2.offset ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst.step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst.offset ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1.rows ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1.cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst_step1 ));
|
|
|
|
|
|
|
|
//if(_scalar != NULL)
|
|
|
|
//{
|
2012-10-11 16:22:47 +08:00
|
|
|
float scalar1 = *((float *)_scalar);
|
|
|
|
args.push_back( make_pair( sizeof(float), (float *)&scalar1 ));
|
2012-09-24 20:28:35 +08:00
|
|
|
//}
|
|
|
|
|
|
|
|
openCLExecuteKernel2(clCxt, kernelString, kernelName, globalThreads, localThreads, args, -1, src1.depth(), CLFLUSH);
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
|
|
|
|
2012-09-24 20:28:35 +08:00
|
|
|
void multiply_cus(const oclMat &src1, oclMat &dst, float scalar)
|
2012-09-17 09:48:34 +08:00
|
|
|
{
|
2012-09-24 20:28:35 +08:00
|
|
|
arithmetic_run(src1, dst, "arithm_muls", &pyrlk, (void *)(&scalar));
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
|
|
|
|
2012-09-24 20:28:35 +08:00
|
|
|
void pyrdown_run_cus(const oclMat &src, const oclMat &dst)
|
|
|
|
{
|
|
|
|
|
|
|
|
CV_Assert(src.type() == dst.type());
|
|
|
|
CV_Assert(src.depth() != CV_8S);
|
|
|
|
|
|
|
|
Context *clCxt = src.clCxt;
|
|
|
|
|
|
|
|
string kernelName = "pyrDown";
|
|
|
|
|
|
|
|
size_t localThreads[3] = { 256, 1, 1 };
|
|
|
|
size_t globalThreads[3] = { src.cols, dst.rows, 1};
|
|
|
|
|
|
|
|
vector<pair<size_t , const void *> > args;
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src.step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src.rows));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src.cols));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst.step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst.cols));
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
openCLExecuteKernel2(clCxt, &pyr_down, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth(), CLFLUSH);
|
2012-09-24 20:28:35 +08:00
|
|
|
}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
void pyrDown_cus(const oclMat &src, oclMat &dst)
|
2012-09-24 20:28:35 +08:00
|
|
|
{
|
|
|
|
CV_Assert(src.depth() <= CV_32F && src.channels() <= 4);
|
|
|
|
|
|
|
|
dst.create((src.rows + 1) / 2, (src.cols + 1) / 2, src.type());
|
|
|
|
|
|
|
|
pyrdown_run_cus(src, dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//struct MultiplyScalar
|
|
|
|
//{
|
|
|
|
// MultiplyScalar(double val_, double scale_) : val(val_), scale(scale_) {}
|
|
|
|
// double operator ()(double a) const
|
|
|
|
// {
|
|
|
|
// return (scale * a * val);
|
|
|
|
// }
|
|
|
|
// const double val;
|
|
|
|
// const double scale;
|
|
|
|
//};
|
|
|
|
//
|
|
|
|
//void callF(const oclMat& src, oclMat& dst, MultiplyScalar op, int mask)
|
|
|
|
//{
|
2012-10-26 17:43:59 +08:00
|
|
|
// Mat srcTemp;
|
|
|
|
// Mat dstTemp;
|
|
|
|
// src.download(srcTemp);
|
|
|
|
// dst.download(dstTemp);
|
2012-09-24 20:28:35 +08:00
|
|
|
//
|
2012-10-26 17:43:59 +08:00
|
|
|
// int i;
|
|
|
|
// int j;
|
|
|
|
// int k;
|
|
|
|
// for(i = 0; i < srcTemp.rows; i++)
|
|
|
|
// {
|
|
|
|
// for(j = 0; j < srcTemp.cols; j++)
|
|
|
|
// {
|
|
|
|
// for(k = 0; k < srcTemp.channels(); k++)
|
|
|
|
// {
|
|
|
|
// ((float*)dstTemp.data)[srcTemp.channels() * (i * srcTemp.rows + j) + k] = (float)op(((float*)srcTemp.data)[srcTemp.channels() * (i * srcTemp.rows + j) + k]);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2012-09-24 20:28:35 +08:00
|
|
|
//
|
2012-10-26 17:43:59 +08:00
|
|
|
// dst = dstTemp;
|
2012-09-24 20:28:35 +08:00
|
|
|
//}
|
|
|
|
//
|
|
|
|
//static inline bool isAligned(const unsigned char* ptr, size_t size)
|
|
|
|
//{
|
|
|
|
// return reinterpret_cast<size_t>(ptr) % size == 0;
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//static inline bool isAligned(size_t step, size_t size)
|
|
|
|
//{
|
|
|
|
// return step % size == 0;
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//void callT(const oclMat& src, oclMat& dst, MultiplyScalar op, int mask)
|
|
|
|
//{
|
2012-10-11 16:22:47 +08:00
|
|
|
// if (!isAligned(src.data, 4 * sizeof(double)) || !isAligned(src.step, 4 * sizeof(double)) ||
|
2012-09-24 20:28:35 +08:00
|
|
|
// !isAligned(dst.data, 4 * sizeof(double)) || !isAligned(dst.step, 4 * sizeof(double)))
|
|
|
|
// {
|
|
|
|
// callF(src, dst, op, mask);
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
//
|
2012-10-26 17:43:59 +08:00
|
|
|
// Mat srcTemp;
|
|
|
|
// Mat dstTemp;
|
|
|
|
// src.download(srcTemp);
|
|
|
|
// dst.download(dstTemp);
|
2012-09-24 20:28:35 +08:00
|
|
|
//
|
2012-10-26 17:43:59 +08:00
|
|
|
// int x_shifted;
|
2012-09-24 20:28:35 +08:00
|
|
|
//
|
2012-10-26 17:43:59 +08:00
|
|
|
// int i;
|
|
|
|
// int j;
|
|
|
|
// for(i = 0; i < srcTemp.rows; i++)
|
|
|
|
// {
|
|
|
|
// const double* srcRow = (const double*)srcTemp.data + i * srcTemp.rows;
|
2012-09-24 20:28:35 +08:00
|
|
|
// double* dstRow = (double*)dstTemp.data + i * dstTemp.rows;;
|
|
|
|
//
|
2012-10-26 17:43:59 +08:00
|
|
|
// for(j = 0; j < srcTemp.cols; j++)
|
|
|
|
// {
|
|
|
|
// x_shifted = j * 4;
|
2012-09-24 20:28:35 +08:00
|
|
|
//
|
2012-10-26 17:43:59 +08:00
|
|
|
// if(x_shifted + 4 - 1 < srcTemp.cols)
|
|
|
|
// {
|
|
|
|
// dstRow[x_shifted ] = op(srcRow[x_shifted ]);
|
|
|
|
// dstRow[x_shifted + 1] = op(srcRow[x_shifted + 1]);
|
|
|
|
// dstRow[x_shifted + 2] = op(srcRow[x_shifted + 2]);
|
|
|
|
// dstRow[x_shifted + 3] = op(srcRow[x_shifted + 3]);
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// for (int real_x = x_shifted; real_x < srcTemp.cols; ++real_x)
|
|
|
|
// {
|
|
|
|
// ((float*)dstTemp.data)[i * srcTemp.rows + real_x] = op(((float*)srcTemp.data)[i * srcTemp.rows + real_x]);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2012-09-24 20:28:35 +08:00
|
|
|
//}
|
|
|
|
//
|
|
|
|
//void multiply(const oclMat& src1, double val, oclMat& dst, double scale = 1.0f);
|
|
|
|
//void multiply(const oclMat& src1, double val, oclMat& dst, double scale)
|
|
|
|
//{
|
|
|
|
// MultiplyScalar op(val, scale);
|
2012-10-26 17:43:59 +08:00
|
|
|
// //if(src1.channels() == 1 && dst.channels() == 1)
|
|
|
|
// //{
|
|
|
|
// // callT(src1, dst, op, 0);
|
|
|
|
// //}
|
|
|
|
// //else
|
|
|
|
// //{
|
|
|
|
// callF(src1, dst, op, 0);
|
|
|
|
// //}
|
2012-09-24 20:28:35 +08:00
|
|
|
//}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
cl_mem bindTexture(const oclMat &mat, int depth, int channels)
|
2012-09-17 09:48:34 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
cl_mem texture;
|
2012-09-17 09:48:34 +08:00
|
|
|
cl_image_format format;
|
|
|
|
int err;
|
2012-10-11 16:22:47 +08:00
|
|
|
if(depth == 0)
|
|
|
|
{
|
|
|
|
format.image_channel_data_type = CL_UNSIGNED_INT8;
|
|
|
|
}
|
|
|
|
else if(depth == 5)
|
|
|
|
{
|
|
|
|
format.image_channel_data_type = CL_FLOAT;
|
|
|
|
}
|
|
|
|
if(channels == 1)
|
|
|
|
{
|
|
|
|
format.image_channel_order = CL_R;
|
|
|
|
}
|
|
|
|
else if(channels == 3)
|
|
|
|
{
|
|
|
|
format.image_channel_order = CL_RGB;
|
|
|
|
}
|
|
|
|
else if(channels == 4)
|
|
|
|
{
|
|
|
|
format.image_channel_order = CL_RGBA;
|
|
|
|
}
|
2012-09-17 09:48:34 +08:00
|
|
|
#if CL_VERSION_1_2
|
|
|
|
cl_image_desc desc;
|
|
|
|
desc.image_type = CL_MEM_OBJECT_IMAGE2D;
|
2012-10-11 16:22:47 +08:00
|
|
|
desc.image_width = mat.step / mat.elemSize();
|
2012-09-17 09:48:34 +08:00
|
|
|
desc.image_height = mat.rows;
|
|
|
|
desc.image_depth = NULL;
|
|
|
|
desc.image_array_size = 1;
|
|
|
|
desc.image_row_pitch = 0;
|
2012-10-11 16:22:47 +08:00
|
|
|
desc.image_slice_pitch = 0;
|
2012-09-17 09:48:34 +08:00
|
|
|
desc.buffer = NULL;
|
|
|
|
desc.num_mip_levels = 0;
|
|
|
|
desc.num_samples = 0;
|
2012-10-11 16:22:47 +08:00
|
|
|
texture = clCreateImage(mat.clCxt->impl->clContext, CL_MEM_READ_WRITE, &format, &desc, NULL, &err);
|
2012-09-17 09:48:34 +08:00
|
|
|
#else
|
|
|
|
texture = clCreateImage2D(
|
2012-10-11 16:22:47 +08:00
|
|
|
mat.clCxt->impl->clContext,
|
|
|
|
CL_MEM_READ_WRITE,
|
|
|
|
&format,
|
|
|
|
mat.step / mat.elemSize(),
|
|
|
|
mat.rows,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
&err);
|
2012-09-17 09:48:34 +08:00
|
|
|
#endif
|
2012-10-11 16:22:47 +08:00
|
|
|
size_t origin[] = { 0, 0, 0 };
|
|
|
|
size_t region[] = { mat.step / mat.elemSize(), mat.rows, 1 };
|
|
|
|
clEnqueueCopyBufferToImage(mat.clCxt->impl->clCmdQueue, (cl_mem)mat.data, texture, 0, origin, region, 0, NULL, 0);
|
2012-09-17 09:48:34 +08:00
|
|
|
openCLSafeCall(err);
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
return texture;
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
|
|
|
|
2012-09-24 20:28:35 +08:00
|
|
|
void releaseTexture(cl_mem texture)
|
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
openCLFree(texture);
|
2012-09-24 20:28:35 +08:00
|
|
|
}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
void lkSparse_run(oclMat &I, oclMat &J,
|
|
|
|
const oclMat &prevPts, oclMat &nextPts, oclMat &status, oclMat *err, bool GET_MIN_EIGENVALS, int ptcount,
|
|
|
|
int level, /*dim3 block, */dim3 patch, Size winSize, int iters)
|
2012-09-17 09:48:34 +08:00
|
|
|
{
|
|
|
|
Context *clCxt = I.clCxt;
|
|
|
|
|
|
|
|
string kernelName = "lkSparse";
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
size_t localThreads[3] = { 8, 32, 1 };
|
2012-09-24 20:28:35 +08:00
|
|
|
size_t globalThreads[3] = { 8 * ptcount, 32, 1};
|
2012-09-17 09:48:34 +08:00
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
int cn = I.oclchannels();
|
2012-09-17 09:48:34 +08:00
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
bool calcErr;
|
2012-09-17 09:48:34 +08:00
|
|
|
if (err)
|
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
calcErr = true;
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
calcErr = false;
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
2012-10-11 16:22:47 +08:00
|
|
|
calcErr = true;
|
2012-09-17 09:48:34 +08:00
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
cl_mem ITex = bindTexture(I, I.depth(), cn);
|
|
|
|
cl_mem JTex = bindTexture(J, J.depth(), cn);
|
2012-09-17 09:48:34 +08:00
|
|
|
|
|
|
|
vector<pair<size_t , const void *> > args;
|
|
|
|
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&ITex ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&JTex ));
|
|
|
|
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&prevPts.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&prevPts.step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&nextPts.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&nextPts.step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&status.data ));
|
|
|
|
//args.push_back( make_pair( sizeof(cl_mem), (void *)&(err->data) ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&level ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&I.rows ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&I.cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&patch.x ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&patch.y ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&cn ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&winSize.width ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&winSize.height ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&iters ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_char), (void *)&calcErr ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_char), (void *)&GET_MIN_EIGENVALS ));
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
openCLExecuteKernel2(clCxt, &pyrlk, kernelName, globalThreads, localThreads, args, I.oclchannels(), I.depth(), CLFLUSH);
|
|
|
|
|
|
|
|
releaseTexture(ITex);
|
|
|
|
releaseTexture(JTex);
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
void cv::ocl::PyrLKOpticalFlow::sparse(const oclMat &prevImg, const oclMat &nextImg, const oclMat &prevPts, oclMat &nextPts, oclMat &status, oclMat *err)
|
2012-09-17 09:48:34 +08:00
|
|
|
{
|
2012-10-26 17:43:59 +08:00
|
|
|
if (prevImg.clCxt->impl->devName.find("Intel(R) HD Graphics") != string::npos)
|
|
|
|
{
|
|
|
|
cout << " Intel HD GPU device unsupported " << endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-17 09:48:34 +08:00
|
|
|
if (prevPts.empty())
|
|
|
|
{
|
|
|
|
nextPts.release();
|
|
|
|
status.release();
|
|
|
|
if (err) err->release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
derivLambda = std::min(std::max(derivLambda, 0.0), 1.0);
|
|
|
|
|
|
|
|
iters = std::min(std::max(iters, 0), 100);
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
const int cn = prevImg.oclchannels();
|
2012-09-17 09:48:34 +08:00
|
|
|
|
|
|
|
dim3 block, patch;
|
2012-10-11 16:22:47 +08:00
|
|
|
calcPatchSize(winSize, cn, block, patch, isDeviceArch11_);
|
2012-09-17 09:48:34 +08:00
|
|
|
|
|
|
|
CV_Assert(derivLambda >= 0);
|
|
|
|
CV_Assert(maxLevel >= 0 && winSize.width > 2 && winSize.height > 2);
|
|
|
|
CV_Assert(prevImg.size() == nextImg.size() && prevImg.type() == nextImg.type());
|
|
|
|
CV_Assert(patch.x > 0 && patch.x < 6 && patch.y > 0 && patch.y < 6);
|
|
|
|
CV_Assert(prevPts.rows == 1 && prevPts.type() == CV_32FC2);
|
|
|
|
|
|
|
|
if (useInitialFlow)
|
|
|
|
CV_Assert(nextPts.size() == prevPts.size() && nextPts.type() == CV_32FC2);
|
|
|
|
else
|
|
|
|
ensureSizeIsEnough(1, prevPts.cols, prevPts.type(), nextPts);
|
|
|
|
|
|
|
|
oclMat temp1 = (useInitialFlow ? nextPts : prevPts).reshape(1);
|
|
|
|
oclMat temp2 = nextPts.reshape(1);
|
2012-10-11 16:22:47 +08:00
|
|
|
//oclMat scalar(temp1.rows, temp1.cols, temp1.type(), Scalar(1.0f / (1 << maxLevel) / 2.0f));
|
|
|
|
multiply_cus(temp1, temp2, 1.0f / (1 << maxLevel) / 2.0f);
|
|
|
|
//::multiply(temp1, 1.0f / (1 << maxLevel) / 2.0f, temp2);
|
2012-09-17 09:48:34 +08:00
|
|
|
|
|
|
|
ensureSizeIsEnough(1, prevPts.cols, CV_8UC1, status);
|
2012-09-24 20:28:35 +08:00
|
|
|
//status.setTo(Scalar::all(1));
|
|
|
|
setTo(status, Scalar::all(1));
|
2012-09-17 09:48:34 +08:00
|
|
|
|
2012-09-24 20:28:35 +08:00
|
|
|
//if (err)
|
|
|
|
// ensureSizeIsEnough(1, prevPts.cols, CV_32FC1, *err);
|
2012-09-17 09:48:34 +08:00
|
|
|
|
|
|
|
// build the image pyramids.
|
|
|
|
|
|
|
|
prevPyr_.resize(maxLevel + 1);
|
|
|
|
nextPyr_.resize(maxLevel + 1);
|
|
|
|
|
|
|
|
if (cn == 1 || cn == 4)
|
|
|
|
{
|
2012-09-24 20:28:35 +08:00
|
|
|
//prevImg.convertTo(prevPyr_[0], CV_32F);
|
|
|
|
//nextImg.convertTo(nextPyr_[0], CV_32F);
|
|
|
|
convertTo(prevImg, prevPyr_[0], CV_32F);
|
|
|
|
convertTo(nextImg, nextPyr_[0], CV_32F);
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
//oclMat buf_;
|
|
|
|
// cvtColor(prevImg, buf_, COLOR_BGR2BGRA);
|
|
|
|
// buf_.convertTo(prevPyr_[0], CV_32F);
|
2012-09-17 09:48:34 +08:00
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
// cvtColor(nextImg, buf_, COLOR_BGR2BGRA);
|
|
|
|
// buf_.convertTo(nextPyr_[0], CV_32F);
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int level = 1; level <= maxLevel; ++level)
|
|
|
|
{
|
2012-09-24 20:28:35 +08:00
|
|
|
pyrDown_cus(prevPyr_[level - 1], prevPyr_[level]);
|
|
|
|
pyrDown_cus(nextPyr_[level - 1], nextPyr_[level]);
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// dI/dx ~ Ix, dI/dy ~ Iy
|
|
|
|
|
|
|
|
for (int level = maxLevel; level >= 0; level--)
|
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
lkSparse_run(prevPyr_[level], nextPyr_[level],
|
|
|
|
prevPts, nextPts, status, level == 0 && err ? err : 0, getMinEigenVals, prevPts.cols,
|
|
|
|
level, /*block, */patch, winSize, iters);
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
2012-09-24 20:28:35 +08:00
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
clFinish(prevImg.clCxt->impl->clCmdQueue);
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
void lkDense_run(oclMat &I, oclMat &J, oclMat &u, oclMat &v,
|
|
|
|
oclMat &prevU, oclMat &prevV, oclMat *err, Size winSize, int iters)
|
2012-09-17 09:48:34 +08:00
|
|
|
{
|
|
|
|
Context *clCxt = I.clCxt;
|
|
|
|
|
|
|
|
string kernelName = "lkDense";
|
|
|
|
|
|
|
|
size_t localThreads[3] = { 16, 16, 1 };
|
|
|
|
size_t globalThreads[3] = { I.cols, I.rows, 1};
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
int cn = I.oclchannels();
|
2012-09-17 09:48:34 +08:00
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
bool calcErr;
|
2012-09-17 09:48:34 +08:00
|
|
|
if (err)
|
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
calcErr = true;
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
calcErr = false;
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
cl_mem ITex = bindTexture(I, I.depth(), cn);
|
|
|
|
cl_mem JTex = bindTexture(J, J.depth(), cn);
|
2012-09-17 09:48:34 +08:00
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
//int2 halfWin = {(winSize.width - 1) / 2, (winSize.height - 1) / 2};
|
2012-09-24 20:28:35 +08:00
|
|
|
//const int patchWidth = 16 + 2 * halfWin.x;
|
|
|
|
//const int patchHeight = 16 + 2 * halfWin.y;
|
|
|
|
//size_t smem_size = 3 * patchWidth * patchHeight * sizeof(int);
|
2012-09-17 09:48:34 +08:00
|
|
|
|
|
|
|
vector<pair<size_t , const void *> > args;
|
|
|
|
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&ITex ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&JTex ));
|
|
|
|
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&u.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&u.step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&v.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&v.step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&prevU.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&prevU.step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&prevV.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&prevV.step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&I.rows ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&I.cols ));
|
|
|
|
//args.push_back( make_pair( sizeof(cl_mem), (void *)&(*err).data ));
|
|
|
|
//args.push_back( make_pair( sizeof(cl_int), (void *)&(*err).step ));
|
2012-10-11 16:22:47 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&winSize.width ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&winSize.height ));
|
2012-09-17 09:48:34 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&iters ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_char), (void *)&calcErr ));
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
openCLExecuteKernel2(clCxt, &pyrlk, kernelName, globalThreads, localThreads, args, I.oclchannels(), I.depth(), CLFLUSH);
|
|
|
|
|
|
|
|
releaseTexture(ITex);
|
|
|
|
releaseTexture(JTex);
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
void cv::ocl::PyrLKOpticalFlow::dense(const oclMat &prevImg, const oclMat &nextImg, oclMat &u, oclMat &v, oclMat *err)
|
2012-09-17 09:48:34 +08:00
|
|
|
{
|
|
|
|
CV_Assert(prevImg.type() == CV_8UC1);
|
|
|
|
CV_Assert(prevImg.size() == nextImg.size() && prevImg.type() == nextImg.type());
|
|
|
|
CV_Assert(maxLevel >= 0);
|
|
|
|
CV_Assert(winSize.width > 2 && winSize.height > 2);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
err->create(prevImg.size(), CV_32FC1);
|
|
|
|
|
|
|
|
prevPyr_.resize(maxLevel + 1);
|
|
|
|
nextPyr_.resize(maxLevel + 1);
|
|
|
|
|
|
|
|
prevPyr_[0] = prevImg;
|
2012-10-22 15:14:22 +08:00
|
|
|
//nextImg.convertTo(nextPyr_[0], CV_32F);
|
|
|
|
convertTo(nextImg, nextPyr_[0], CV_32F);
|
2012-09-17 09:48:34 +08:00
|
|
|
|
|
|
|
for (int level = 1; level <= maxLevel; ++level)
|
|
|
|
{
|
2012-10-22 15:14:22 +08:00
|
|
|
pyrDown_cus(prevPyr_[level - 1], prevPyr_[level]);
|
|
|
|
pyrDown_cus(nextPyr_[level - 1], nextPyr_[level]);
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ensureSizeIsEnough(prevImg.size(), CV_32FC1, uPyr_[0]);
|
|
|
|
ensureSizeIsEnough(prevImg.size(), CV_32FC1, vPyr_[0]);
|
|
|
|
ensureSizeIsEnough(prevImg.size(), CV_32FC1, uPyr_[1]);
|
|
|
|
ensureSizeIsEnough(prevImg.size(), CV_32FC1, vPyr_[1]);
|
2012-10-22 15:14:22 +08:00
|
|
|
//uPyr_[1].setTo(Scalar::all(0));
|
|
|
|
//vPyr_[1].setTo(Scalar::all(0));
|
|
|
|
setTo(uPyr_[1], Scalar::all(0));
|
|
|
|
setTo(vPyr_[1], Scalar::all(0));
|
2012-09-17 09:48:34 +08:00
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
Size winSize2i(winSize.width, winSize.height);
|
2012-09-17 09:48:34 +08:00
|
|
|
|
|
|
|
int idx = 0;
|
|
|
|
|
|
|
|
for (int level = maxLevel; level >= 0; level--)
|
|
|
|
{
|
|
|
|
int idx2 = (idx + 1) & 1;
|
|
|
|
|
|
|
|
lkDense_run(prevPyr_[level], nextPyr_[level], uPyr_[idx], vPyr_[idx], uPyr_[idx2], vPyr_[idx2],
|
2012-10-11 16:22:47 +08:00
|
|
|
level == 0 ? err : 0, winSize2i, iters);
|
2012-09-17 09:48:34 +08:00
|
|
|
|
|
|
|
if (level > 0)
|
|
|
|
idx = idx2;
|
|
|
|
}
|
|
|
|
|
2012-10-22 15:14:22 +08:00
|
|
|
//uPyr_[idx].copyTo(u);
|
|
|
|
//vPyr_[idx].copyTo(v);
|
|
|
|
copyTo(uPyr_[idx], u);
|
|
|
|
copyTo(vPyr_[idx], v);
|
|
|
|
|
|
|
|
clFinish(prevImg.clCxt->impl->clCmdQueue);
|
2012-09-17 09:48:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !defined (HAVE_CUDA) */
|