This source file includes following definitions.
- dst
- ocl_blendLinear
- blendLinear
#include "precomp.hpp"
#include "opencl_kernels_imgproc.hpp"
namespace cv {
template <typename T>
class BlendLinearInvoker :
public ParallelLoopBody
{
public:
BlendLinearInvoker(const Mat & _src1, const Mat & _src2, const Mat & _weights1,
const Mat & _weights2, Mat & _dst) :
src1(&_src1), src2(&_src2), weights1(&_weights1), weights2(&_weights2), dst(&_dst)
{
}
virtual void operator() (const Range & range) const
{
int cn = src1->channels(), width = src1->cols * cn;
for (int y = range.start; y < range.end; ++y)
{
const float * const weights1_row = weights1->ptr<float>(y);
const float * const weights2_row = weights2->ptr<float>(y);
const T * const src1_row = src1->ptr<T>(y);
const T * const src2_row = src2->ptr<T>(y);
T * const dst_row = dst->ptr<T>(y);
for (int x = 0; x < width; ++x)
{
int x1 = x / cn;
float w1 = weights1_row[x1], w2 = weights2_row[x1];
float den = (w1 + w2 + 1e-5f);
float num = (src1_row[x] * w1 + src2_row[x] * w2);
dst_row[x] = saturate_cast<T>(num / den);
}
}
}
private:
const BlendLinearInvoker & operator= (const BlendLinearInvoker &);
BlendLinearInvoker(const BlendLinearInvoker &);
const Mat * src1, * src2, * weights1, * weights2;
Mat * dst;
};
#ifdef HAVE_OPENCL
static bool ocl_blendLinear( InputArray _src1, InputArray _src2, InputArray _weights1, InputArray _weights2, OutputArray _dst )
{
int type = _src1.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
char cvt[30];
ocl::Kernel k("blendLinear", ocl::imgproc::blend_linear_oclsrc,
format("-D T=%s -D cn=%d -D convertToT=%s", ocl::typeToStr(depth),
cn, ocl::convertTypeStr(CV_32F, depth, 1, cvt)));
if (k.empty())
return false;
UMat src1 = _src1.getUMat(), src2 = _src2.getUMat(), weights1 = _weights1.getUMat(),
weights2 = _weights2.getUMat(), dst = _dst.getUMat();
k.args(ocl::KernelArg::ReadOnlyNoSize(src1), ocl::KernelArg::ReadOnlyNoSize(src2),
ocl::KernelArg::ReadOnlyNoSize(weights1), ocl::KernelArg::ReadOnlyNoSize(weights2),
ocl::KernelArg::WriteOnly(dst));
size_t globalsize[2] = { dst.cols, dst.rows };
return k.run(2, globalsize, NULL, false);
}
#endif
}
void cv::blendLinear( InputArray _src1, InputArray _src2, InputArray _weights1, InputArray _weights2, OutputArray _dst )
{
int type = _src1.type(), depth = CV_MAT_DEPTH(type);
Size size = _src1.size();
CV_Assert(depth == CV_8U || depth == CV_32F);
CV_Assert(size == _src2.size() && size == _weights1.size() && size == _weights2.size());
CV_Assert(type == _src2.type() && _weights1.type() == CV_32FC1 && _weights2.type() == CV_32FC1);
_dst.create(size, type);
CV_OCL_RUN(_dst.isUMat(),
ocl_blendLinear(_src1, _src2, _weights1, _weights2, _dst))
Mat src1 = _src1.getMat(), src2 = _src2.getMat(), weights1 = _weights1.getMat(),
weights2 = _weights2.getMat(), dst = _dst.getMat();
if (depth == CV_8U)
{
BlendLinearInvoker<uchar> invoker(src1, src2, weights1, weights2, dst);
parallel_for_(Range(0, src1.rows), invoker, dst.total()/(double)(1<<16));
}
else if (depth == CV_32F)
{
BlendLinearInvoker<float> invoker(src1, src2, weights1, weights2, dst);
parallel_for_(Range(0, src1.rows), invoker, dst.total()/(double)(1<<16));
}
}