Commit 55af7857 authored by Ilya Lavrenov's avatar Ilya Lavrenov

added cv::warpPerspective to T-API

parent 90c23067
......@@ -286,7 +286,7 @@ public:
Kernel();
Kernel(const char* kname, const Program& prog);
Kernel(const char* kname, const ProgramSource2& prog,
const String& buildopts, String* errmsg=0);
const String& buildopts = String(), String* errmsg=0);
~Kernel();
Kernel(const Kernel& k);
Kernel& operator = (const Kernel& k);
......
......@@ -1893,7 +1893,7 @@ Context2& Context2::getDefault()
// First, try to retrieve existing context of the same type.
// In its turn, Platform::getContext() may call Context2::create()
// if there is no such context.
ctx.create(Device::TYPE_ACCELERATOR);
ctx.create(Device::TYPE_CPU);
if(!ctx.p)
ctx.create(Device::TYPE_DGPU);
if(!ctx.p)
......@@ -2041,6 +2041,7 @@ struct Kernel::Impl
cl_int retval = 0;
handle = ph != 0 ?
clCreateKernel(ph, kname, &retval) : 0;
printf("kernel creation error code: %d\n", retval);
for( int i = 0; i < MAX_ARRS; i++ )
u[i] = 0;
haveTempDstUMats = false;
......@@ -2218,7 +2219,7 @@ int Kernel::set(int i, const KernelArg& arg)
else if( arg.m->dims <= 2 )
{
UMat2D u2d(*arg.m);
clSetKernelArg(p->handle, (cl_uint)i, sizeof(h), &h);
clSetKernelArg(p->handle, (cl_uint)i, sizeof(h), &h));
clSetKernelArg(p->handle, (cl_uint)(i+1), sizeof(u2d.step), &u2d.step);
clSetKernelArg(p->handle, (cl_uint)(i+2), sizeof(u2d.offset), &u2d.offset);
i += 3;
......
......@@ -4030,16 +4030,76 @@ private:
};
#endif
static bool ocl_warpPerspective(InputArray _src, OutputArray _dst, InputArray _M0,
Size dsize, int flags, int borderType, const Scalar& borderValue)
{
int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type), wdepth = depth;
double doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0;
int interpolation = flags & INTER_MAX;
if( interpolation == INTER_AREA )
interpolation = INTER_LINEAR;
if ( !(borderType == cv::BORDER_CONSTANT &&
(interpolation == cv::INTER_NEAREST || interpolation == cv::INTER_LINEAR || interpolation == cv::INTER_CUBIC)) ||
(!doubleSupport && depth == CV_64F) || cn > 4 || cn == 3)
return false;
UMat src = _src.getUMat(), M0;
_dst.create( dsize.area() == 0 ? src.size() : dsize, src.type() );
UMat dst = _dst.getUMat();
double M[9];
Mat matM(3, 3, doubleSupport ? CV_64F : CV_32F, M), M1 = _M0.getMat();
CV_Assert( (M1.type() == CV_32F || M1.type() == CV_64F) && M1.rows == 3 && M1.cols == 3 );
M1.convertTo(matM, matM.type());
if( !(flags & WARP_INVERSE_MAP) )
invert(matM, matM);
matM.copyTo(M0);
const char * const interpolationMap[3] = { "NEAREST", "LINEAR", "CUBIC" };
ocl::Kernel k;
if (interpolation == INTER_NEAREST)
{
k.create("warpPerspective", ocl::imgproc::warp_perspective_oclsrc,
format("-D INTER_NEAREST -D T=%s%s", ocl::typeToStr(type),
doubleSupport ? " -D DOUBLE_SUPPORT" : ""));
}
else
{
char cvt[2][50];
wdepth = std::max(CV_32S, depth);
k.create("warpPerspective", ocl::imgproc::warp_perspective_oclsrc,
format("-D INTER_%s -D T=%s -D WT=%s -D depth=%d -D convertToWT=%s -D convertToT=%s%s",
interpolationMap[interpolation], ocl::typeToStr(type),
ocl::typeToStr(CV_MAKE_TYPE(wdepth, cn)), depth,
ocl::convertTypeStr(depth, wdepth, cn, cvt[0]),
ocl::convertTypeStr(wdepth, depth, cn, cvt[1]),
doubleSupport ? " -D DOUBLE_SUPPORT" : ""));
}
k.args(ocl::KernelArg::ReadOnly(src), ocl::KernelArg::WriteOnly(dst),
ocl::KernelArg::PtrOnly(M0), ocl::KernelArg::Constant(Mat(1, 1, CV_MAKE_TYPE(wdepth, cn), borderValue)));
size_t globalThreads[2] = { dst.cols, dst.rows };
return k.run(2, globalThreads, NULL, false);
}
}
void cv::warpPerspective( InputArray _src, OutputArray _dst, InputArray _M0,
Size dsize, int flags, int borderType, const Scalar& borderValue )
{
CV_Assert( _src.total() > 0 );
if (ocl::useOpenCL() && _dst.isUMat() && ocl_warpPerspective(_src, _dst, _M0, dsize, flags, borderType, borderValue))
return;
Mat src = _src.getMat(), M0 = _M0.getMat();
_dst.create( dsize.area() == 0 ? src.size() : dsize, src.type() );
Mat dst = _dst.getMat();
CV_Assert( src.cols > 0 && src.rows > 0 );
if( dst.data == src.data )
src = src.clone();
......
This diff is collapsed.
/*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.
// Third party copyrights are property of their respective owners.
//
// @Authors
// Zhang Ying, zhangying913@gmail.com
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors as is and
// any express or 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*/
#ifdef DOUBLE_SUPPORT
#ifdef cl_amd_fp64
#pragma OPENCL EXTENSION cl_amd_fp64:enable
#elif defined (cl_khr_fp64)
#pragma OPENCL EXTENSION cl_khr_fp64:enable
#endif
#define CT double
#else
#define CT float
#endif
#define INTER_BITS 5
#define INTER_TAB_SIZE (1 << INTER_BITS)
#define INTER_SCALE 1.f / INTER_TAB_SIZE
#define AB_BITS max(10, (int)INTER_BITS)
#define AB_SCALE (1 << AB_BITS)
#define INTER_REMAP_COEF_BITS 15
#define INTER_REMAP_COEF_SCALE (1 << INTER_REMAP_COEF_BITS)
#define noconvert
#ifdef INTER_NEAREST
__kernel void warpPerspective(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols,
__global uchar * dstptr, int dst_step, int dst_offset, int dst_rows, int dst_cols,
__constant CT * M, T scalar)
{
int dx = get_global_id(0);
int dy = get_global_id(1);
if (dx < dst_cols && dy < dst_rows)
{
CT X0 = M[0] * dx + M[1] * dy + M[2];
CT Y0 = M[3] * dx + M[4] * dy + M[5];
CT W = M[6] * dx + M[7] * dy + M[8];
W = W != 0.0f ? 1.f / W : 0.0f;
short sx = convert_short_sat_rte(X0*W);
short sy = convert_short_sat_rte(Y0*W);
int dst_index = mad24(dy, dst_step, dx * (int)sizeof(T) + dst_offset);
__global T * dst = (__global T *)(dstptr + dst_index);
if (sx >= 0 && sx < src_cols && sy >= 0 && sy < src_rows)
{
int src_index = mad24(sy, src_step, sx * (int)sizeof(T) + src_offset);
__global const T * src = (__global const T *)(srcptr + src_index);
dst[0] = src[0];
}
else
dst[0] = scalar;
}
}
#elif defined INTER_LINEAR
__kernel void warpPerspective(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols,
__global uchar * dstptr, int dst_step, int dst_offset, int dst_rows, int dst_cols,
__constant CT * M, WT scalar)
{
int dx = get_global_id(0);
int dy = get_global_id(1);
if (dx < dst_cols && dy < dst_rows)
{
CT X0 = M[0] * dx + M[1] * dy + M[2];
CT Y0 = M[3] * dx + M[4] * dy + M[5];
CT W = M[6] * dx + M[7] * dy + M[8];
W = W != 0.0f ? INTER_TAB_SIZE / W : 0.0f;
int X = rint(X0 * W), Y = rint(Y0 * W);
short sx = convert_short_sat(X >> INTER_BITS);
short sy = convert_short_sat(Y >> INTER_BITS);
short ay = (short)(Y & (INTER_TAB_SIZE - 1));
short ax = (short)(X & (INTER_TAB_SIZE - 1));
WT v0 = (sx >= 0 && sx < src_cols && sy >= 0 && sy < src_rows) ?
convertToWT(*(__global const T *)(srcptr + mad24(sy, src_step, src_offset + sx * (int)sizeof(T)))) : scalar;
WT v1 = (sx+1 >= 0 && sx+1 < src_cols && sy >= 0 && sy < src_rows) ?
convertToWT(*(__global const T *)(srcptr + mad24(sy, src_step, src_offset + (sx+1) * (int)sizeof(T)))) : scalar;
WT v2 = (sx >= 0 && sx < src_cols && sy+1 >= 0 && sy+1 < src_rows) ?
convertToWT(*(__global const T *)(srcptr + mad24(sy+1, src_step, src_offset + sx * (int)sizeof(T)))) : scalar;
WT v3 = (sx+1 >= 0 && sx+1 < src_cols && sy+1 >= 0 && sy+1 < src_rows) ?
convertToWT(*(__global const T *)(srcptr + mad24(sy+1, src_step, src_offset + (sx+1) * (int)sizeof(T)))) : scalar;
float taby = 1.f/INTER_TAB_SIZE*ay;
float tabx = 1.f/INTER_TAB_SIZE*ax;
int dst_index = mad24(dy, dst_step, dst_offset + dx * (int)sizeof(T));
__global T * dst = (__global T *)(dstptr + dst_index);
#if depth <= 4
int itab0 = convert_short_sat_rte( (1.0f-taby)*(1.0f-tabx) * INTER_REMAP_COEF_SCALE );
int itab1 = convert_short_sat_rte( (1.0f-taby)*tabx * INTER_REMAP_COEF_SCALE );
int itab2 = convert_short_sat_rte( taby*(1.0f-tabx) * INTER_REMAP_COEF_SCALE );
int itab3 = convert_short_sat_rte( taby*tabx * INTER_REMAP_COEF_SCALE );
WT val = v0 * itab0 + v1 * itab1 + v2 * itab2 + v3 * itab3;
dst[0] = convertToT((val + (1 << (INTER_REMAP_COEF_BITS-1))) >> INTER_REMAP_COEF_BITS);
#else
float tabx2 = 1.0f - tabx, taby2 = 1.0f - taby;
WT val = v0 * tabx2 * taby2 + v1 * tabx * taby2 + v2 * tabx2 * taby + v3 * tabx * taby;
dst[0] = convertToT(val);
#endif
}
}
#elif defined INTER_CUBIC
inline void interpolateCubic( float x, float* coeffs )
{
const float A = -0.75f;
coeffs[0] = ((A*(x + 1.f) - 5.0f*A)*(x + 1.f) + 8.0f*A)*(x + 1.f) - 4.0f*A;
coeffs[1] = ((A + 2.f)*x - (A + 3.f))*x*x + 1.f;
coeffs[2] = ((A + 2.f)*(1.f - x) - (A + 3.f))*(1.f - x)*(1.f - x) + 1.f;
coeffs[3] = 1.f - coeffs[0] - coeffs[1] - coeffs[2];
}
__kernel void warpPerspective(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols,
__global uchar * dstptr, int dst_step, int dst_offset, int dst_rows, int dst_cols,
__constant CT * M, WT scalar)
{
int dx = get_global_id(0);
int dy = get_global_id(1);
if (dx < dst_cols && dy < dst_rows)
{
CT X0 = M[0] * dx + M[1] * dy + M[2];
CT Y0 = M[3] * dx + M[4] * dy + M[5];
CT W = M[6] * dx + M[7] * dy + M[8];
W = W != 0.0f ? INTER_TAB_SIZE / W : 0.0f;
int X = rint(X0 * W), Y = rint(Y0 * W);
short sx = convert_short_sat(X >> INTER_BITS) - 1;
short sy = convert_short_sat(Y >> INTER_BITS) - 1;
short ay = (short)(Y & (INTER_TAB_SIZE-1));
short ax = (short)(X & (INTER_TAB_SIZE-1));
WT v[16];
#pragma unroll
for (int y = 0; y < 4; y++)
#pragma unroll
for (int x = 0; x < 4; x++)
v[mad24(y, 4, x)] = (sx+x >= 0 && sx+x < src_cols && sy+y >= 0 && sy+y < src_rows) ?
convertToWT(*(__global const T *)(srcptr + mad24(sy+y, src_step, src_offset + (sx+x) * (int)sizeof(T)))) : scalar;
float tab1y[4], tab1x[4];
float ayy = INTER_SCALE * ay;
float axx = INTER_SCALE * ax;
interpolateCubic(ayy, tab1y);
interpolateCubic(axx, tab1x);
int dst_index = mad24(dy, dst_step, dst_offset + dx * (int)sizeof(T));
__global T * dst = (__global T *)(dstptr + dst_index);
WT sum = (WT)(0);
#if depth <= 4
int itab[16];
#pragma unroll
for (int i = 0; i < 16; i++)
itab[i] = rint(tab1y[(i>>2)] * tab1x[(i&3)] * INTER_REMAP_COEF_SCALE);
#pragma unroll
for (int i = 0; i < 16; i++)
sum += v[i] * itab[i];
dst[0] = convertToT( (sum + (1 << (INTER_REMAP_COEF_BITS-1))) >> INTER_REMAP_COEF_BITS );
#else
#pragma unroll
for (int i = 0; i < 16; i++)
sum += v[i] * tab1y[(i>>2)] * tab1x[(i&3)];
dst[0] = convertToT( sum );
#endif
}
}
#endif
......@@ -61,7 +61,99 @@ namespace cvtest {
namespace ocl {
/////////////////////////////////////////////////////////////////////////////////////////////////
// resize
// warpAffine & warpPerspective
PARAM_TEST_CASE(WarpTestBase, MatType, Interpolation, bool, bool)
{
int type, interpolation;
Size dsize;
bool useRoi, mapInverse;
TEST_DECLARE_INPUT_PARATEMER(src)
TEST_DECLARE_OUTPUT_PARATEMER(dst)
virtual void SetUp()
{
type = GET_PARAM(0);
interpolation = GET_PARAM(1);
mapInverse = GET_PARAM(2);
useRoi = GET_PARAM(3);
if (mapInverse)
interpolation |= WARP_INVERSE_MAP;
}
void random_roi()
{
dsize = randomSize(1, MAX_VALUE);
Size roiSize = randomSize(1, MAX_VALUE);
Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
randomSubMat(src, src_roi, roiSize, srcBorder, type, -MAX_VALUE, MAX_VALUE);
Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
randomSubMat(dst, dst_roi, dsize, dstBorder, type, -MAX_VALUE, MAX_VALUE);
UMAT_UPLOAD_INPUT_PARAMETER(src)
UMAT_UPLOAD_OUTPUT_PARAMETER(dst)
}
void Near(double threshold = 0.0)
{
EXPECT_MAT_NEAR(dst, udst, threshold);
EXPECT_MAT_NEAR(dst_roi, udst_roi, threshold);
}
};
/////warpAffine
typedef WarpTestBase WarpAffine;
OCL_TEST_P(WarpAffine, Mat)
{
for (int j = 0; j < test_loop_times; j++)
{
random_roi();
Mat M = getRotationMatrix2D(Point2f(src_roi.cols / 2.0f, src_roi.rows / 2.0f),
rng.uniform(-180.f, 180.f), rng.uniform(0.4f, 2.0f));
OCL_OFF(cv::warpAffine(src_roi, dst_roi, M, dsize, interpolation));
OCL_ON(cv::warpAffine(usrc_roi, udst_roi, M, dsize, interpolation));
Near(1.0);
}
}
//// warpPerspective
typedef WarpTestBase WarpPerspective;
OCL_TEST_P(WarpPerspective, Mat)
{
for (int j = 0; j < test_loop_times; j++)
{
random_roi();
float cols = static_cast<float>(src_roi.cols), rows = static_cast<float>(src_roi.rows);
float cols2 = cols / 2.0f, rows2 = rows / 2.0f;
Point2f sp[] = { Point2f(0.0f, 0.0f), Point2f(cols, 0.0f), Point2f(0.0f, rows), Point2f(cols, rows) };
Point2f dp[] = { Point2f(rng.uniform(0.0f, cols2), rng.uniform(0.0f, rows2)),
Point2f(rng.uniform(cols2, cols), rng.uniform(0.0f, rows2)),
Point2f(rng.uniform(0.0f, cols2), rng.uniform(rows2, rows)),
Point2f(rng.uniform(cols2, cols), rng.uniform(rows2, rows)) };
Mat M = getPerspectiveTransform(sp, dp);
OCL_OFF(cv::warpPerspective(src_roi, dst_roi, M, dsize, interpolation));
OCL_ON(cv::warpPerspective(usrc_roi, udst_roi, M, dsize, interpolation));
Near(1.0);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//// resize
PARAM_TEST_CASE(Resize, MatType, double, double, Interpolation, bool)
{
......@@ -127,10 +219,22 @@ OCL_TEST_P(Resize, Mat)
/////////////////////////////////////////////////////////////////////////////////////
OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarpResize, Resize, Combine(
Values((MatType)CV_8UC1, CV_8UC4, CV_32FC1, CV_32FC4),
Values(0.7, 0.4, 2.0),
Values(0.3, 0.6, 2.0),
OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpAffine, Combine(
Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR, (Interpolation)INTER_CUBIC),
Bool(),
Bool()));
OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpPerspective, Combine(
Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),
Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR, (Interpolation)INTER_CUBIC),
Bool(),
Bool()));
OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, Resize, Combine(
Values(CV_8UC1, CV_8UC4, CV_16UC2, CV_32FC1, CV_32FC4),
Values(0.5, 1.5, 2.0),
Values(0.5, 1.5, 2.0),
Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR),
Bool()));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment