2012-07-17 01:08:14 +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) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
|
|
|
|
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
|
2013-02-08 11:41:46 +08:00
|
|
|
// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
|
2012-07-17 01:08:14 +08:00
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// @Authors
|
|
|
|
// Niko Li, newlife20080214@gmail.com
|
|
|
|
// Jia Haipeng, jiahaipeng95@gmail.com
|
|
|
|
// Shengen Yan, yanshengen@gmail.com
|
|
|
|
// Jiang Liyuan, jlyuan001.good@163.com
|
|
|
|
// Rock Li, Rock.Li@amd.com
|
|
|
|
// Zailong Wu, bullet@yeah.net
|
2013-05-17 15:34:22 +08:00
|
|
|
// Peng Xiao, pengxiao@outlook.com
|
2012-07-17 01:08:14 +08:00
|
|
|
//
|
|
|
|
// 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
|
2013-10-25 22:00:46 +08:00
|
|
|
// and/or other materials provided with the distribution.
|
2012-07-17 01:08:14 +08:00
|
|
|
//
|
|
|
|
// * 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 implied warranties, including, but not limited to, the implied
|
|
|
|
// 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"
|
2013-09-20 23:19:52 +08:00
|
|
|
#include "opencl_kernels.hpp"
|
2012-07-17 01:08:14 +08:00
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace cv::ocl;
|
|
|
|
|
2013-10-12 04:58:58 +08:00
|
|
|
static std::vector<uchar> scalarToVector(const cv::Scalar & sc, int depth, int ocn, int cn)
|
|
|
|
{
|
|
|
|
CV_Assert(ocn == cn || (ocn == 4 && cn == 3));
|
|
|
|
|
|
|
|
static const int sizeMap[] = { sizeof(uchar), sizeof(char), sizeof(ushort),
|
|
|
|
sizeof(short), sizeof(int), sizeof(float), sizeof(double) };
|
|
|
|
|
|
|
|
int elemSize1 = sizeMap[depth];
|
|
|
|
int bufSize = elemSize1 * ocn;
|
|
|
|
std::vector<uchar> _buf(bufSize);
|
|
|
|
uchar * buf = &_buf[0];
|
|
|
|
scalarToRawData(sc, buf, CV_MAKE_TYPE(depth, cn));
|
|
|
|
memset(buf + elemSize1 * cn, 0, (ocn - cn) * elemSize1);
|
|
|
|
|
|
|
|
return _buf;
|
|
|
|
}
|
|
|
|
|
2013-09-24 17:34:55 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2013-10-07 21:41:50 +08:00
|
|
|
/////////////// add subtract multiply divide min max /////////////////////////
|
2013-09-24 17:34:55 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-10-08 21:16:30 +08:00
|
|
|
enum { ADD = 0, SUB, MUL, DIV, ABS, ABS_DIFF, MIN, MAX };
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:34:55 +08:00
|
|
|
static void arithmetic_run_generic(const oclMat &src1, const oclMat &src2, const Scalar & scalar, const oclMat & mask,
|
|
|
|
oclMat &dst, int op_type, bool use_scalar = false)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-24 17:34:55 +08:00
|
|
|
Context *clCxt = src1.clCxt;
|
2013-09-20 23:19:52 +08:00
|
|
|
bool hasDouble = clCxt->supportsFeature(FEATURE_CL_DOUBLE);
|
2013-09-24 17:34:55 +08:00
|
|
|
if (!hasDouble && (src1.depth() == CV_64F || src2.depth() == CV_64F || dst.depth() == CV_64F))
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
2012-07-17 01:08:14 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-24 17:34:55 +08:00
|
|
|
CV_Assert(src2.empty() || (!src2.empty() && src1.type() == src2.type() && src1.size() == src2.size()));
|
|
|
|
CV_Assert(mask.empty() || (!mask.empty() && mask.type() == CV_8UC1 && mask.size() == src1.size()));
|
2013-10-07 21:41:50 +08:00
|
|
|
CV_Assert(op_type >= ADD && op_type <= MAX);
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:34:55 +08:00
|
|
|
dst.create(src1.size(), src1.type());
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:34:55 +08:00
|
|
|
int oclChannels = src1.oclchannels(), depth = src1.depth();
|
|
|
|
int src1step1 = src1.step / src1.elemSize(), src1offset1 = src1.offset / src1.elemSize();
|
|
|
|
int src2step1 = src2.step / src2.elemSize(), src2offset1 = src2.offset / src2.elemSize();
|
|
|
|
int maskstep1 = mask.step, maskoffset1 = mask.offset / mask.elemSize();
|
|
|
|
int dststep1 = dst.step / dst.elemSize(), dstoffset1 = dst.offset / dst.elemSize();
|
2013-10-12 04:58:58 +08:00
|
|
|
std::vector<uchar> m;
|
2013-09-24 17:34:55 +08:00
|
|
|
|
|
|
|
size_t localThreads[3] = { 16, 16, 1 };
|
|
|
|
size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
|
|
|
|
|
2013-10-04 00:04:04 +08:00
|
|
|
std::string kernelName = "arithm_binary_op";
|
2013-09-24 17:34:55 +08:00
|
|
|
|
|
|
|
const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
|
|
|
|
const char * const WTypeMap[] = { "short", "short", "int", "int", "int", "float", "double" };
|
2013-10-08 21:16:30 +08:00
|
|
|
const char * const funcMap[] = { "FUNC_ADD", "FUNC_SUB", "FUNC_MUL", "FUNC_DIV", "FUNC_ABS", "FUNC_ABS_DIFF", "FUNC_MIN", "FUNC_MAX" };
|
2013-09-24 17:34:55 +08:00
|
|
|
const char * const channelMap[] = { "", "", "2", "4", "4" };
|
|
|
|
bool haveScalar = use_scalar || src2.empty();
|
|
|
|
|
|
|
|
int WDepth = depth;
|
|
|
|
if (haveScalar)
|
|
|
|
WDepth = hasDouble && WDepth == CV_64F ? CV_64F : CV_32F;
|
|
|
|
if (op_type == DIV)
|
|
|
|
WDepth = hasDouble ? CV_64F : CV_32F;
|
|
|
|
else if (op_type == MUL)
|
|
|
|
WDepth = hasDouble && (depth == CV_32S || depth == CV_64F) ? CV_64F : CV_32F;
|
|
|
|
|
2013-10-04 00:04:04 +08:00
|
|
|
std::string buildOptions = format("-D T=%s%s -D WT=%s%s -D convertToT=convert_%s%s%s -D %s "
|
|
|
|
"-D convertToWT=convert_%s%s",
|
2013-09-24 17:34:55 +08:00
|
|
|
typeMap[depth], channelMap[oclChannels],
|
|
|
|
WTypeMap[WDepth], channelMap[oclChannels],
|
|
|
|
typeMap[depth], channelMap[oclChannels], (depth >= CV_32F ? "" : (depth == CV_32S ? "_rte" : "_sat_rte")),
|
2013-10-04 00:04:04 +08:00
|
|
|
funcMap[op_type], WTypeMap[WDepth], channelMap[oclChannels]);
|
2012-07-17 01:08:14 +08:00
|
|
|
|
|
|
|
vector<pair<size_t , const void *> > args;
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src1.data ));
|
2013-09-24 17:34:55 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1step1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1offset1 ));
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:34:55 +08:00
|
|
|
if (!src2.empty())
|
2013-04-06 13:37:36 +08:00
|
|
|
{
|
2013-09-24 17:34:55 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src2.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src2step1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src2offset1 ));
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:34:55 +08:00
|
|
|
kernelName += "_mat";
|
2013-10-04 00:04:04 +08:00
|
|
|
|
|
|
|
if (haveScalar)
|
|
|
|
buildOptions += " -D HAVE_SCALAR";
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
2013-09-24 17:34:55 +08:00
|
|
|
if (haveScalar)
|
2013-01-25 18:31:34 +08:00
|
|
|
{
|
2013-09-24 17:34:55 +08:00
|
|
|
const int WDepthMap[] = { CV_16S, CV_16S, CV_32S, CV_32S, CV_32S, CV_32F, CV_64F };
|
2013-10-12 04:58:58 +08:00
|
|
|
m = scalarToVector(scalar, WDepthMap[WDepth], oclChannels, src1.channels());
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-10-12 04:58:58 +08:00
|
|
|
args.push_back( make_pair( m.size(), (void *)&m[0]));
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:34:55 +08:00
|
|
|
kernelName += "_scalar";
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
2013-09-24 17:34:55 +08:00
|
|
|
if (!mask.empty())
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-24 17:34:55 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&mask.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&maskstep1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&maskoffset1 ));
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:34:55 +08:00
|
|
|
kernelName += "_mask";
|
2012-08-03 14:08:36 +08:00
|
|
|
}
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:34:55 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dststep1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dstoffset1 ));
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:34:55 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1.cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1.rows ));
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:34:55 +08:00
|
|
|
openCLExecuteKernel(clCxt, mask.empty() ?
|
|
|
|
(!src2.empty() ? &arithm_add : &arithm_add_scalar) :
|
|
|
|
(!src2.empty() ? &arithm_add_mask : &arithm_add_scalar_mask),
|
|
|
|
kernelName, globalThreads, localThreads,
|
|
|
|
args, -1, -1, buildOptions.c_str());
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-24 17:34:55 +08:00
|
|
|
|
|
|
|
void cv::ocl::add(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-24 17:34:55 +08:00
|
|
|
arithmetic_run_generic(src1, src2, Scalar(), mask, dst, ADD);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cv::ocl::add(const oclMat &src1, const Scalar &src2, oclMat &dst, const oclMat &mask)
|
|
|
|
{
|
2013-09-24 17:34:55 +08:00
|
|
|
arithmetic_run_generic(src1, oclMat(), src2, mask, dst, ADD);
|
|
|
|
}
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:34:55 +08:00
|
|
|
void cv::ocl::subtract(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask)
|
|
|
|
{
|
|
|
|
arithmetic_run_generic(src1, src2, Scalar(), mask, dst, SUB);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cv::ocl::subtract(const oclMat &src1, const Scalar &src2, oclMat &dst, const oclMat &mask)
|
|
|
|
{
|
2013-09-24 17:34:55 +08:00
|
|
|
arithmetic_run_generic(src1, oclMat(), src2, mask, dst, SUB);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-24 17:34:55 +08:00
|
|
|
|
|
|
|
void cv::ocl::multiply(const oclMat &src1, const oclMat &src2, oclMat &dst, double scalar)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-24 17:34:55 +08:00
|
|
|
const bool use_scalar = !(std::abs(scalar - 1.0) < std::numeric_limits<double>::epsilon());
|
|
|
|
arithmetic_run_generic(src1, src2, Scalar::all(scalar), oclMat(), dst, MUL, use_scalar);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-24 17:34:55 +08:00
|
|
|
|
2013-05-17 15:34:22 +08:00
|
|
|
void cv::ocl::multiply(double scalar, const oclMat &src, oclMat &dst)
|
|
|
|
{
|
2013-09-24 17:34:55 +08:00
|
|
|
arithmetic_run_generic(src, oclMat(), Scalar::all(scalar), oclMat(), dst, MUL);
|
2013-05-17 15:34:22 +08:00
|
|
|
}
|
2013-09-24 17:34:55 +08:00
|
|
|
|
|
|
|
void cv::ocl::divide(const oclMat &src1, const oclMat &src2, oclMat &dst, double scalar)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-24 17:34:55 +08:00
|
|
|
const bool use_scalar = !(std::abs(scalar - 1.0) < std::numeric_limits<double>::epsilon());
|
|
|
|
arithmetic_run_generic(src1, src2, Scalar::all(scalar), oclMat(), dst, DIV, use_scalar);
|
|
|
|
}
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:34:55 +08:00
|
|
|
void cv::ocl::divide(double scalar, const oclMat &src, oclMat &dst)
|
|
|
|
{
|
|
|
|
arithmetic_run_generic(src, oclMat(), Scalar::all(scalar), oclMat(), dst, DIV);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-24 17:34:55 +08:00
|
|
|
|
2013-10-07 21:41:50 +08:00
|
|
|
void cv::ocl::min(const oclMat &src1, const oclMat &src2, oclMat &dst)
|
|
|
|
{
|
|
|
|
arithmetic_run_generic(src1, src2, Scalar::all(0), oclMat(), dst, MIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cv::ocl::max(const oclMat &src1, const oclMat &src2, oclMat &dst)
|
|
|
|
{
|
|
|
|
arithmetic_run_generic(src1, src2, Scalar::all(0), oclMat(), dst, MAX);
|
|
|
|
}
|
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2013-10-08 21:16:30 +08:00
|
|
|
/////////////////////////////Abs, Absdiff ////////////////////////////////////
|
2012-07-17 01:08:14 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2013-09-24 17:34:55 +08:00
|
|
|
|
2013-10-08 22:49:20 +08:00
|
|
|
void cv::ocl::abs(const oclMat &src, oclMat &dst)
|
2013-10-08 21:16:30 +08:00
|
|
|
{
|
|
|
|
// explicitly uses use_scalar (even if zero) so that the correct kernel is used
|
2013-10-08 22:49:20 +08:00
|
|
|
arithmetic_run_generic(src, oclMat(), Scalar(), oclMat(), dst, ABS, true);
|
2013-10-08 21:16:30 +08:00
|
|
|
}
|
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
void cv::ocl::absdiff(const oclMat &src1, const oclMat &src2, oclMat &dst)
|
|
|
|
{
|
2013-09-24 17:34:55 +08:00
|
|
|
arithmetic_run_generic(src1, src2, Scalar(), oclMat(), dst, ABS_DIFF);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-24 17:34:55 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
void cv::ocl::absdiff(const oclMat &src1, const Scalar &src2, oclMat &dst)
|
|
|
|
{
|
2013-09-24 17:34:55 +08:00
|
|
|
arithmetic_run_generic(src1, oclMat(), src2, oclMat(), dst, ABS_DIFF);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-24 17:34:55 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////// compare ///////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2013-09-24 17:58:18 +08:00
|
|
|
|
|
|
|
static void compare_run(const oclMat &src1, const oclMat &src2, oclMat &dst, int cmpOp,
|
2013-09-20 23:19:52 +08:00
|
|
|
string kernelName, const cv::ocl::ProgramEntry* source)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-24 17:58:18 +08:00
|
|
|
dst.create(src1.size(), CV_8UC1);
|
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
int depth = src1.depth();
|
|
|
|
size_t localThreads[3] = { 64, 4, 1 };
|
2013-09-24 17:58:18 +08:00
|
|
|
size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
|
|
|
|
|
|
|
|
int src1step1 = src1.step1(), src1offset1 = src1.offset / src1.elemSize1();
|
|
|
|
int src2step1 = src2.step1(), src2offset1 = src2.offset / src2.elemSize1();
|
|
|
|
int dststep1 = dst.step1(), dstoffset1 = dst.offset / dst.elemSize1();
|
|
|
|
|
|
|
|
const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
|
|
|
|
const char * operationMap[] = { "==", ">", ">=", "<", "<=", "!=" };
|
|
|
|
std::string buildOptions = format("-D T=%s -D Operation=%s", typeMap[depth], operationMap[cmpOp]);
|
2013-09-16 19:11:56 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
vector<pair<size_t , const void *> > args;
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src1.data ));
|
2013-09-24 17:58:18 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1step1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1offset1 ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src2.data ));
|
2013-09-24 17:58:18 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src2step1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src2offset1 ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data ));
|
2013-09-24 17:58:18 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dststep1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dstoffset1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1.cols ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1.rows ));
|
2013-09-24 17:58:18 +08:00
|
|
|
|
2013-10-09 22:05:09 +08:00
|
|
|
openCLExecuteKernel(src1.clCxt, source, kernelName, globalThreads, localThreads,
|
2013-09-24 17:58:18 +08:00
|
|
|
args, -1, -1, buildOptions.c_str());
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cv::ocl::compare(const oclMat &src1, const oclMat &src2, oclMat &dst , int cmpOp)
|
|
|
|
{
|
2013-09-20 23:19:52 +08:00
|
|
|
if (!src1.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src1.depth() == CV_64F)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
2012-07-17 01:08:14 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-09-24 17:58:18 +08:00
|
|
|
|
2013-10-09 22:05:09 +08:00
|
|
|
CV_Assert(src1.type() == src2.type() && src1.channels() == 1);
|
2013-09-24 17:58:18 +08:00
|
|
|
CV_Assert(cmpOp >= CMP_EQ && cmpOp <= CMP_NE);
|
|
|
|
|
|
|
|
compare_run(src1, src2, dst, cmpOp, "arithm_compare", &arithm_compare);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////// sum //////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-09-27 21:56:30 +08:00
|
|
|
enum { SUM = 0, ABS_SUM, SQR_SUM };
|
|
|
|
|
|
|
|
static void arithmetic_sum_buffer_run(const oclMat &src, cl_mem &dst, int groupnum, int type, int ddepth)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-27 21:56:30 +08:00
|
|
|
int ochannels = src.oclchannels();
|
|
|
|
int all_cols = src.step / src.elemSize();
|
|
|
|
int pre_cols = (src.offset % src.step) / src.elemSize();
|
|
|
|
int sec_cols = all_cols - (src.offset % src.step + src.cols * src.elemSize() - 1) / src.elemSize() - 1;
|
2012-07-17 01:08:14 +08:00
|
|
|
int invalid_cols = pre_cols + sec_cols;
|
|
|
|
int cols = all_cols - invalid_cols , elemnum = cols * src.rows;;
|
2013-09-27 21:56:30 +08:00
|
|
|
int offset = src.offset / src.elemSize();
|
|
|
|
|
|
|
|
const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
|
|
|
|
const char * const funcMap[] = { "FUNC_SUM", "FUNC_ABS_SUM", "FUNC_SQR_SUM" };
|
|
|
|
const char * const channelMap[] = { " ", " ", "2", "4", "4" };
|
|
|
|
string buildOptions = format("-D srcT=%s%s -D dstT=%s%s -D convertToDstT=convert_%s%s -D %s",
|
|
|
|
typeMap[src.depth()], channelMap[ochannels],
|
|
|
|
typeMap[ddepth], channelMap[ochannels],
|
|
|
|
typeMap[ddepth], channelMap[ochannels],
|
|
|
|
funcMap[type]);
|
|
|
|
|
|
|
|
vector<pair<size_t , const void *> > args;
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&invalid_cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&offset));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&elemnum));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&groupnum));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst ));
|
2013-09-27 21:56:30 +08:00
|
|
|
size_t globalThreads[3] = { groupnum * 256, 1, 1 };
|
|
|
|
size_t localThreads[3] = { 256, 1, 1 };
|
|
|
|
|
|
|
|
openCLExecuteKernel(src.clCxt, &arithm_sum, "arithm_op_sum", globalThreads, localThreads,
|
|
|
|
args, -1, -1, buildOptions.c_str());
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2013-09-27 21:56:30 +08:00
|
|
|
Scalar arithmetic_sum(const oclMat &src, int type, int ddepth)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-27 21:56:30 +08:00
|
|
|
CV_Assert(src.step % src.elemSize() == 0);
|
|
|
|
|
2013-09-20 23:19:52 +08:00
|
|
|
size_t groupnum = src.clCxt->getDeviceInfo().maxComputeUnits;
|
2012-07-17 01:08:14 +08:00
|
|
|
CV_Assert(groupnum != 0);
|
2013-09-27 21:56:30 +08:00
|
|
|
|
|
|
|
int dbsize = groupnum * src.oclchannels();
|
2012-07-17 01:08:14 +08:00
|
|
|
Context *clCxt = src.clCxt;
|
2013-09-25 19:28:12 +08:00
|
|
|
|
|
|
|
AutoBuffer<T> _buf(dbsize);
|
|
|
|
T *p = (T*)_buf;
|
2012-07-17 01:08:14 +08:00
|
|
|
memset(p, 0, dbsize * sizeof(T));
|
2013-09-27 21:56:30 +08:00
|
|
|
|
|
|
|
cl_mem dstBuffer = openCLCreateBuffer(clCxt, CL_MEM_WRITE_ONLY, dbsize * sizeof(T));
|
|
|
|
arithmetic_sum_buffer_run(src, dstBuffer, groupnum, type, ddepth);
|
2012-10-11 16:22:47 +08:00
|
|
|
openCLReadBuffer(clCxt, dstBuffer, (void *)p, dbsize * sizeof(T));
|
2013-09-27 21:56:30 +08:00
|
|
|
openCLFree(dstBuffer);
|
|
|
|
|
|
|
|
Scalar s = Scalar::all(0.0);
|
2013-09-25 19:02:47 +08:00
|
|
|
for (int i = 0; i < dbsize;)
|
2013-09-27 21:56:30 +08:00
|
|
|
for (int j = 0; j < src.oclchannels(); j++, i++)
|
2012-07-17 01:08:14 +08:00
|
|
|
s.val[j] += p[i];
|
2013-09-25 19:28:12 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2013-09-27 21:56:30 +08:00
|
|
|
typedef Scalar (*sumFunc)(const oclMat &src, int type, int ddepth);
|
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
Scalar cv::ocl::sum(const oclMat &src)
|
|
|
|
{
|
2013-09-20 23:19:52 +08:00
|
|
|
if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
2013-10-08 20:02:47 +08:00
|
|
|
return Scalar::all(0);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-27 21:56:30 +08:00
|
|
|
static sumFunc functab[3] =
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-27 21:56:30 +08:00
|
|
|
arithmetic_sum<int>,
|
2012-07-17 01:08:14 +08:00
|
|
|
arithmetic_sum<float>,
|
|
|
|
arithmetic_sum<double>
|
|
|
|
};
|
|
|
|
|
2013-09-27 21:56:30 +08:00
|
|
|
int ddepth = std::max(src.depth(), CV_32S);
|
|
|
|
sumFunc func = functab[ddepth - CV_32S];
|
|
|
|
return func(src, SUM, ddepth);
|
2012-10-11 16:22:47 +08:00
|
|
|
}
|
|
|
|
|
2012-10-26 17:43:59 +08:00
|
|
|
Scalar cv::ocl::absSum(const oclMat &src)
|
|
|
|
{
|
2013-09-20 23:19:52 +08:00
|
|
|
if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
|
2012-10-26 17:43:59 +08:00
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
2013-10-08 20:02:47 +08:00
|
|
|
return cv::Scalar::all(0);
|
2012-10-26 17:43:59 +08:00
|
|
|
}
|
2013-10-08 20:02:47 +08:00
|
|
|
|
2013-09-27 21:56:30 +08:00
|
|
|
static sumFunc functab[3] =
|
2012-10-26 17:43:59 +08:00
|
|
|
{
|
2013-09-27 21:56:30 +08:00
|
|
|
arithmetic_sum<int>,
|
2012-10-26 17:43:59 +08:00
|
|
|
arithmetic_sum<float>,
|
|
|
|
arithmetic_sum<double>
|
|
|
|
};
|
|
|
|
|
2013-09-27 21:56:30 +08:00
|
|
|
int ddepth = std::max(src.depth(), CV_32S);
|
|
|
|
sumFunc func = functab[ddepth - CV_32S];
|
|
|
|
return func(src, ABS_SUM, ddepth);
|
2012-10-26 17:43:59 +08:00
|
|
|
}
|
2012-10-11 16:22:47 +08:00
|
|
|
|
|
|
|
Scalar cv::ocl::sqrSum(const oclMat &src)
|
|
|
|
{
|
2013-09-20 23:19:52 +08:00
|
|
|
if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
|
2012-10-11 16:22:47 +08:00
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
2013-10-08 20:02:47 +08:00
|
|
|
return cv::Scalar::all(0);
|
2012-10-11 16:22:47 +08:00
|
|
|
}
|
2013-09-27 21:56:30 +08:00
|
|
|
static sumFunc functab[3] =
|
2012-10-11 16:22:47 +08:00
|
|
|
{
|
2013-09-27 21:56:30 +08:00
|
|
|
arithmetic_sum<int>,
|
2013-10-08 20:02:47 +08:00
|
|
|
arithmetic_sum<float>,
|
2012-10-11 16:22:47 +08:00
|
|
|
arithmetic_sum<double>
|
|
|
|
};
|
|
|
|
|
2013-10-09 22:05:09 +08:00
|
|
|
int ddepth = std::max(src.depth(), CV_32S);
|
2013-09-27 21:56:30 +08:00
|
|
|
sumFunc func = functab[ddepth - CV_32S];
|
|
|
|
return func(src, SQR_SUM, ddepth);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////// meanStdDev //////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
void cv::ocl::meanStdDev(const oclMat &src, Scalar &mean, Scalar &stddev)
|
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
if (src.depth() == CV_64F && !src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
|
|
|
|
{
|
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-30 21:36:59 +08:00
|
|
|
double total = 1.0 / src.size().area();
|
|
|
|
|
|
|
|
mean = sum(src);
|
|
|
|
stddev = sqrSum(src);
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; ++i)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-30 21:36:59 +08:00
|
|
|
mean[i] *= total;
|
|
|
|
stddev[i] = std::sqrt(std::max(stddev[i] * total - mean.val[i] * mean.val[i] , 0.));
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////// minMax /////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2013-09-27 19:53:47 +08:00
|
|
|
template <typename T, typename WT>
|
|
|
|
static void arithmetic_minMax_run(const oclMat &src, const oclMat & mask, cl_mem &dst, int groupnum, string kernelName)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-27 19:53:47 +08:00
|
|
|
int all_cols = src.step / src.elemSize();
|
|
|
|
int pre_cols = (src.offset % src.step) / src.elemSize();
|
|
|
|
int sec_cols = all_cols - (src.offset % src.step + src.cols * src.elemSize() - 1) / src.elemSize() - 1;
|
2012-07-17 01:08:14 +08:00
|
|
|
int invalid_cols = pre_cols + sec_cols;
|
2013-09-27 19:53:47 +08:00
|
|
|
int cols = all_cols - invalid_cols , elemnum = cols * src.rows;
|
|
|
|
int offset = src.offset / src.elemSize();
|
|
|
|
|
|
|
|
const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
|
|
|
|
const char * const channelMap[] = { " ", " ", "2", "4", "4" };
|
|
|
|
|
|
|
|
ostringstream stream;
|
|
|
|
stream << "-D T=" << typeMap[src.depth()] << channelMap[src.channels()];
|
2013-10-27 19:55:40 +08:00
|
|
|
if (numeric_limits<T>::is_integer)
|
|
|
|
{
|
|
|
|
stream << " -D MAX_VAL=" << (WT)numeric_limits<T>::max();
|
|
|
|
stream << " -D MIN_VAL=" << (WT)numeric_limits<T>::min();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
stream << " -D DEPTH_" << src.depth();
|
|
|
|
std::string buildOptions = stream.str();
|
2013-09-27 19:53:47 +08:00
|
|
|
|
|
|
|
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_mem) , (void *)&dst ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&invalid_cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&offset));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&elemnum));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&groupnum));
|
2013-09-27 19:53:47 +08:00
|
|
|
|
|
|
|
int minvalid_cols = 0, moffset = 0;
|
2013-09-25 19:02:47 +08:00
|
|
|
if (!mask.empty())
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-27 19:53:47 +08:00
|
|
|
int mall_cols = mask.step / mask.elemSize();
|
|
|
|
int mpre_cols = (mask.offset % mask.step) / mask.elemSize();
|
|
|
|
int msec_cols = mall_cols - (mask.offset % mask.step + mask.cols * mask.elemSize() - 1) / mask.elemSize() - 1;
|
|
|
|
minvalid_cols = mpre_cols + msec_cols;
|
|
|
|
moffset = mask.offset / mask.elemSize();
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-27 19:53:47 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem) , (void *)&mask.data ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&minvalid_cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&moffset ));
|
2013-09-27 19:53:47 +08:00
|
|
|
|
|
|
|
kernelName += "_mask";
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
2013-09-27 19:53:47 +08:00
|
|
|
size_t globalThreads[3] = {groupnum * 256, 1, 1};
|
|
|
|
size_t localThreads[3] = {256, 1, 1};
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-27 19:53:47 +08:00
|
|
|
openCLExecuteKernel(src.clCxt, &arithm_minMax, kernelName, globalThreads, localThreads,
|
|
|
|
args, -1, -1, buildOptions.c_str());
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
2013-09-27 19:53:47 +08:00
|
|
|
template <typename T, typename WT>
|
2013-10-03 15:36:49 +08:00
|
|
|
void arithmetic_minMax(const oclMat &src, double *minVal, double *maxVal, const oclMat &mask)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-20 23:19:52 +08:00
|
|
|
size_t groupnum = src.clCxt->getDeviceInfo().maxComputeUnits;
|
2012-07-17 01:08:14 +08:00
|
|
|
CV_Assert(groupnum != 0);
|
2013-05-22 13:46:42 +08:00
|
|
|
|
2013-09-27 19:53:47 +08:00
|
|
|
int dbsize = groupnum * 2 * src.elemSize();
|
2013-10-03 15:36:49 +08:00
|
|
|
oclMat buf;
|
2013-05-22 13:46:42 +08:00
|
|
|
ensureSizeIsEnough(1, dbsize, CV_8UC1, buf);
|
|
|
|
|
|
|
|
cl_mem buf_data = reinterpret_cast<cl_mem>(buf.data);
|
2013-09-27 19:53:47 +08:00
|
|
|
arithmetic_minMax_run<T, WT>(src, mask, buf_data, groupnum, "arithm_op_minMax");
|
2013-05-22 13:46:42 +08:00
|
|
|
|
|
|
|
Mat matbuf = Mat(buf);
|
|
|
|
T *p = matbuf.ptr<T>();
|
2013-09-25 19:02:47 +08:00
|
|
|
if (minVal != NULL)
|
2013-05-22 13:46:42 +08:00
|
|
|
{
|
|
|
|
*minVal = std::numeric_limits<double>::max();
|
2013-09-27 19:53:47 +08:00
|
|
|
for (int i = 0, end = src.oclchannels() * (int)groupnum; i < end; i++)
|
2012-10-26 17:43:59 +08:00
|
|
|
*minVal = *minVal < p[i] ? *minVal : p[i];
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-25 19:02:47 +08:00
|
|
|
if (maxVal != NULL)
|
2013-05-22 13:46:42 +08:00
|
|
|
{
|
|
|
|
*maxVal = -std::numeric_limits<double>::max();
|
2013-09-27 19:53:47 +08:00
|
|
|
for (int i = src.oclchannels() * (int)groupnum, end = i << 1; i < end; i++)
|
2012-10-26 17:43:59 +08:00
|
|
|
*maxVal = *maxVal > p[i] ? *maxVal : p[i];
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-03 15:36:49 +08:00
|
|
|
typedef void (*minMaxFunc)(const oclMat &src, double *minVal, double *maxVal, const oclMat &mask);
|
2013-09-27 19:53:47 +08:00
|
|
|
|
2013-10-03 15:36:49 +08:00
|
|
|
void cv::ocl::minMax(const oclMat &src, double *minVal, double *maxVal, const oclMat &mask)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-27 19:53:47 +08:00
|
|
|
CV_Assert(src.channels() == 1);
|
|
|
|
CV_Assert(src.size() == mask.size() || mask.empty());
|
|
|
|
CV_Assert(src.step % src.elemSize() == 0);
|
|
|
|
|
|
|
|
if (minVal == NULL && maxVal == NULL)
|
|
|
|
return;
|
|
|
|
|
2013-09-20 23:19:52 +08:00
|
|
|
if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
2013-10-08 20:02:47 +08:00
|
|
|
return;
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-27 19:53:47 +08:00
|
|
|
|
2013-10-03 15:36:49 +08:00
|
|
|
static minMaxFunc functab[] =
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-27 19:53:47 +08:00
|
|
|
arithmetic_minMax<uchar, int>,
|
|
|
|
arithmetic_minMax<char, int>,
|
|
|
|
arithmetic_minMax<ushort, int>,
|
|
|
|
arithmetic_minMax<short, int>,
|
|
|
|
arithmetic_minMax<int, int>,
|
|
|
|
arithmetic_minMax<float, float>,
|
|
|
|
arithmetic_minMax<double, double>,
|
2012-07-17 01:08:14 +08:00
|
|
|
0
|
|
|
|
};
|
2013-09-27 19:53:47 +08:00
|
|
|
|
2013-10-03 15:36:49 +08:00
|
|
|
minMaxFunc func = functab[src.depth()];
|
|
|
|
CV_Assert(func != 0);
|
|
|
|
|
|
|
|
func(src, minVal, maxVal, mask);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
/////////////////////////////////// norm /////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
double cv::ocl::norm(const oclMat &src1, int normType)
|
|
|
|
{
|
2013-09-30 22:58:32 +08:00
|
|
|
CV_Assert((normType & NORM_RELATIVE) == 0);
|
|
|
|
return norm(src1, oclMat(), normType);
|
|
|
|
}
|
|
|
|
|
2013-10-08 20:02:47 +08:00
|
|
|
static void arithm_absdiff_nonsaturate_run(const oclMat & src1, const oclMat & src2, oclMat & diff, int ntype)
|
2013-09-30 22:58:32 +08:00
|
|
|
{
|
|
|
|
Context *clCxt = src1.clCxt;
|
2013-10-09 22:05:09 +08:00
|
|
|
if (!clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src1.depth() == CV_64F)
|
|
|
|
{
|
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CV_Assert(src1.step % src1.elemSize() == 0 && (src2.empty() || src2.step % src2.elemSize() == 0));
|
2013-09-30 22:58:32 +08:00
|
|
|
|
2013-10-08 20:02:47 +08:00
|
|
|
int ddepth = std::max(src1.depth(), CV_32S);
|
|
|
|
if (ntype == NORM_L2)
|
|
|
|
ddepth = std::max<int>(CV_32F, ddepth);
|
|
|
|
|
2013-09-30 22:58:32 +08:00
|
|
|
diff.create(src1.size(), CV_MAKE_TYPE(ddepth, src1.channels()));
|
2013-10-08 20:02:47 +08:00
|
|
|
CV_Assert(diff.step % diff.elemSize() == 0);
|
2013-09-30 22:58:32 +08:00
|
|
|
|
|
|
|
int oclChannels = src1.oclchannels(), sdepth = src1.depth();
|
|
|
|
int src1step1 = src1.step / src1.elemSize(), src1offset1 = src1.offset / src1.elemSize();
|
|
|
|
int src2step1 = src2.step / src2.elemSize(), src2offset1 = src2.offset / src2.elemSize();
|
|
|
|
int diffstep1 = diff.step / diff.elemSize(), diffoffset1 = diff.offset / diff.elemSize();
|
|
|
|
|
|
|
|
string kernelName = "arithm_absdiff_nonsaturate";
|
|
|
|
size_t localThreads[3] = { 16, 16, 1 };
|
|
|
|
size_t globalThreads[3] = { diff.cols, diff.rows, 1 };
|
|
|
|
|
|
|
|
const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
|
|
|
|
const char * const channelMap[] = { "", "", "2", "4", "4" };
|
|
|
|
|
|
|
|
std::string buildOptions = format("-D srcT=%s%s -D dstT=%s%s -D convertToDstT=convert_%s%s",
|
|
|
|
typeMap[sdepth], channelMap[oclChannels],
|
|
|
|
typeMap[ddepth], channelMap[oclChannels],
|
|
|
|
typeMap[ddepth], channelMap[oclChannels]);
|
|
|
|
|
|
|
|
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 *)&src1step1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1offset1 ));
|
|
|
|
|
|
|
|
if (!src2.empty())
|
|
|
|
{
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src2.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src2step1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src2offset1 ));
|
|
|
|
|
|
|
|
kernelName += "_binary";
|
|
|
|
}
|
|
|
|
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&diff.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&diffstep1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&diffoffset1 ));
|
|
|
|
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1.cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1.rows ));
|
|
|
|
|
|
|
|
openCLExecuteKernel(clCxt, &arithm_absdiff_nonsaturate,
|
|
|
|
kernelName, globalThreads, localThreads,
|
|
|
|
args, -1, -1, buildOptions.c_str());
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
double cv::ocl::norm(const oclMat &src1, const oclMat &src2, int normType)
|
|
|
|
{
|
2013-10-01 01:32:46 +08:00
|
|
|
if (!src1.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src1.depth() == CV_64F)
|
2013-09-30 22:58:32 +08:00
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
|
|
|
return -1;
|
2013-09-30 22:58:32 +08:00
|
|
|
}
|
2013-10-09 22:05:09 +08:00
|
|
|
CV_Assert(src2.empty() || (src1.type() == src2.type() && src1.size() == src2.size()));
|
2013-09-30 22:58:32 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
bool isRelative = (normType & NORM_RELATIVE) != 0;
|
2013-09-30 22:58:32 +08:00
|
|
|
normType &= NORM_TYPE_MASK;
|
|
|
|
CV_Assert(normType == NORM_INF || normType == NORM_L1 || normType == NORM_L2);
|
|
|
|
|
|
|
|
Scalar s;
|
|
|
|
int cn = src1.channels();
|
2012-07-17 01:08:14 +08:00
|
|
|
double r = 0;
|
2013-09-30 22:58:32 +08:00
|
|
|
oclMat diff;
|
2013-10-08 20:02:47 +08:00
|
|
|
|
|
|
|
arithm_absdiff_nonsaturate_run(src1, src2, diff, normType);
|
2013-09-30 22:58:32 +08:00
|
|
|
|
|
|
|
switch (normType)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
|
|
|
case NORM_INF:
|
2013-09-30 22:58:32 +08:00
|
|
|
diff = diff.reshape(1);
|
|
|
|
minMax(diff, NULL, &r);
|
2012-07-17 01:08:14 +08:00
|
|
|
break;
|
|
|
|
case NORM_L1:
|
2013-09-30 22:58:32 +08:00
|
|
|
s = sum(diff);
|
|
|
|
for (int i = 0; i < cn; ++i)
|
|
|
|
r += s[i];
|
2012-07-17 01:08:14 +08:00
|
|
|
break;
|
|
|
|
case NORM_L2:
|
2013-09-30 22:58:32 +08:00
|
|
|
s = sqrSum(diff);
|
|
|
|
for (int i = 0; i < cn; ++i)
|
|
|
|
r += s[i];
|
2012-07-17 01:08:14 +08:00
|
|
|
r = std::sqrt(r);
|
|
|
|
break;
|
|
|
|
}
|
2013-09-25 19:02:47 +08:00
|
|
|
if (isRelative)
|
2013-10-30 20:48:12 +08:00
|
|
|
r = r / (norm(src2, normType) + DBL_EPSILON);
|
2013-09-30 22:58:32 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////// flip //////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2013-10-27 03:43:44 +08:00
|
|
|
enum { FLIP_COLS = 1 << 0, FLIP_ROWS = 1 << 1, FLIP_BOTH = FLIP_ROWS | FLIP_COLS };
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-10-27 03:43:44 +08:00
|
|
|
static void arithmetic_flip_run(const oclMat &src, oclMat &dst, string kernelName, int flipType)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-10-27 03:43:44 +08:00
|
|
|
int cols = dst.cols, rows = dst.rows;
|
|
|
|
if ((cols == 1 && flipType == FLIP_COLS) ||
|
|
|
|
(rows == 1 && flipType == FLIP_ROWS) ||
|
|
|
|
(rows == 1 && cols == 1 && flipType == FLIP_BOTH))
|
|
|
|
{
|
|
|
|
src.copyTo(dst);
|
|
|
|
return;
|
|
|
|
}
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-10-27 03:43:44 +08:00
|
|
|
cols = flipType == FLIP_COLS ? divUp(cols, 2) : cols;
|
|
|
|
rows = flipType & FLIP_ROWS ? divUp(rows, 2) : rows;
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-10-27 03:43:44 +08:00
|
|
|
const char * const channelMap[] = { "", "", "2", "4", "4" };
|
|
|
|
const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
|
|
|
|
std::string buildOptions = format("-D T=%s%s", typeMap[dst.depth()], channelMap[dst.oclchannels()]);
|
2012-07-17 01:08:14 +08:00
|
|
|
|
|
|
|
size_t localThreads[3] = { 64, 4, 1 };
|
2013-09-16 19:11:56 +08:00
|
|
|
size_t globalThreads[3] = { cols, rows, 1 };
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-10-27 03:43:44 +08:00
|
|
|
int elemSize = src.elemSize();
|
|
|
|
int src_step = src.step / elemSize, src_offset = src.offset / elemSize;
|
|
|
|
int dst_step = dst.step / elemSize, dst_offset = dst.offset / elemSize;
|
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
vector<pair<size_t , const void *> > args;
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src.data ));
|
2013-10-27 03:43:44 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src_step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src_offset ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data ));
|
2013-10-27 03:43:44 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst_step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst_offset ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst.rows ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst.cols ));
|
2013-10-27 03:43:44 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&rows ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&cols ));
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-10-27 03:43:44 +08:00
|
|
|
openCLExecuteKernel(src.clCxt, &arithm_flip, kernelName, globalThreads, localThreads, args,
|
|
|
|
-1, -1, buildOptions.c_str());
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
void cv::ocl::flip(const oclMat &src, oclMat &dst, int flipCode)
|
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
|
|
|
|
{
|
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
dst.create(src.size(), src.type());
|
2013-10-09 22:05:09 +08:00
|
|
|
|
2013-09-25 19:02:47 +08:00
|
|
|
if (flipCode == 0)
|
2013-10-27 03:43:44 +08:00
|
|
|
arithmetic_flip_run(src, dst, "arithm_flip_rows", FLIP_ROWS);
|
2013-09-25 19:02:47 +08:00
|
|
|
else if (flipCode > 0)
|
2013-10-27 03:43:44 +08:00
|
|
|
arithmetic_flip_run(src, dst, "arithm_flip_cols", FLIP_COLS);
|
2012-07-17 01:08:14 +08:00
|
|
|
else
|
2013-10-27 03:43:44 +08:00
|
|
|
arithmetic_flip_run(src, dst, "arithm_flip_rows_cols", FLIP_BOTH);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////// LUT //////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2013-09-24 17:49:38 +08:00
|
|
|
|
|
|
|
static void arithmetic_lut_run(const oclMat &src, const oclMat &lut, oclMat &dst, string kernelName)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-24 17:49:38 +08:00
|
|
|
int sdepth = src.depth();
|
|
|
|
int src_step1 = src.step1(), dst_step1 = dst.step1();
|
|
|
|
int src_offset1 = src.offset / src.elemSize1(), dst_offset1 = dst.offset / dst.elemSize1();
|
|
|
|
int lut_offset1 = lut.offset / lut.elemSize1() + (sdepth == CV_8U ? 0 : 128) * lut.channels();
|
|
|
|
int cols1 = src.cols * src.oclchannels();
|
|
|
|
|
|
|
|
size_t localSize[] = { 16, 16, 1 };
|
|
|
|
size_t globalSize[] = { lut.channels() == 1 ? cols1 : src.cols, src.rows, 1 };
|
|
|
|
|
|
|
|
const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
|
|
|
|
std::string buildOptions = format("-D srcT=%s -D dstT=%s", typeMap[sdepth], typeMap[dst.depth()]);
|
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
vector<pair<size_t , const void *> > args;
|
2013-09-24 17:49:38 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&lut.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&cols1));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src.rows ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src_offset1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&lut_offset1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst_offset1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src_step1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst_step1 ));
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-10-09 22:05:09 +08:00
|
|
|
openCLExecuteKernel(src.clCxt, &arithm_LUT, kernelName, globalSize, localSize,
|
2013-09-24 17:49:38 +08:00
|
|
|
args, lut.oclchannels(), -1, buildOptions.c_str());
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cv::ocl::LUT(const oclMat &src, const oclMat &lut, oclMat &dst)
|
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
if (!lut.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && lut.depth() == CV_64F)
|
|
|
|
{
|
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-24 17:49:38 +08:00
|
|
|
int cn = src.channels(), depth = src.depth();
|
2013-10-09 22:05:09 +08:00
|
|
|
|
2013-09-24 17:49:38 +08:00
|
|
|
CV_Assert(depth == CV_8U || depth == CV_8S);
|
|
|
|
CV_Assert(lut.channels() == 1 || lut.channels() == src.channels());
|
|
|
|
CV_Assert(lut.rows == 1 && lut.cols == 256);
|
2013-10-09 22:05:09 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
dst.create(src.size(), CV_MAKETYPE(lut.depth(), cn));
|
2013-10-09 22:05:09 +08:00
|
|
|
arithmetic_lut_run(src, lut, dst, "LUT");
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////// exp log /////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2013-09-24 18:02:01 +08:00
|
|
|
|
2013-09-20 23:19:52 +08:00
|
|
|
static void arithmetic_exp_log_run(const oclMat &src, oclMat &dst, string kernelName, const cv::ocl::ProgramEntry* source)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
|
|
|
Context *clCxt = src.clCxt;
|
2013-09-20 23:19:52 +08:00
|
|
|
if (!clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
|
2012-08-03 14:08:36 +08:00
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
2012-08-03 14:08:36 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-09-24 18:02:01 +08:00
|
|
|
|
|
|
|
CV_Assert( src.depth() == CV_32F || src.depth() == CV_64F);
|
|
|
|
dst.create(src.size(), src.type());
|
|
|
|
|
|
|
|
int ddepth = dst.depth();
|
|
|
|
int cols1 = src.cols * src.oclchannels();
|
|
|
|
int srcoffset1 = src.offset / src.elemSize1(), dstoffset1 = dst.offset / dst.elemSize1();
|
|
|
|
int srcstep1 = src.step1(), dststep1 = dst.step1();
|
2012-07-17 01:08:14 +08:00
|
|
|
|
|
|
|
size_t localThreads[3] = { 64, 4, 1 };
|
2013-09-16 19:11:56 +08:00
|
|
|
size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 18:02:01 +08:00
|
|
|
std::string buildOptions = format("-D srcT=%s",
|
|
|
|
ddepth == CV_32F ? "float" : "double");
|
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
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_mem), (void *)&dst.data ));
|
2013-09-24 18:02:01 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&cols1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src.rows ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&srcoffset1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dstoffset1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&srcstep1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dststep1 ));
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-20 23:19:52 +08:00
|
|
|
openCLExecuteKernel(clCxt, source, kernelName, globalThreads, localThreads,
|
2013-09-24 18:02:01 +08:00
|
|
|
args, src.oclchannels(), -1, buildOptions.c_str());
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-24 18:02:01 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
void cv::ocl::exp(const oclMat &src, oclMat &dst)
|
|
|
|
{
|
|
|
|
arithmetic_exp_log_run(src, dst, "arithm_exp", &arithm_exp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cv::ocl::log(const oclMat &src, oclMat &dst)
|
|
|
|
{
|
|
|
|
arithmetic_exp_log_run(src, dst, "arithm_log", &arithm_log);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////// magnitude phase ///////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2013-01-25 18:31:34 +08:00
|
|
|
static void arithmetic_magnitude_phase_run(const oclMat &src1, const oclMat &src2, oclMat &dst, string kernelName)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
|
|
|
int depth = dst.depth();
|
|
|
|
|
|
|
|
size_t localThreads[3] = { 64, 4, 1 };
|
2013-11-12 20:50:42 +08:00
|
|
|
size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
|
|
|
|
|
|
|
|
int src1_step = src1.step / src1.elemSize(), src1_offset = src1.offset / src1.elemSize();
|
|
|
|
int src2_step = src2.step / src2.elemSize(), src2_offset = src2.offset / src2.elemSize();
|
|
|
|
int dst_step = dst.step / dst.elemSize(), dst_offset = dst.offset / dst.elemSize();
|
2012-07-17 01:08:14 +08:00
|
|
|
|
|
|
|
vector<pair<size_t , const void *> > args;
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src1.data ));
|
2013-11-12 20:50:42 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1_step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1_offset ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src2.data ));
|
2013-11-12 20:50:42 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src2_step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src2_offset ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data ));
|
2013-11-12 20:50:42 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst_step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst_offset ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst.rows ));
|
2013-11-12 20:50:42 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst.cols ));
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-11-12 20:50:42 +08:00
|
|
|
const char * const channelMap[] = { "", "", "2", "4", "4" };
|
|
|
|
std::string buildOptions = format("-D T=%s%s", depth == CV_32F ? "float" : "double", channelMap[dst.channels()]);
|
|
|
|
|
|
|
|
openCLExecuteKernel(src1.clCxt, &arithm_magnitude, kernelName, globalThreads, localThreads, args, -1, -1, buildOptions.c_str());
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cv::ocl::magnitude(const oclMat &src1, const oclMat &src2, oclMat &dst)
|
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
if (!src1.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src1.depth() == CV_64F)
|
|
|
|
{
|
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
CV_Assert(src1.type() == src2.type() && src1.size() == src2.size() &&
|
|
|
|
(src1.depth() == CV_32F || src1.depth() == CV_64F));
|
|
|
|
|
|
|
|
dst.create(src1.size(), src1.type());
|
|
|
|
arithmetic_magnitude_phase_run(src1, src2, dst, "arithm_magnitude");
|
|
|
|
}
|
|
|
|
|
2013-09-20 23:19:52 +08:00
|
|
|
static void arithmetic_phase_run(const oclMat &src1, const oclMat &src2, oclMat &dst, string kernelName, const cv::ocl::ProgramEntry* source)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-30 19:08:26 +08:00
|
|
|
int depth = dst.depth(), cols1 = src1.cols * src1.oclchannels();
|
|
|
|
int src1step1 = src1.step / src1.elemSize1(), src1offset1 = src1.offset / src1.elemSize1();
|
|
|
|
int src2step1 = src2.step / src2.elemSize1(), src2offset1 = src2.offset / src2.elemSize1();
|
|
|
|
int dststep1 = dst.step / dst.elemSize1(), dstoffset1 = dst.offset / dst.elemSize1();
|
2012-07-17 01:08:14 +08:00
|
|
|
|
|
|
|
size_t localThreads[3] = { 64, 4, 1 };
|
2013-09-30 19:08:26 +08:00
|
|
|
size_t globalThreads[3] = { cols1, dst.rows, 1 };
|
2012-07-17 01:08:14 +08:00
|
|
|
|
|
|
|
vector<pair<size_t , const void *> > args;
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src1.data ));
|
2013-09-30 19:08:26 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1step1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1offset1 ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src2.data ));
|
2013-09-30 19:08:26 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src2step1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src2offset1 ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data ));
|
2013-09-30 19:08:26 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dststep1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dstoffset1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&cols1 ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst.rows ));
|
|
|
|
|
2013-10-09 22:05:09 +08:00
|
|
|
openCLExecuteKernel(src1.clCxt, source, kernelName, globalThreads, localThreads, args, -1, depth);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2013-09-30 19:08:26 +08:00
|
|
|
void cv::ocl::phase(const oclMat &x, const oclMat &y, oclMat &Angle, bool angleInDegrees)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
if (!x.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && x.depth() == CV_64F)
|
|
|
|
{
|
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
CV_Assert(x.type() == y.type() && x.size() == y.size() && (x.depth() == CV_32F || x.depth() == CV_64F));
|
2013-09-30 19:08:26 +08:00
|
|
|
CV_Assert(x.step % x.elemSize() == 0 && y.step % y.elemSize() == 0);
|
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
Angle.create(x.size(), x.type());
|
2013-09-30 19:08:26 +08:00
|
|
|
arithmetic_phase_run(x, y, Angle, angleInDegrees ? "arithm_phase_indegrees" : "arithm_phase_inradians", &arithm_phase);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////// cartToPolar ///////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2013-01-25 18:31:34 +08:00
|
|
|
static void arithmetic_cartToPolar_run(const oclMat &src1, const oclMat &src2, oclMat &dst_mag, oclMat &dst_cart,
|
2012-07-17 01:08:14 +08:00
|
|
|
string kernelName, bool angleInDegrees)
|
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
int channels = src1.oclchannels();
|
2012-07-17 01:08:14 +08:00
|
|
|
int depth = src1.depth();
|
|
|
|
|
|
|
|
int cols = src1.cols * channels;
|
|
|
|
|
|
|
|
size_t localThreads[3] = { 64, 4, 1 };
|
2013-09-16 19:11:56 +08:00
|
|
|
size_t globalThreads[3] = { cols, src1.rows, 1 };
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-11-12 20:50:42 +08:00
|
|
|
int src1_step = src1.step / src1.elemSize1(), src1_offset = src1.offset / src1.elemSize1();
|
|
|
|
int src2_step = src2.step / src2.elemSize1(), src2_offset = src2.offset / src2.elemSize1();
|
|
|
|
int dst_mag_step = dst_mag.step / dst_mag.elemSize1(), dst_mag_offset = dst_mag.offset / dst_mag.elemSize1();
|
|
|
|
int dst_cart_step = dst_cart.step / dst_cart.elemSize1(), dst_cart_offset = dst_cart.offset / dst_cart.elemSize1();
|
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
vector<pair<size_t , const void *> > args;
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src1.data ));
|
2013-11-12 20:50:42 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1_step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1_offset ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src2.data ));
|
2013-11-12 20:50:42 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src2_step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src2_offset ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&dst_mag.data ));
|
2013-11-12 20:50:42 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst_mag_step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst_mag_offset ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&dst_cart.data ));
|
2013-11-12 20:50:42 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst_cart_step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst_cart_offset ));
|
2013-09-16 19:11:56 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1.rows ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&cols ));
|
|
|
|
|
2013-11-12 20:50:42 +08:00
|
|
|
openCLExecuteKernel(src1.clCxt, &arithm_cartToPolar, kernelName, globalThreads, localThreads, args,
|
|
|
|
-1, depth, angleInDegrees ? "-D DEGREE" : "-D RADIAN");
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
void cv::ocl::cartToPolar(const oclMat &x, const oclMat &y, oclMat &mag, oclMat &angle, bool angleInDegrees)
|
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
if (!x.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && x.depth() == CV_64F)
|
|
|
|
{
|
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
CV_Assert(x.type() == y.type() && x.size() == y.size() && (x.depth() == CV_32F || x.depth() == CV_64F));
|
|
|
|
|
|
|
|
mag.create(x.size(), x.type());
|
|
|
|
angle.create(x.size(), x.type());
|
|
|
|
|
|
|
|
arithmetic_cartToPolar_run(x, y, mag, angle, "arithm_cartToPolar", angleInDegrees);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////// polarToCart ///////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2013-01-25 18:31:34 +08:00
|
|
|
static void arithmetic_ptc_run(const oclMat &src1, const oclMat &src2, oclMat &dst1, oclMat &dst2, bool angleInDegrees,
|
2012-07-17 01:08:14 +08:00
|
|
|
string kernelName)
|
|
|
|
{
|
2013-11-12 20:50:42 +08:00
|
|
|
int channels = src2.oclchannels(), depth = src2.depth();
|
|
|
|
int cols = src2.cols * channels, rows = src2.rows;
|
2012-07-17 01:08:14 +08:00
|
|
|
|
|
|
|
size_t localThreads[3] = { 64, 4, 1 };
|
2013-09-16 19:11:56 +08:00
|
|
|
size_t globalThreads[3] = { cols, rows, 1 };
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-11-12 20:50:42 +08:00
|
|
|
int src1_step = src1.step / src1.elemSize1(), src1_offset = src1.offset / src1.elemSize1();
|
|
|
|
int src2_step = src2.step / src2.elemSize1(), src2_offset = src2.offset / src2.elemSize1();
|
|
|
|
int dst1_step = dst1.step / dst1.elemSize1(), dst1_offset = dst1.offset / dst1.elemSize1();
|
|
|
|
int dst2_step = dst2.step / dst2.elemSize1(), dst2_offset = dst2.offset / dst2.elemSize1();
|
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
vector<pair<size_t , const void *> > args;
|
2013-09-25 19:02:47 +08:00
|
|
|
if (src1.data)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src1.data ));
|
2013-11-12 20:50:42 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1_step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1_offset ));
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src2.data ));
|
2013-11-12 20:50:42 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src2_step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src2_offset ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&dst1.data ));
|
2013-11-12 20:50:42 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst1_step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst1_offset ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&dst2.data ));
|
2013-11-12 20:50:42 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst2_step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst2_offset ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&rows ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&cols ));
|
|
|
|
|
2013-11-12 20:50:42 +08:00
|
|
|
openCLExecuteKernel(src1.clCxt, &arithm_polarToCart, kernelName, globalThreads, localThreads,
|
|
|
|
args, -1, depth, angleInDegrees ? "-D DEGREE" : "-D RADIAN");
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cv::ocl::polarToCart(const oclMat &magnitude, const oclMat &angle, oclMat &x, oclMat &y, bool angleInDegrees)
|
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
if (!magnitude.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && magnitude.depth() == CV_64F)
|
|
|
|
{
|
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
CV_Assert(angle.depth() == CV_32F || angle.depth() == CV_64F);
|
2013-10-09 22:05:09 +08:00
|
|
|
CV_Assert(magnitude.size() == angle.size() && magnitude.type() == angle.type());
|
2012-07-17 01:08:14 +08:00
|
|
|
|
|
|
|
x.create(angle.size(), angle.type());
|
|
|
|
y.create(angle.size(), angle.type());
|
|
|
|
|
2013-09-25 19:02:47 +08:00
|
|
|
if ( magnitude.data )
|
2012-07-17 01:08:14 +08:00
|
|
|
arithmetic_ptc_run(magnitude, angle, x, y, angleInDegrees, "arithm_polarToCart_mag");
|
|
|
|
else
|
|
|
|
arithmetic_ptc_run(magnitude, angle, x, y, angleInDegrees, "arithm_polarToCart");
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
/////////////////////////////////// minMaxLoc ////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2013-01-25 18:31:34 +08:00
|
|
|
static void arithmetic_minMaxLoc_run(const oclMat &src, cl_mem &dst, int vlen , int groupnum)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
|
|
|
vector<pair<size_t , const void *> > args;
|
|
|
|
int all_cols = src.step / (vlen * src.elemSize1());
|
|
|
|
int pre_cols = (src.offset % src.step) / (vlen * src.elemSize1());
|
|
|
|
int sec_cols = all_cols - (src.offset % src.step + src.cols * src.elemSize1() - 1) / (vlen * src.elemSize1()) - 1;
|
|
|
|
int invalid_cols = pre_cols + sec_cols;
|
|
|
|
int cols = all_cols - invalid_cols , elemnum = cols * src.rows;;
|
|
|
|
int offset = src.offset / (vlen * src.elemSize1());
|
|
|
|
int repeat_s = src.offset / src.elemSize1() - offset * vlen;
|
|
|
|
int repeat_e = (offset + cols) * vlen - src.offset / src.elemSize1() - src.cols;
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&invalid_cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&offset));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&elemnum));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&groupnum));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst ));
|
|
|
|
char build_options[50];
|
|
|
|
sprintf(build_options, "-D DEPTH_%d -D REPEAT_S%d -D REPEAT_E%d", src.depth(), repeat_s, repeat_e);
|
|
|
|
size_t gt[3] = {groupnum * 256, 1, 1}, lt[3] = {256, 1, 1};
|
|
|
|
openCLExecuteKernel(src.clCxt, &arithm_minMaxLoc, "arithm_op_minMaxLoc", gt, lt, args, -1, -1, build_options);
|
|
|
|
}
|
|
|
|
|
2013-01-25 18:31:34 +08:00
|
|
|
static void arithmetic_minMaxLoc_mask_run(const oclMat &src, const oclMat &mask, cl_mem &dst, int vlen, int groupnum)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
|
|
|
vector<pair<size_t , const void *> > args;
|
|
|
|
size_t gt[3] = {groupnum * 256, 1, 1}, lt[3] = {256, 1, 1};
|
|
|
|
char build_options[50];
|
2013-09-25 19:02:47 +08:00
|
|
|
if (src.oclchannels() == 1)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
|
|
|
int cols = (src.cols - 1) / vlen + 1;
|
|
|
|
int invalid_cols = src.step / (vlen * src.elemSize1()) - cols;
|
|
|
|
int offset = src.offset / src.elemSize1();
|
|
|
|
int repeat_me = vlen - (mask.cols % vlen == 0 ? vlen : mask.cols % vlen);
|
|
|
|
int minvalid_cols = mask.step / (vlen * mask.elemSize1()) - cols;
|
|
|
|
int moffset = mask.offset / mask.elemSize1();
|
|
|
|
int elemnum = cols * src.rows;
|
|
|
|
sprintf(build_options, "-D DEPTH_%d -D REPEAT_E%d", src.depth(), repeat_me);
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&invalid_cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&offset));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&elemnum));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&groupnum));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&minvalid_cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&moffset ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem) , (void *)&mask.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst ));
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
openCLExecuteKernel(src.clCxt, &arithm_minMaxLoc_mask, "arithm_op_minMaxLoc_mask", gt, lt, args, -1, -1, build_options);
|
|
|
|
}
|
|
|
|
}
|
2013-09-25 19:02:47 +08:00
|
|
|
|
|
|
|
template <typename T>
|
2012-07-17 01:08:14 +08:00
|
|
|
void arithmetic_minMaxLoc(const oclMat &src, double *minVal, double *maxVal,
|
2012-10-11 16:22:47 +08:00
|
|
|
Point *minLoc, Point *maxLoc, const oclMat &mask)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
CV_Assert(src.oclchannels() == 1);
|
2013-09-20 23:19:52 +08:00
|
|
|
size_t groupnum = src.clCxt->getDeviceInfo().maxComputeUnits;
|
2012-07-17 01:08:14 +08:00
|
|
|
CV_Assert(groupnum != 0);
|
|
|
|
int minloc = -1 , maxloc = -1;
|
2012-10-22 15:14:22 +08:00
|
|
|
int vlen = 4, dbsize = groupnum * vlen * 4 * sizeof(T) ;
|
2012-07-17 01:08:14 +08:00
|
|
|
Context *clCxt = src.clCxt;
|
2012-10-11 16:22:47 +08:00
|
|
|
cl_mem dstBuffer = openCLCreateBuffer(clCxt, CL_MEM_WRITE_ONLY, dbsize);
|
2012-07-17 01:08:14 +08:00
|
|
|
*minVal = std::numeric_limits<double>::max() , *maxVal = -std::numeric_limits<double>::max();
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
if (mask.empty())
|
|
|
|
arithmetic_minMaxLoc_run(src, dstBuffer, vlen, groupnum);
|
|
|
|
else
|
|
|
|
arithmetic_minMaxLoc_mask_run(src, mask, dstBuffer, vlen, groupnum);
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2013-09-25 19:28:12 +08:00
|
|
|
AutoBuffer<T> _buf(groupnum * vlen * 4);
|
|
|
|
T *p = (T*)_buf;
|
2012-07-17 01:08:14 +08:00
|
|
|
memset(p, 0, dbsize);
|
2013-09-25 19:28:12 +08:00
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
openCLReadBuffer(clCxt, dstBuffer, (void *)p, dbsize);
|
2013-09-25 19:02:47 +08:00
|
|
|
for (int i = 0; i < vlen * (int)groupnum; i++)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
*minVal = (*minVal < p[i] || p[i + 2 * vlen * groupnum] == -1) ? *minVal : p[i];
|
2012-10-22 15:14:22 +08:00
|
|
|
minloc = (*minVal < p[i] || p[i + 2 * vlen * groupnum] == -1) ? minloc : cvRound(p[i + 2 * vlen * groupnum]);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-25 19:02:47 +08:00
|
|
|
for (int i = vlen * (int)groupnum; i < 2 * vlen * (int)groupnum; i++)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
*maxVal = (*maxVal > p[i] || p[i + 2 * vlen * groupnum] == -1) ? *maxVal : p[i];
|
2012-10-22 15:14:22 +08:00
|
|
|
maxloc = (*maxVal > p[i] || p[i + 2 * vlen * groupnum] == -1) ? maxloc : cvRound(p[i + 2 * vlen * groupnum]);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int pre_rows = src.offset / src.step;
|
|
|
|
int pre_cols = (src.offset % src.step) / src.elemSize1();
|
|
|
|
int wholecols = src.step / src.elemSize1();
|
2013-09-25 19:02:47 +08:00
|
|
|
if ( minLoc )
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-25 19:02:47 +08:00
|
|
|
if ( minloc >= 0 )
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
|
|
|
minLoc->y = minloc / wholecols - pre_rows;
|
|
|
|
minLoc->x = minloc % wholecols - pre_cols;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
minLoc->x = minLoc->y = -1;
|
|
|
|
}
|
2013-09-25 19:02:47 +08:00
|
|
|
if ( maxLoc )
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-25 19:02:47 +08:00
|
|
|
if ( maxloc >= 0 )
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
|
|
|
maxLoc->y = maxloc / wholecols - pre_rows;
|
|
|
|
maxLoc->x = maxloc % wholecols - pre_cols;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
maxLoc->x = maxLoc->y = -1;
|
|
|
|
}
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
openCLSafeCall(clReleaseMemObject(dstBuffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef void (*minMaxLocFunc)(const oclMat &src, double *minVal, double *maxVal,
|
2012-10-11 16:22:47 +08:00
|
|
|
Point *minLoc, Point *maxLoc, const oclMat &mask);
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
void cv::ocl::minMaxLoc(const oclMat &src, double *minVal, double *maxVal,
|
|
|
|
Point *minLoc, Point *maxLoc, const oclMat &mask)
|
|
|
|
{
|
2013-09-20 23:19:52 +08:00
|
|
|
if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
2013-09-24 18:13:10 +08:00
|
|
|
return;
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
static minMaxLocFunc functab[2] =
|
|
|
|
{
|
|
|
|
arithmetic_minMaxLoc<float>,
|
|
|
|
arithmetic_minMaxLoc<double>
|
|
|
|
};
|
|
|
|
|
|
|
|
minMaxLocFunc func;
|
2013-09-20 23:19:52 +08:00
|
|
|
func = functab[(int)src.clCxt->supportsFeature(FEATURE_CL_DOUBLE)];
|
2012-10-11 16:22:47 +08:00
|
|
|
func(src, minVal, maxVal, minLoc, maxLoc, mask);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////// countNonZero ///////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2013-09-27 21:32:31 +08:00
|
|
|
static void arithmetic_countNonZero_run(const oclMat &src, cl_mem &dst, int groupnum, string kernelName)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-27 21:32:31 +08:00
|
|
|
int ochannels = src.oclchannels();
|
|
|
|
int all_cols = src.step / src.elemSize();
|
|
|
|
int pre_cols = (src.offset % src.step) / src.elemSize();
|
|
|
|
int sec_cols = all_cols - (src.offset % src.step + src.cols * src.elemSize() - 1) / src.elemSize() - 1;
|
2012-07-17 01:08:14 +08:00
|
|
|
int invalid_cols = pre_cols + sec_cols;
|
|
|
|
int cols = all_cols - invalid_cols , elemnum = cols * src.rows;;
|
2013-09-27 21:32:31 +08:00
|
|
|
int offset = src.offset / src.elemSize();
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-27 21:32:31 +08:00
|
|
|
const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
|
|
|
|
const char * const channelMap[] = { " ", " ", "2", "4", "4" };
|
|
|
|
string buildOptions = format("-D srcT=%s%s -D dstT=int%s", typeMap[src.depth()], channelMap[ochannels],
|
|
|
|
channelMap[ochannels]);
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-27 21:32:31 +08:00
|
|
|
vector<pair<size_t , const void *> > args;
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&invalid_cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&offset));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&elemnum));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int) , (void *)&groupnum));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst ));
|
2013-09-27 21:32:31 +08:00
|
|
|
|
|
|
|
size_t globalThreads[3] = { groupnum * 256, 1, 1 };
|
|
|
|
size_t localThreads[3] = { 256, 1, 1 };
|
|
|
|
|
|
|
|
openCLExecuteKernel(src.clCxt, &arithm_nonzero, kernelName, globalThreads, localThreads,
|
|
|
|
args, -1, -1, buildOptions.c_str());
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int cv::ocl::countNonZero(const oclMat &src)
|
|
|
|
{
|
2013-09-27 21:32:31 +08:00
|
|
|
CV_Assert(src.step % src.elemSize() == 0);
|
|
|
|
CV_Assert(src.channels() == 1);
|
|
|
|
|
|
|
|
Context *clCxt = src.clCxt;
|
2013-09-20 23:19:52 +08:00
|
|
|
if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "selected device doesn't support double");
|
|
|
|
return -1;
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-27 21:32:31 +08:00
|
|
|
|
2013-10-01 01:32:46 +08:00
|
|
|
size_t groupnum = src.clCxt->getDeviceInfo().maxComputeUnits;
|
2012-07-17 01:08:14 +08:00
|
|
|
CV_Assert(groupnum != 0);
|
2013-09-27 21:32:31 +08:00
|
|
|
int dbsize = groupnum;
|
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
string kernelName = "arithm_op_nonzero";
|
2013-09-25 19:28:12 +08:00
|
|
|
|
|
|
|
AutoBuffer<int> _buf(dbsize);
|
|
|
|
int *p = (int*)_buf, nonzero = 0;
|
2012-07-17 01:08:14 +08:00
|
|
|
memset(p, 0, dbsize * sizeof(int));
|
2013-09-27 21:32:31 +08:00
|
|
|
|
|
|
|
cl_mem dstBuffer = openCLCreateBuffer(clCxt, CL_MEM_WRITE_ONLY, dbsize * sizeof(int));
|
|
|
|
arithmetic_countNonZero_run(src, dstBuffer, groupnum, kernelName);
|
2012-10-11 16:22:47 +08:00
|
|
|
openCLReadBuffer(clCxt, dstBuffer, (void *)p, dbsize * sizeof(int));
|
2013-09-27 21:32:31 +08:00
|
|
|
|
2013-09-25 19:02:47 +08:00
|
|
|
for (int i = 0; i < dbsize; i++)
|
2012-07-17 01:08:14 +08:00
|
|
|
nonzero += p[i];
|
2013-09-24 18:13:10 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
openCLSafeCall(clReleaseMemObject(dstBuffer));
|
2013-09-27 21:32:31 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
return nonzero;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////bitwise_op////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2013-09-24 17:54:46 +08:00
|
|
|
|
2013-09-20 23:19:52 +08:00
|
|
|
static void bitwise_unary_run(const oclMat &src1, oclMat &dst, string kernelName, const cv::ocl::ProgramEntry* source)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
|
|
|
dst.create(src1.size(), src1.type());
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
int channels = dst.oclchannels();
|
2012-07-17 01:08:14 +08:00
|
|
|
int depth = dst.depth();
|
|
|
|
|
|
|
|
int vector_lengths[4][7] = {{4, 4, 4, 4, 1, 1, 1},
|
|
|
|
{4, 4, 4, 4, 1, 1, 1},
|
|
|
|
{4, 4, 4, 4, 1, 1, 1},
|
|
|
|
{4, 4, 4, 4, 1, 1, 1}
|
|
|
|
};
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
size_t vector_length = vector_lengths[channels - 1][depth];
|
2012-07-17 01:08:14 +08:00
|
|
|
int offset_cols = (dst.offset / dst.elemSize1()) & (vector_length - 1);
|
|
|
|
int cols = divUp(dst.cols * channels + offset_cols, vector_length);
|
|
|
|
|
|
|
|
size_t localThreads[3] = { 64, 4, 1 };
|
2013-09-16 19:11:56 +08:00
|
|
|
size_t globalThreads[3] = { cols, dst.rows, 1 };
|
2012-07-17 01:08:14 +08:00
|
|
|
|
|
|
|
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 *)&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 *)&cols ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst_step1 ));
|
|
|
|
|
2013-10-09 22:05:09 +08:00
|
|
|
openCLExecuteKernel(src1.clCxt, source, kernelName, globalThreads, localThreads, args, -1, depth);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
2013-09-24 17:54:46 +08:00
|
|
|
enum { AND = 0, OR, XOR };
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:54:46 +08:00
|
|
|
static void bitwise_binary_run(const oclMat &src1, const oclMat &src2, const Scalar& src3, const oclMat &mask,
|
|
|
|
oclMat &dst, int operationType)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-24 17:54:46 +08:00
|
|
|
CV_Assert(operationType >= AND && operationType <= XOR);
|
|
|
|
CV_Assert(src2.empty() || (!src2.empty() && src1.type() == src2.type() && src1.size() == src2.size()));
|
|
|
|
CV_Assert(mask.empty() || (!mask.empty() && mask.type() == CV_8UC1 && mask.size() == src1.size()));
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:54:46 +08:00
|
|
|
dst.create(src1.size(), src1.type());
|
|
|
|
oclMat m;
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:54:46 +08:00
|
|
|
const char operationMap[] = { '&', '|', '^' };
|
|
|
|
std::string kernelName("arithm_bitwise_binary");
|
2013-10-12 04:58:58 +08:00
|
|
|
|
|
|
|
int vlen = std::min<int>(8, src1.elemSize1() * src1.oclchannels());
|
2013-10-15 17:12:36 +08:00
|
|
|
std::string vlenstr = vlen > 1 ? format("%d", vlen) : "";
|
2013-10-12 04:58:58 +08:00
|
|
|
std::string buildOptions = format("-D Operation=%c -D vloadn=vload%s -D vstoren=vstore%s -D elemSize=%d -D vlen=%d"
|
|
|
|
" -D ucharv=uchar%s",
|
|
|
|
operationMap[operationType], vlenstr.c_str(), vlenstr.c_str(),
|
|
|
|
(int)src1.elemSize(), vlen, vlenstr.c_str());
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:54:46 +08:00
|
|
|
size_t localThreads[3] = { 16, 16, 1 };
|
2013-10-12 04:58:58 +08:00
|
|
|
size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
|
2012-07-17 01:08:14 +08:00
|
|
|
|
|
|
|
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 ));
|
|
|
|
|
2013-09-24 17:54:46 +08:00
|
|
|
if (src2.empty())
|
2013-01-25 18:31:34 +08:00
|
|
|
{
|
2013-09-24 17:54:46 +08:00
|
|
|
m.create(1, 1, dst.type());
|
|
|
|
m.setTo(src3);
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:54:46 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&m.data ));
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:54:46 +08:00
|
|
|
kernelName += "_scalar";
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-24 17:54:46 +08:00
|
|
|
else
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-24 17:54:46 +08:00
|
|
|
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 ));
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
2013-09-24 17:54:46 +08:00
|
|
|
if (!mask.empty())
|
|
|
|
{
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&mask.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&mask.step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&mask.offset ));
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:54:46 +08:00
|
|
|
kernelName += "_mask";
|
|
|
|
}
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:54:46 +08:00
|
|
|
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 ));
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-10-12 04:58:58 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1.cols ));
|
2013-09-24 17:54:46 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1.rows ));
|
|
|
|
|
2013-10-09 22:05:09 +08:00
|
|
|
openCLExecuteKernel(src1.clCxt, mask.empty() ? (!src2.empty() ? &arithm_bitwise_binary : &arithm_bitwise_binary_scalar) :
|
2013-09-24 17:54:46 +08:00
|
|
|
(!src2.empty() ? &arithm_bitwise_binary_mask : &arithm_bitwise_binary_scalar_mask),
|
|
|
|
kernelName, globalThreads, localThreads,
|
|
|
|
args, -1, -1, buildOptions.c_str());
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cv::ocl::bitwise_not(const oclMat &src, oclMat &dst)
|
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-10-12 04:58:58 +08:00
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
2012-07-17 01:08:14 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-09-24 17:54:46 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
dst.create(src.size(), src.type());
|
2013-10-12 04:58:58 +08:00
|
|
|
bitwise_unary_run(src, dst, "arithm_bitwise_not", &arithm_bitwise_not);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cv::ocl::bitwise_or(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask)
|
|
|
|
{
|
2013-09-24 17:54:46 +08:00
|
|
|
bitwise_binary_run(src1, src2, Scalar(), mask, dst, OR);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cv::ocl::bitwise_or(const oclMat &src1, const Scalar &src2, oclMat &dst, const oclMat &mask)
|
|
|
|
{
|
2013-09-24 17:54:46 +08:00
|
|
|
bitwise_binary_run(src1, oclMat(), src2, mask, dst, OR);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cv::ocl::bitwise_and(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask)
|
|
|
|
{
|
2013-09-24 17:54:46 +08:00
|
|
|
bitwise_binary_run(src1, src2, Scalar(), mask, dst, AND);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cv::ocl::bitwise_and(const oclMat &src1, const Scalar &src2, oclMat &dst, const oclMat &mask)
|
|
|
|
{
|
2013-09-24 17:54:46 +08:00
|
|
|
bitwise_binary_run(src1, oclMat(), src2, mask, dst, AND);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cv::ocl::bitwise_xor(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask)
|
|
|
|
{
|
2013-09-24 17:54:46 +08:00
|
|
|
bitwise_binary_run(src1, src2, Scalar(), mask, dst, XOR);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cv::ocl::bitwise_xor(const oclMat &src1, const Scalar &src2, oclMat &dst, const oclMat &mask)
|
|
|
|
{
|
2013-09-24 17:54:46 +08:00
|
|
|
bitwise_binary_run(src1, oclMat(), src2, mask, dst, XOR);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
2013-02-22 04:30:27 +08:00
|
|
|
oclMat cv::ocl::operator ~ (const oclMat &src)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-02-16 18:41:43 +08:00
|
|
|
return oclMatExpr(src, oclMat(), MAT_NOT);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
2013-02-22 04:30:27 +08:00
|
|
|
oclMat cv::ocl::operator | (const oclMat &src1, const oclMat &src2)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-02-16 18:41:43 +08:00
|
|
|
return oclMatExpr(src1, src2, MAT_OR);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
2013-02-22 04:30:27 +08:00
|
|
|
oclMat cv::ocl::operator & (const oclMat &src1, const oclMat &src2)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-02-16 18:41:43 +08:00
|
|
|
return oclMatExpr(src1, src2, MAT_AND);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
2013-02-22 04:30:27 +08:00
|
|
|
oclMat cv::ocl::operator ^ (const oclMat &src1, const oclMat &src2)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-02-16 18:41:43 +08:00
|
|
|
return oclMatExpr(src1, src2, MAT_XOR);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
2013-02-16 11:05:23 +08:00
|
|
|
cv::ocl::oclMatExpr cv::ocl::operator + (const oclMat &src1, const oclMat &src2)
|
2013-02-08 11:41:46 +08:00
|
|
|
{
|
2013-02-16 18:41:43 +08:00
|
|
|
return oclMatExpr(src1, src2, cv::ocl::MAT_ADD);
|
2013-02-08 11:41:46 +08:00
|
|
|
}
|
|
|
|
|
2013-02-16 11:05:23 +08:00
|
|
|
cv::ocl::oclMatExpr cv::ocl::operator - (const oclMat &src1, const oclMat &src2)
|
2013-02-08 11:41:46 +08:00
|
|
|
{
|
2013-02-16 18:41:43 +08:00
|
|
|
return oclMatExpr(src1, src2, cv::ocl::MAT_SUB);
|
2013-02-08 11:41:46 +08:00
|
|
|
}
|
|
|
|
|
2013-02-16 11:05:23 +08:00
|
|
|
cv::ocl::oclMatExpr cv::ocl::operator * (const oclMat &src1, const oclMat &src2)
|
2013-02-08 11:41:46 +08:00
|
|
|
{
|
2013-02-16 18:41:43 +08:00
|
|
|
return oclMatExpr(src1, src2, cv::ocl::MAT_MUL);
|
2013-02-08 11:41:46 +08:00
|
|
|
}
|
|
|
|
|
2013-02-16 11:05:23 +08:00
|
|
|
cv::ocl::oclMatExpr cv::ocl::operator / (const oclMat &src1, const oclMat &src2)
|
2013-02-08 11:41:46 +08:00
|
|
|
{
|
2013-02-16 18:41:43 +08:00
|
|
|
return oclMatExpr(src1, src2, cv::ocl::MAT_DIV);
|
2013-02-08 11:41:46 +08:00
|
|
|
}
|
|
|
|
|
2013-02-16 11:05:23 +08:00
|
|
|
void oclMatExpr::assign(oclMat& m) const
|
|
|
|
{
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case MAT_ADD:
|
|
|
|
add(a, b, m);
|
|
|
|
break;
|
|
|
|
case MAT_SUB:
|
|
|
|
subtract(a, b, m);
|
|
|
|
break;
|
|
|
|
case MAT_MUL:
|
|
|
|
multiply(a, b, m);
|
|
|
|
break;
|
|
|
|
case MAT_DIV:
|
|
|
|
divide(a, b, m);
|
|
|
|
break;
|
2013-02-16 18:41:43 +08:00
|
|
|
case MAT_NOT:
|
|
|
|
bitwise_not(a, m);
|
|
|
|
break;
|
|
|
|
case MAT_AND:
|
|
|
|
bitwise_and(a, b, m);
|
|
|
|
break;
|
|
|
|
case MAT_OR:
|
|
|
|
bitwise_or(a, b, m);
|
|
|
|
break;
|
|
|
|
case MAT_XOR:
|
|
|
|
bitwise_xor(a, b, m);
|
|
|
|
break;
|
2013-02-16 11:05:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
oclMatExpr::operator oclMat() const
|
|
|
|
{
|
|
|
|
oclMat m;
|
|
|
|
assign(m);
|
|
|
|
return m;
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
/////////////////////////////// transpose ////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2013-09-24 18:07:54 +08:00
|
|
|
|
2013-09-30 19:08:26 +08:00
|
|
|
#define TILE_DIM (32)
|
|
|
|
#define BLOCK_ROWS (256 / TILE_DIM)
|
2013-09-24 18:07:54 +08:00
|
|
|
|
2013-09-25 02:54:13 +08:00
|
|
|
static void transpose_run(const oclMat &src, oclMat &dst, string kernelName, bool inplace = false)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-09-24 18:07:54 +08:00
|
|
|
const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
|
|
|
|
const char channelsString[] = { ' ', ' ', '2', '4', '4' };
|
|
|
|
std::string buildOptions = format("-D T=%s%c", typeMap[src.depth()],
|
|
|
|
channelsString[src.channels()]);
|
2012-07-17 01:08:14 +08:00
|
|
|
|
|
|
|
size_t localThreads[3] = { TILE_DIM, BLOCK_ROWS, 1 };
|
2013-09-25 02:54:13 +08:00
|
|
|
size_t globalThreads[3] = { src.cols, inplace ? src.rows : divUp(src.rows, TILE_DIM) * BLOCK_ROWS, 1 };
|
2013-09-24 18:07:54 +08:00
|
|
|
|
|
|
|
int srcstep1 = src.step / src.elemSize(), dststep1 = dst.step / dst.elemSize();
|
|
|
|
int srcoffset1 = src.offset / src.elemSize(), dstoffset1 = dst.offset / dst.elemSize();
|
2012-07-17 01:08:14 +08:00
|
|
|
|
|
|
|
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_mem), (void *)&dst.data ));
|
2013-09-24 18:07:54 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src.cols ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src.rows ));
|
2013-09-24 18:07:54 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&srcstep1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dststep1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&srcoffset1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dstoffset1 ));
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-10-09 22:05:09 +08:00
|
|
|
openCLExecuteKernel(src.clCxt, &arithm_transpose, kernelName, globalThreads, localThreads,
|
2013-09-24 18:07:54 +08:00
|
|
|
args, -1, -1, buildOptions.c_str());
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cv::ocl::transpose(const oclMat &src, oclMat &dst)
|
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
|
|
|
|
{
|
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
|
|
|
return;
|
|
|
|
}
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 18:07:54 +08:00
|
|
|
if ( src.data == dst.data && src.cols == src.rows && dst.offset == src.offset
|
2013-09-25 02:54:13 +08:00
|
|
|
&& dst.size() == src.size())
|
|
|
|
transpose_run( src, dst, "transpose_inplace", true);
|
2012-07-17 01:08:14 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
dst.create(src.cols, src.rows, src.type());
|
|
|
|
transpose_run( src, dst, "transpose");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-24 18:07:54 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////// addWeighted ///////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
void cv::ocl::addWeighted(const oclMat &src1, double alpha, const oclMat &src2, double beta, double gama, oclMat &dst)
|
|
|
|
{
|
2013-09-24 17:51:37 +08:00
|
|
|
Context *clCxt = src1.clCxt;
|
2013-09-20 23:19:52 +08:00
|
|
|
bool hasDouble = clCxt->supportsFeature(FEATURE_CL_DOUBLE);
|
2013-09-24 17:51:37 +08:00
|
|
|
if (!hasDouble && src1.depth() == CV_64F)
|
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
2013-09-24 17:51:37 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CV_Assert(src1.size() == src2.size() && src1.type() == src2.type());
|
2012-07-17 01:08:14 +08:00
|
|
|
dst.create(src1.size(), src1.type());
|
|
|
|
|
2012-10-11 16:22:47 +08:00
|
|
|
int channels = dst.oclchannels();
|
2012-07-17 01:08:14 +08:00
|
|
|
int depth = dst.depth();
|
|
|
|
|
2013-09-24 17:51:37 +08:00
|
|
|
int cols1 = src1.cols * channels;
|
|
|
|
int src1step1 = src1.step1(), src1offset1 = src1.offset / src1.elemSize1();
|
|
|
|
int src2step1 = src2.step1(), src2offset1 = src2.offset / src1.elemSize1();
|
|
|
|
int dststep1 = dst.step1(), dstoffset1 = dst.offset / dst.elemSize1();
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-09-24 17:51:37 +08:00
|
|
|
const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
|
|
|
|
std::string buildOptions = format("-D T=%s -D WT=%s -D convertToT=convert_%s%s",
|
|
|
|
typeMap[depth], hasDouble ? "double" : "float", typeMap[depth],
|
|
|
|
depth >= CV_32F ? "" : "_sat_rte");
|
2012-07-17 01:08:14 +08:00
|
|
|
|
|
|
|
size_t localThreads[3] = { 256, 1, 1 };
|
2013-09-24 17:51:37 +08:00
|
|
|
size_t globalThreads[3] = { cols1, dst.rows, 1};
|
|
|
|
|
|
|
|
float alpha_f = static_cast<float>(alpha),
|
|
|
|
beta_f = static_cast<float>(beta),
|
|
|
|
gama_f = static_cast<float>(gama);
|
2012-07-17 01:08:14 +08:00
|
|
|
|
|
|
|
vector<pair<size_t , const void *> > args;
|
2012-08-03 14:08:36 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src1.data ));
|
2013-09-24 17:51:37 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1step1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1offset1));
|
2012-08-03 14:08:36 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src2.data ));
|
2013-09-24 17:51:37 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src2step1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src2offset1));
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dststep1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dstoffset1));
|
2012-08-03 14:08:36 +08:00
|
|
|
|
2013-09-24 17:51:37 +08:00
|
|
|
if (!hasDouble)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2012-09-21 16:51:45 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_float), (void *)&alpha_f ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_float), (void *)&beta_f ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_float), (void *)&gama_f ));
|
2012-10-11 16:22:47 +08:00
|
|
|
}
|
2013-09-24 17:51:37 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
args.push_back( make_pair( sizeof(cl_double), (void *)&alpha ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_double), (void *)&beta ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_double), (void *)&gama ));
|
|
|
|
}
|
2012-08-03 14:08:36 +08:00
|
|
|
|
2013-09-24 17:51:37 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&cols1 ));
|
2012-08-03 14:08:36 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src1.rows ));
|
|
|
|
|
2013-09-24 17:51:37 +08:00
|
|
|
openCLExecuteKernel(clCxt, &arithm_addWeighted, "addWeighted", globalThreads, localThreads,
|
|
|
|
args, -1, -1, buildOptions.c_str());
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
|
|
|
|
2013-09-24 18:07:54 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
/////////////////////////////////// Pow //////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-11-12 20:50:42 +08:00
|
|
|
static void arithmetic_pow_run(const oclMat &src, double p, oclMat &dst, string kernelName, const cv::ocl::ProgramEntry* source)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2012-10-11 16:22:47 +08:00
|
|
|
int channels = dst.oclchannels();
|
2012-07-17 01:08:14 +08:00
|
|
|
int depth = dst.depth();
|
|
|
|
|
|
|
|
size_t localThreads[3] = { 64, 4, 1 };
|
2013-11-12 20:50:42 +08:00
|
|
|
size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
|
|
|
|
|
|
|
|
const char * const channelMap[] = { "", "", "2", "4", "4" };
|
|
|
|
std::string buildOptions = format("-D T=%s%s", depth == CV_32F ? "float" : "double", channelMap[channels]);
|
|
|
|
|
|
|
|
int src_step = src.step / src.elemSize(), src_offset = src.offset / src.elemSize();
|
|
|
|
int dst_step = dst.step / dst.elemSize(), dst_offset = dst.offset / dst.elemSize();
|
2012-07-17 01:08:14 +08:00
|
|
|
|
|
|
|
vector<pair<size_t , const void *> > args;
|
2013-11-12 20:50:42 +08:00
|
|
|
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_offset ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data ));
|
2013-11-12 20:50:42 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst_step ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst_offset ));
|
2012-07-17 01:08:14 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst.rows ));
|
2013-11-12 20:50:42 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&dst.cols ));
|
2013-09-24 18:13:10 +08:00
|
|
|
|
|
|
|
float pf = static_cast<float>(p);
|
2013-11-12 20:50:42 +08:00
|
|
|
if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
|
2012-10-11 16:22:47 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_float), (void *)&pf ));
|
2012-09-21 16:51:45 +08:00
|
|
|
else
|
2012-10-11 16:22:47 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_double), (void *)&p ));
|
2012-07-17 01:08:14 +08:00
|
|
|
|
2013-11-12 20:50:42 +08:00
|
|
|
openCLExecuteKernel(src.clCxt, source, kernelName, globalThreads, localThreads, args, -1, -1, buildOptions.c_str());
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-24 18:07:54 +08:00
|
|
|
|
2012-07-17 01:08:14 +08:00
|
|
|
void cv::ocl::pow(const oclMat &x, double p, oclMat &y)
|
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
if (!x.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && x.depth() == CV_64F)
|
2012-07-17 01:08:14 +08:00
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
2012-07-17 01:08:14 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-22 21:38:55 +08:00
|
|
|
CV_Assert(x.depth() == CV_32F || x.depth() == CV_64F);
|
2012-07-17 01:08:14 +08:00
|
|
|
y.create(x.size(), x.type());
|
|
|
|
|
2013-10-09 22:05:09 +08:00
|
|
|
arithmetic_pow_run(x, p, y, "arithm_pow", &arithm_pow);
|
2012-07-17 01:08:14 +08:00
|
|
|
}
|
2013-09-24 18:07:54 +08:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
/////////////////////////////// setIdentity //////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-09-30 20:54:09 +08:00
|
|
|
void cv::ocl::setIdentity(oclMat& src, const Scalar & scalar)
|
2013-09-09 16:08:23 +08:00
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
|
2013-09-09 16:08:23 +08:00
|
|
|
{
|
2013-10-09 22:05:09 +08:00
|
|
|
CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
|
2013-09-30 20:54:09 +08:00
|
|
|
return;
|
2013-09-09 16:08:23 +08:00
|
|
|
}
|
|
|
|
|
2013-09-30 20:54:09 +08:00
|
|
|
CV_Assert(src.step % src.elemSize() == 0);
|
|
|
|
|
|
|
|
int src_step1 = src.step / src.elemSize(), src_offset1 = src.offset / src.elemSize();
|
|
|
|
size_t local_threads[] = { 16, 16, 1 };
|
|
|
|
size_t global_threads[] = { src.cols, src.rows, 1 };
|
|
|
|
|
|
|
|
const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
|
|
|
|
const char * const channelMap[] = { "", "", "2", "4", "4" };
|
|
|
|
string buildOptions = format("-D T=%s%s", typeMap[src.depth()], channelMap[src.oclchannels()]);
|
|
|
|
|
2013-09-09 16:08:23 +08:00
|
|
|
vector<pair<size_t , const void *> > args;
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&src.data ));
|
2013-09-30 20:54:09 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src_step1 ));
|
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src_offset1 ));
|
2013-09-09 16:08:23 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src.cols));
|
2013-09-30 20:54:09 +08:00
|
|
|
args.push_back( make_pair( sizeof(cl_int), (void *)&src.rows));
|
2013-09-09 16:08:23 +08:00
|
|
|
|
2013-09-30 20:54:09 +08:00
|
|
|
oclMat sc(1, 1, src.type(), scalar);
|
|
|
|
args.push_back( make_pair( sizeof(cl_mem), (void *)&sc.data ));
|
2013-09-09 16:08:23 +08:00
|
|
|
|
2013-10-09 22:05:09 +08:00
|
|
|
openCLExecuteKernel(src.clCxt, &arithm_setidentity, "setIdentity", global_threads, local_threads,
|
2013-09-30 20:54:09 +08:00
|
|
|
args, -1, -1, buildOptions.c_str());
|
2013-09-09 16:08:23 +08:00
|
|
|
}
|
2013-11-05 03:59:56 +08:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
////////////////////////////////// Repeat ////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void cv::ocl::repeat(const oclMat & src, int ny, int nx, oclMat & dst)
|
|
|
|
{
|
|
|
|
CV_Assert(nx > 0 && ny > 0);
|
|
|
|
dst.create(src.rows * ny, src.cols * nx, src.type());
|
|
|
|
|
|
|
|
for (int y = 0; y < ny; ++y)
|
|
|
|
for (int x = 0; x < nx; ++x)
|
|
|
|
{
|
|
|
|
Rect roi(x * src.cols, y * src.rows, src.cols, src.rows);
|
|
|
|
oclMat hdr = dst(roi);
|
|
|
|
src.copyTo(hdr);
|
|
|
|
}
|
|
|
|
}
|