mirror of https://github.com/opencv/opencv.git
Merge pull request #1824 from vpisarev:ocl_experiments5
commit
8d1a8df1a9
39 changed files with 2638 additions and 366 deletions
@ -0,0 +1,60 @@ |
||||
/*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) 2013, OpenCV Foundation, all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other 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 OpenCV Foundation 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*/
|
||||
|
||||
#ifndef __OPENCV_OPENCL_GENBASE_HPP__ |
||||
#define __OPENCV_OPENCL_GENBASE_HPP__ |
||||
|
||||
namespace cv |
||||
{ |
||||
namespace ocl |
||||
{ |
||||
|
||||
struct ProgramEntry |
||||
{ |
||||
const char* name; |
||||
const char* programStr; |
||||
const char* programHash; |
||||
}; |
||||
|
||||
} |
||||
} |
||||
|
||||
#endif |
@ -0,0 +1,303 @@ |
||||
/*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. |
||||
// Copyright (C) 2013, OpenCV Foundation, all rights reserved. |
||||
// Third party copyrights are property of their respective owners. |
||||
// |
||||
// @Authors |
||||
// Jia Haipeng, jiahaipeng95@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 copyright holders 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*/ |
||||
|
||||
/* |
||||
Usage: |
||||
after compiling this program user gets a single kernel called KF. |
||||
the following flags should be passed: |
||||
1) one of "-D BINARY_OP", "-D UNARY_OP", "-D MASK_BINARY_OP" or "-D MASK_UNARY_OP" |
||||
2) the actual operation performed, one of "-D OP_...", see below the list of operations. |
||||
2a) "-D dstDepth=<destination depth> [-D cn=<num channels]" |
||||
for some operations, like min/max/and/or/xor it's enough |
||||
2b) "-D srcDepth1=<source1 depth> -D srcDepth2=<source2 depth> -D dstDepth=<destination depth> |
||||
-D workDepth=<work depth> [-D cn=<num channels>]" - for mixed-type operations |
||||
*/ |
||||
|
||||
#if defined (DOUBLE_SUPPORT) |
||||
#ifdef cl_khr_fp64 |
||||
#pragma OPENCL EXTENSION cl_khr_fp64:enable |
||||
#elif defined (cl_amd_fp64) |
||||
#pragma OPENCL EXTENSION cl_amd_fp64:enable |
||||
#endif |
||||
#endif |
||||
|
||||
#define CV_32S 4 |
||||
#define CV_32F 5 |
||||
|
||||
#define dstelem *(dstT*)(dstptr + dst_index) |
||||
#define noconvert(x) x |
||||
|
||||
#ifndef workT |
||||
|
||||
#define srcT1 dstT |
||||
#define srcT2 dstT |
||||
#define workT dstT |
||||
#define srcelem1 *(dstT*)(srcptr1 + src1_index) |
||||
#define srcelem2 *(dstT*)(srcptr2 + src2_index) |
||||
#define convertToDT noconvert |
||||
|
||||
#else |
||||
|
||||
#define srcelem1 convertToWT1(*(srcT1*)(srcptr1 + src1_index)) |
||||
#define srcelem2 convertToWT2(*(srcT2*)(srcptr2 + src2_index)) |
||||
|
||||
#endif |
||||
|
||||
#define EXTRA_PARAMS |
||||
|
||||
#if defined OP_ADD_SAT |
||||
#define PROCESS_ELEM dstelem = add_sat(srcelem1, srcelem2) |
||||
|
||||
#elif defined OP_ADD |
||||
#define PROCESS_ELEM dstelem = convertToDT(srcelem1 + srcelem2) |
||||
|
||||
#elif defined OP_SUB_SAT |
||||
#define PROCESS_ELEM dstelem = sub_sat(srcelem1, srcelem2) |
||||
|
||||
#elif defined OP_SUB |
||||
#define PROCESS_ELEM dstelem = convertToDT(srcelem1 - srcelem2) |
||||
|
||||
#elif defined OP_RSUB_SAT |
||||
#define PROCESS_ELEM dstelem = sub_sat(srcelem2, srcelem1) |
||||
|
||||
#elif defined OP_RSUB |
||||
#define PROCESS_ELEM dstelem = convertToDT(srcelem2 - srcelem1) |
||||
|
||||
#elif defined OP_ABSDIFF |
||||
#define PROCESS_ELEM dstelem = abs_diff(srcelem1, srcelem2) |
||||
|
||||
#elif defined OP_AND |
||||
#define PROCESS_ELEM dstelem = srcelem1 & srcelem2 |
||||
|
||||
#elif defined OP_OR |
||||
#define PROCESS_ELEM dstelem = srcelem1 | srcelem2 |
||||
|
||||
#elif defined OP_XOR |
||||
#define PROCESS_ELEM dstelem = srcelem1 ^ srcelem2 |
||||
|
||||
#elif defined OP_NOT |
||||
#define PROCESS_ELEM dstelem = ~srcelem1 |
||||
|
||||
#elif defined OP_MIN |
||||
#define PROCESS_ELEM dstelem = min(srcelem1, srcelem2) |
||||
|
||||
#elif defined OP_MAX |
||||
#define PROCESS_ELEM dstelem = max(srcelem1, srcelem2) |
||||
|
||||
#elif defined OP_MUL |
||||
#define PROCESS_ELEM dstelem = convertToDT(srcelem1 * srcelem2) |
||||
|
||||
#elif defined OP_MUL_SCALE |
||||
#undef EXTRA_PARAMS |
||||
#define EXTRA_PARAMS , workT scale |
||||
#define PROCESS_ELEM dstelem = convertToDT(srcelem1 * srcelem2 * scale) |
||||
|
||||
#elif defined OP_DIV |
||||
#define PROCESS_ELEM \ |
||||
workT e2 = srcelem2, zero = (workT)(0); \ |
||||
dstelem = convertToDT(e2 != zero ? srcelem1 / e2 : zero) |
||||
|
||||
#elif defined OP_DIV_SCALE |
||||
#undef EXTRA_PARAMS |
||||
#define EXTRA_PARAMS , workT scale |
||||
#define PROCESS_ELEM \ |
||||
workT e2 = srcelem2, zero = (workT)(0); \ |
||||
dstelem = convertToDT(e2 != zero ? srcelem1 * scale / e2 : zero) |
||||
|
||||
#elif defined OP_RECIP_SCALE |
||||
#undef EXTRA_PARAMS |
||||
#define EXTRA_PARAMS , workT scale |
||||
#define PROCESS_ELEM \ |
||||
workT e1 = srcelem1, zero = (workT)(0); \ |
||||
dstelem = convertToDT(e1 != zero ? scale / e1 : zero) |
||||
|
||||
#elif defined OP_ADDW |
||||
#undef EXTRA_PARAMS |
||||
#define EXTRA_PARAMS , workT alpha, workT beta, workT gamma |
||||
#define PROCESS_ELEM dstelem = convertToDT(srcelem1*alpha + srcelem2*beta + gamma) |
||||
|
||||
#elif defined OP_MAG |
||||
#define PROCESS_ELEM dstelem = hypot(srcelem1, srcelem2) |
||||
|
||||
#elif defined OP_PHASE_RADIANS |
||||
#define PROCESS_ELEM \ |
||||
workT tmp = atan2(srcelem2, srcelem1); \ |
||||
if(tmp < 0) tmp += 6.283185307179586232; \ |
||||
dstelem = tmp |
||||
|
||||
#elif defined OP_PHASE_DEGREES |
||||
#define PROCESS_ELEM \ |
||||
workT tmp = atan2(srcelem2, srcelem1)*57.29577951308232286465; \ |
||||
if(tmp < 0) tmp += 360; \ |
||||
dstelem = tmp |
||||
|
||||
#elif defined OP_EXP |
||||
#define PROCESS_ELEM dstelem = exp(srcelem1) |
||||
|
||||
#elif defined OP_SQRT |
||||
#define PROCESS_ELEM dstelem = sqrt(srcelem1) |
||||
|
||||
#elif defined OP_LOG |
||||
#define PROCESS_ELEM dstelem = log(abs(srcelem1)) |
||||
|
||||
#elif defined OP_CMP |
||||
#define PROCESS_ELEM dstelem = convert_uchar(srcelem1 CMP_OPERATOR srcelem2 ? 255 : 0) |
||||
|
||||
#elif defined OP_CONVERT |
||||
#define PROCESS_ELEM dstelem = convertToDT(srcelem1) |
||||
|
||||
#elif defined OP_CONVERT_SCALE |
||||
#undef EXTRA_PARAMS |
||||
#define EXTRA_PARAMS , workT alpha, workT beta |
||||
#define PROCESS_ELEM dstelem = convertToDT(srcelem1*alpha + beta) |
||||
|
||||
#else |
||||
#error "unknown op type" |
||||
#endif |
||||
|
||||
#if defined UNARY_OP || defined MASK_UNARY_OP |
||||
#undef srcelem2 |
||||
#if defined OP_AND || defined OP_OR || defined OP_XOR || defined OP_ADD || defined OP_SAT_ADD || \ |
||||
defined OP_SUB || defined OP_SAT_SUB || defined OP_RSUB || defined OP_SAT_RSUB || \ |
||||
defined OP_ABSDIFF || defined OP_CMP || defined OP_MIN || defined OP_MAX |
||||
#undef EXTRA_PARAMS |
||||
#define EXTRA_PARAMS , workT srcelem2 |
||||
#endif |
||||
#endif |
||||
|
||||
#if defined BINARY_OP |
||||
|
||||
__kernel void KF(__global const uchar* srcptr1, int srcstep1, int srcoffset1, |
||||
__global const uchar* srcptr2, int srcstep2, int srcoffset2, |
||||
__global uchar* dstptr, int dststep, int dstoffset, |
||||
int rows, int cols EXTRA_PARAMS ) |
||||
{ |
||||
int x = get_global_id(0); |
||||
int y = get_global_id(1); |
||||
|
||||
if (x < cols && y < rows) |
||||
{ |
||||
int src1_index = mad24(y, srcstep1, x*sizeof(srcT1) + srcoffset1); |
||||
int src2_index = mad24(y, srcstep2, x*sizeof(srcT2) + srcoffset2); |
||||
int dst_index = mad24(y, dststep, x*sizeof(dstT) + dstoffset); |
||||
|
||||
PROCESS_ELEM; |
||||
//printf("(x=%d, y=%d). %d, %d, %d\n", x, y, (int)srcelem1, (int)srcelem2, (int)dstelem); |
||||
} |
||||
} |
||||
|
||||
#elif defined MASK_BINARY_OP |
||||
|
||||
__kernel void KF(__global const uchar* srcptr1, int srcstep1, int srcoffset1, |
||||
__global const uchar* srcptr2, int srcstep2, int srcoffset2, |
||||
__global const uchar* mask, int maskstep, int maskoffset, |
||||
__global uchar* dstptr, int dststep, int dstoffset, |
||||
int rows, int cols EXTRA_PARAMS ) |
||||
{ |
||||
int x = get_global_id(0); |
||||
int y = get_global_id(1); |
||||
|
||||
if (x < cols && y < rows) |
||||
{ |
||||
int mask_index = mad24(y, maskstep, x + maskoffset); |
||||
if( mask[mask_index] ) |
||||
{ |
||||
int src1_index = mad24(y, srcstep1, x*sizeof(srcT1) + srcoffset1); |
||||
int src2_index = mad24(y, srcstep2, x*sizeof(srcT2) + srcoffset2); |
||||
int dst_index = mad24(y, dststep, x*sizeof(dstT) + dstoffset); |
||||
|
||||
PROCESS_ELEM; |
||||
} |
||||
} |
||||
} |
||||
|
||||
#elif defined UNARY_OP |
||||
|
||||
__kernel void KF(__global const uchar* srcptr1, int srcstep1, int srcoffset1, |
||||
__global uchar* dstptr, int dststep, int dstoffset, |
||||
int rows, int cols EXTRA_PARAMS ) |
||||
{ |
||||
int x = get_global_id(0); |
||||
int y = get_global_id(1); |
||||
|
||||
if (x < cols && y < rows) |
||||
{ |
||||
int src1_index = mad24(y, srcstep1, x*sizeof(srcT1) + srcoffset1); |
||||
int dst_index = mad24(y, dststep, x*sizeof(dstT) + dstoffset); |
||||
|
||||
PROCESS_ELEM; |
||||
} |
||||
} |
||||
|
||||
#elif defined MASK_UNARY_OP |
||||
|
||||
__kernel void KF(__global const uchar* srcptr1, int srcstep1, int srcoffset1, |
||||
__global const uchar* mask, int maskstep, int maskoffset, |
||||
__global uchar* dstptr, int dststep, int dstoffset, |
||||
int rows, int cols EXTRA_PARAMS ) |
||||
{ |
||||
int x = get_global_id(0); |
||||
int y = get_global_id(1); |
||||
|
||||
if (x < cols && y < rows) |
||||
{ |
||||
int mask_index = mad24(y, maskstep, x + maskoffset); |
||||
if( mask[mask_index] ) |
||||
{ |
||||
int src1_index = mad24(y, srcstep1, x*sizeof(srcT1) + srcoffset1); |
||||
int dst_index = mad24(y, dststep, x*sizeof(dstT) + dstoffset); |
||||
|
||||
PROCESS_ELEM; |
||||
} |
||||
} |
||||
} |
||||
|
||||
#else |
||||
|
||||
#error "Unknown operation type" |
||||
|
||||
#endif |
@ -0,0 +1,73 @@ |
||||
/*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. |
||||
// Copyright (C) 2013, OpenCV Foundation, all rights reserved. |
||||
// Third party copyrights are property of their respective owners. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without modification, |
||||
// are permitted provided that the following conditions are met: |
||||
// |
||||
// * Redistribution's of source code must retain the above copyright notice, |
||||
// this list of conditions and the following disclaimer. |
||||
// |
||||
// * Redistribution's in binary form must reproduce the above copyright notice, |
||||
// this list of conditions and the following disclaimer in the documentation |
||||
// and/or other 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 copyright holders 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*/ |
||||
|
||||
__kernel void setMask(__global const uchar* mask, int maskstep, int maskoffset, |
||||
__global uchar* dstptr, int dststep, int dstoffset, |
||||
int rows, int cols, dstT value ) |
||||
{ |
||||
int x = get_global_id(0); |
||||
int y = get_global_id(1); |
||||
|
||||
if (x < cols && y < rows) |
||||
{ |
||||
int mask_index = mad24(y, maskstep, x + maskoffset); |
||||
if( mask[mask_index] ) |
||||
{ |
||||
int dst_index = mad24(y, dststep, x*sizeof(dstT) + dstoffset); |
||||
*(dstT*)(dstptr + dst_index) = value; |
||||
} |
||||
} |
||||
} |
||||
|
||||
__kernel void set(__global uchar* dstptr, int dststep, int dstoffset, |
||||
int rows, int cols, dstT value ) |
||||
{ |
||||
int x = get_global_id(0); |
||||
int y = get_global_id(1); |
||||
|
||||
if (x < cols && y < rows) |
||||
{ |
||||
int dst_index = mad24(y, dststep, x*sizeof(dstT) + dstoffset); |
||||
*(dstT*)(dstptr + dst_index) = value; |
||||
} |
||||
} |
@ -0,0 +1,306 @@ |
||||
/*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 |
||||
// Jia Haipeng, jiahaipeng95@gmail.com |
||||
// Peng Xiao, pengxiao@multicorewareinc.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*/ |
||||
|
||||
/**************************************PUBLICFUNC*************************************/ |
||||
|
||||
#if defined (DOUBLE_SUPPORT) |
||||
#pragma OPENCL EXTENSION cl_khr_fp64:enable |
||||
#endif |
||||
|
||||
#if depth == 0 |
||||
#define DATA_TYPE uchar |
||||
#define MAX_NUM 255 |
||||
#define HALF_MAX 128 |
||||
#define SAT_CAST(num) convert_uchar_sat(num) |
||||
#define DEPTH_0 |
||||
#elif depth == 2 |
||||
#define DATA_TYPE ushort |
||||
#define MAX_NUM 65535 |
||||
#define HALF_MAX 32768 |
||||
#define SAT_CAST(num) convert_ushort_sat(num) |
||||
#define DEPTH_2 |
||||
#elif depth == 5 |
||||
#define DATA_TYPE float |
||||
#define MAX_NUM 1.0f |
||||
#define HALF_MAX 0.5f |
||||
#define SAT_CAST(num) (num) |
||||
#define DEPTH_5 |
||||
#else |
||||
#error "invalid depth: should be 0 (CV_8U), 2 (CV_16U) or 5 (CV_32F)" |
||||
#endif |
||||
|
||||
#define CV_DESCALE(x,n) (((x) + (1 << ((n)-1))) >> (n)) |
||||
|
||||
enum |
||||
{ |
||||
yuv_shift = 14, |
||||
xyz_shift = 12, |
||||
R2Y = 4899, |
||||
G2Y = 9617, |
||||
B2Y = 1868, |
||||
BLOCK_SIZE = 256 |
||||
}; |
||||
|
||||
#define scnbytes ((int)sizeof(DATA_TYPE)*scn) |
||||
#define dcnbytes ((int)sizeof(DATA_TYPE)*dcn) |
||||
|
||||
///////////////////////////////////// RGB <-> GRAY ////////////////////////////////////// |
||||
|
||||
__kernel void RGB2Gray(__global const uchar* srcptr, int srcstep, int srcoffset, |
||||
__global uchar* dstptr, int dststep, int dstoffset, |
||||
int rows, int cols) |
||||
{ |
||||
const int x = get_global_id(0); |
||||
const int y = get_global_id(1); |
||||
|
||||
if (y < rows && x < cols) |
||||
{ |
||||
const DATA_TYPE* src = (const DATA_TYPE*)(srcptr + mad24(y, srcstep, srcoffset + x * scnbytes)); |
||||
DATA_TYPE* dst = (DATA_TYPE*)(dstptr + mad24(y, dststep, dstoffset + x * dcnbytes)); |
||||
#if defined (DEPTH_5) |
||||
dst[0] = src[bidx] * 0.114f + src[1] * 0.587f + src[(bidx^2)] * 0.299f; |
||||
#else |
||||
dst[0] = (DATA_TYPE)CV_DESCALE((src[bidx] * B2Y + src[1] * G2Y + src[(bidx^2)] * R2Y), yuv_shift); |
||||
#endif |
||||
} |
||||
} |
||||
|
||||
__kernel void Gray2RGB(__global const uchar* srcptr, int srcstep, int srcoffset, |
||||
__global uchar* dstptr, int dststep, int dstoffset, |
||||
int rows, int cols) |
||||
{ |
||||
const int x = get_global_id(0); |
||||
const int y = get_global_id(1); |
||||
|
||||
if (y < rows && x < cols) |
||||
{ |
||||
const DATA_TYPE* src = (const DATA_TYPE*)(srcptr + mad24(y, srcstep, srcoffset + x * scnbytes)); |
||||
DATA_TYPE* dst = (DATA_TYPE*)(dstptr + mad24(y, dststep, dstoffset + x * dcnbytes)); |
||||
DATA_TYPE val = src[0]; |
||||
dst[0] = dst[1] = dst[2] = val; |
||||
#if dcn == 4 |
||||
dst[3] = MAX_NUM; |
||||
#endif |
||||
} |
||||
} |
||||
|
||||
///////////////////////////////////// RGB <-> YUV ////////////////////////////////////// |
||||
|
||||
__constant float c_RGB2YUVCoeffs_f[5] = { 0.114f, 0.587f, 0.299f, 0.492f, 0.877f }; |
||||
__constant int c_RGB2YUVCoeffs_i[5] = { B2Y, G2Y, R2Y, 8061, 14369 }; |
||||
|
||||
__kernel void RGB2YUV(__global const uchar* srcptr, int srcstep, int srcoffset, |
||||
__global uchar* dstptr, int dststep, int dstoffset, |
||||
int rows, int cols) |
||||
{ |
||||
int x = get_global_id(0); |
||||
int y = get_global_id(1); |
||||
|
||||
if (y < rows && x < cols) |
||||
{ |
||||
const DATA_TYPE* src = (const DATA_TYPE*)(srcptr + mad24(y, srcstep, srcoffset + x * scnbytes)); |
||||
DATA_TYPE* dst = (DATA_TYPE*)(dstptr + mad24(y, dststep, dstoffset + x * dcnbytes)); |
||||
DATA_TYPE b=src[bidx], g=src[1], r=src[bidx^2]; |
||||
|
||||
#if defined (DEPTH_5) |
||||
__constant float * coeffs = c_RGB2YUVCoeffs_f; |
||||
const DATA_TYPE Y = b * coeffs[0] + g * coeffs[1] + r * coeffs[2]; |
||||
const DATA_TYPE U = (b - Y) * coeffs[3] + HALF_MAX; |
||||
const DATA_TYPE V = (r - Y) * coeffs[4] + HALF_MAX; |
||||
#else |
||||
__constant int * coeffs = c_RGB2YUVCoeffs_i; |
||||
const int delta = HALF_MAX * (1 << yuv_shift); |
||||
const int Y = CV_DESCALE(b * coeffs[0] + g * coeffs[1] + r * coeffs[2], yuv_shift); |
||||
const int U = CV_DESCALE((b - Y) * coeffs[3] + delta, yuv_shift); |
||||
const int V = CV_DESCALE((r - Y) * coeffs[4] + delta, yuv_shift); |
||||
#endif |
||||
|
||||
dst[0] = SAT_CAST( Y ); |
||||
dst[1] = SAT_CAST( U ); |
||||
dst[2] = SAT_CAST( V ); |
||||
} |
||||
} |
||||
|
||||
__constant float c_YUV2RGBCoeffs_f[5] = { 2.032f, -0.395f, -0.581f, 1.140f }; |
||||
__constant int c_YUV2RGBCoeffs_i[5] = { 33292, -6472, -9519, 18678 }; |
||||
|
||||
__kernel void YUV2RGB(__global const uchar* srcptr, int srcstep, int srcoffset, |
||||
__global uchar* dstptr, int dststep, int dstoffset, |
||||
int rows, int cols) |
||||
{ |
||||
int x = get_global_id(0); |
||||
int y = get_global_id(1); |
||||
|
||||
if (y < rows && x < cols) |
||||
{ |
||||
const DATA_TYPE* src = (const DATA_TYPE*)(srcptr + mad24(y, srcstep, srcoffset + x * scnbytes)); |
||||
DATA_TYPE* dst = (DATA_TYPE*)(dstptr + mad24(y, dststep, dstoffset + x * dcnbytes)); |
||||
DATA_TYPE Y = src[0], U = src[1], V = src[2]; |
||||
|
||||
#if defined (DEPTH_5) |
||||
__constant float * coeffs = c_YUV2RGBCoeffs_f; |
||||
const float r = Y + (V - HALF_MAX) * coeffs[3]; |
||||
const float g = Y + (V - HALF_MAX) * coeffs[2] + (U - HALF_MAX) * coeffs[1]; |
||||
const float b = Y + (U - HALF_MAX) * coeffs[0]; |
||||
#else |
||||
__constant int * coeffs = c_YUV2RGBCoeffs_i; |
||||
const int r = Y + CV_DESCALE((V - HALF_MAX) * coeffs[3], yuv_shift); |
||||
const int g = Y + CV_DESCALE((V - HALF_MAX) * coeffs[2] + (U - HALF_MAX) * coeffs[1], yuv_shift); |
||||
const int b = Y + CV_DESCALE((U - HALF_MAX) * coeffs[0], yuv_shift); |
||||
#endif |
||||
|
||||
dst[bidx] = SAT_CAST( b ); |
||||
dst[1] = SAT_CAST( g ); |
||||
dst[bidx^2] = SAT_CAST( r ); |
||||
#if dcn == 4 |
||||
dst[3] = MAX_NUM; |
||||
#endif |
||||
} |
||||
} |
||||
|
||||
__constant int ITUR_BT_601_CY = 1220542; |
||||
__constant int ITUR_BT_601_CUB = 2116026; |
||||
__constant int ITUR_BT_601_CUG = 409993; |
||||
__constant int ITUR_BT_601_CVG = 852492; |
||||
__constant int ITUR_BT_601_CVR = 1673527; |
||||
__constant int ITUR_BT_601_SHIFT = 20; |
||||
|
||||
__kernel void YUV2RGBA_NV12(__global const uchar* srcptr, int srcstep, int srcoffset, |
||||
__global uchar* dstptr, int dststep, int dstoffset, |
||||
int rows, int cols) |
||||
{ |
||||
const int x = get_global_id(0); // max_x = width / 2 |
||||
const int y = get_global_id(1); // max_y = height/ 2 |
||||
|
||||
if (y < rows / 2 && x < cols / 2 ) |
||||
{ |
||||
__global const uchar* ysrc = srcptr + mad24(y << 1, srcstep, (x << 1) + srcoffset); |
||||
__global const uchar* usrc = srcptr + mad24(rows + y, srcstep, (x << 1) + srcoffset); |
||||
__global uchar* dst1 = dstptr + mad24(y << 1, dststep, x*(dcn*2) + dstoffset); |
||||
__global uchar* dst2 = dstptr + mad24((y << 1) + 1, dststep, x*(dcn*2) + dstoffset); |
||||
|
||||
int Y1 = ysrc[0]; |
||||
int Y2 = ysrc[1]; |
||||
int Y3 = ysrc[srcstep]; |
||||
int Y4 = ysrc[srcstep + 1]; |
||||
|
||||
int U = usrc[0] - 128; |
||||
int V = usrc[1] - 128; |
||||
|
||||
int ruv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CVR * V; |
||||
int guv = (1 << (ITUR_BT_601_SHIFT - 1)) - ITUR_BT_601_CVG * V - ITUR_BT_601_CUG * U; |
||||
int buv = (1 << (ITUR_BT_601_SHIFT - 1)) + ITUR_BT_601_CUB * U; |
||||
|
||||
Y1 = max(0, Y1 - 16) * ITUR_BT_601_CY; |
||||
dst1[2 - bidx] = convert_uchar_sat((Y1 + ruv) >> ITUR_BT_601_SHIFT); |
||||
dst1[1] = convert_uchar_sat((Y1 + guv) >> ITUR_BT_601_SHIFT); |
||||
dst1[bidx] = convert_uchar_sat((Y1 + buv) >> ITUR_BT_601_SHIFT); |
||||
#if dcn == 4 |
||||
dst1[3] = 255; |
||||
#endif |
||||
|
||||
Y2 = max(0, Y2 - 16) * ITUR_BT_601_CY; |
||||
dst1[(dcn + 2) - bidx] = convert_uchar_sat((Y2 + ruv) >> ITUR_BT_601_SHIFT); |
||||
dst1[dcn + 1] = convert_uchar_sat((Y2 + guv) >> ITUR_BT_601_SHIFT); |
||||
dst1[dcn + bidx] = convert_uchar_sat((Y2 + buv) >> ITUR_BT_601_SHIFT); |
||||
#if dcn == 4 |
||||
dst1[7] = 255; |
||||
#endif |
||||
|
||||
Y3 = max(0, Y3 - 16) * ITUR_BT_601_CY; |
||||
dst2[2 - bidx] = convert_uchar_sat((Y3 + ruv) >> ITUR_BT_601_SHIFT); |
||||
dst2[1] = convert_uchar_sat((Y3 + guv) >> ITUR_BT_601_SHIFT); |
||||
dst2[bidx] = convert_uchar_sat((Y3 + buv) >> ITUR_BT_601_SHIFT); |
||||
#if dcn == 4 |
||||
dst2[3] = 255; |
||||
#endif |
||||
|
||||
Y4 = max(0, Y4 - 16) * ITUR_BT_601_CY; |
||||
dst2[(dcn + 2) - bidx] = convert_uchar_sat((Y4 + ruv) >> ITUR_BT_601_SHIFT); |
||||
dst2[dcn + 1] = convert_uchar_sat((Y4 + guv) >> ITUR_BT_601_SHIFT); |
||||
dst2[dcn + bidx] = convert_uchar_sat((Y4 + buv) >> ITUR_BT_601_SHIFT); |
||||
#if dcn == 4 |
||||
dst2[7] = 255; |
||||
#endif |
||||
} |
||||
} |
||||
|
||||
///////////////////////////////////// RGB <-> YUV ////////////////////////////////////// |
||||
|
||||
__constant float c_RGB2YCrCbCoeffs_f[5] = {0.299f, 0.587f, 0.114f, 0.713f, 0.564f}; |
||||
__constant int c_RGB2YCrCbCoeffs_i[5] = {R2Y, G2Y, B2Y, 11682, 9241}; |
||||
|
||||
__kernel void RGB2YCrCb(__global const uchar* srcptr, int srcstep, int srcoffset, |
||||
__global uchar* dstptr, int dststep, int dstoffset, |
||||
int rows, int cols) |
||||
{ |
||||
int x = get_global_id(0); |
||||
int y = get_global_id(1); |
||||
|
||||
if (y < rows && x < cols) |
||||
{ |
||||
const DATA_TYPE* src = (const DATA_TYPE*)(srcptr + mad24(y, srcstep, srcoffset + x * scnbytes)); |
||||
DATA_TYPE* dst = (DATA_TYPE*)(dstptr + mad24(y, dststep, dstoffset + x * dcnbytes)); |
||||
DATA_TYPE b=src[bidx], g=src[1], r=src[bidx^2]; |
||||
|
||||
#if defined (DEPTH_5) |
||||
__constant float * coeffs = c_RGB2YCrCbCoeffs_f; |
||||
const DATA_TYPE Y = b * coeffs[0] + g * coeffs[1] + r * coeffs[2]; |
||||
const DATA_TYPE Cr = (r - Y) * coeffs[3] + HALF_MAX; |
||||
const DATA_TYPE Cb = (b - Y) * coeffs[4] + HALF_MAX; |
||||
#else |
||||
__constant int * coeffs = c_RGB2YCrCbCoeffs_i; |
||||
const int delta = HALF_MAX * (1 << yuv_shift); |
||||
const int Y = CV_DESCALE(b * coeffs[0] + g * coeffs[1] + r * coeffs[2], yuv_shift); |
||||
const int Cr = CV_DESCALE((r - Y) * coeffs[3] + delta, yuv_shift); |
||||
const int Cb = CV_DESCALE((b - Y) * coeffs[4] + delta, yuv_shift); |
||||
#endif |
||||
|
||||
dst[0] = SAT_CAST( Y ); |
||||
dst[1] = SAT_CAST( Cr ); |
||||
dst[2] = SAT_CAST( Cb ); |
||||
} |
||||
} |
@ -0,0 +1,151 @@ |
||||
/*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 |
||||
// Niko Li, newlife20080214@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*/ |
||||
|
||||
|
||||
// resize kernel |
||||
// Currently, CV_8UC1 CV_8UC4 CV_32FC1 and CV_32FC4are supported. |
||||
// We shall support other types later if necessary. |
||||
|
||||
#if defined DOUBLE_SUPPORT |
||||
#pragma OPENCL EXTENSION cl_khr_fp64:enable |
||||
#define F double |
||||
#else |
||||
#define F float |
||||
#endif |
||||
|
||||
#define INTER_RESIZE_COEF_BITS 11 |
||||
#define INTER_RESIZE_COEF_SCALE (1 << INTER_RESIZE_COEF_BITS) |
||||
#define CAST_BITS (INTER_RESIZE_COEF_BITS << 1) |
||||
#define CAST_SCALE (1.0f/(1<<CAST_BITS)) |
||||
#define INC(x,l) min(x+1,l-1) |
||||
|
||||
#define PIXSIZE ((int)sizeof(PIXTYPE)) |
||||
#define noconvert(x) (x) |
||||
|
||||
#if defined INTER_LINEAR |
||||
|
||||
__kernel void resizeLN(__global const uchar* srcptr, int srcstep, int srcoffset, |
||||
int srcrows, int srccols, |
||||
__global uchar* dstptr, int dststep, int dstoffset, |
||||
int dstrows, int dstcols, |
||||
float ifx, float ify) |
||||
{ |
||||
int dx = get_global_id(0); |
||||
int dy = get_global_id(1); |
||||
|
||||
float sx = ((dx+0.5f) * ifx - 0.5f), sy = ((dy+0.5f) * ify - 0.5f); |
||||
int x = floor(sx), y = floor(sy); |
||||
|
||||
float u = sx - x, v = sy - y; |
||||
|
||||
if ( x<0 ) x=0,u=0; |
||||
if ( x>=srccols ) x=srccols-1,u=0; |
||||
if ( y<0 ) y=0,v=0; |
||||
if ( y>=srcrows ) y=srcrows-1,v=0; |
||||
|
||||
int y_ = INC(y,srcrows); |
||||
int x_ = INC(x,srccols); |
||||
const PIXTYPE* src = (const PIXTYPE*)(srcptr + mad24(y, srcstep, srcoffset + x*PIXSIZE)); |
||||
|
||||
#if depth == 0 |
||||
u = u * INTER_RESIZE_COEF_SCALE; |
||||
v = v * INTER_RESIZE_COEF_SCALE; |
||||
|
||||
int U = rint(u); |
||||
int V = rint(v); |
||||
int U1 = rint(INTER_RESIZE_COEF_SCALE - u); |
||||
int V1 = rint(INTER_RESIZE_COEF_SCALE - v); |
||||
|
||||
WORKTYPE data0 = convertToWT(*(const PIXTYPE*)(srcptr + mad24(y, srcstep, srcoffset + x*PIXSIZE))); |
||||
WORKTYPE data1 = convertToWT(*(const PIXTYPE*)(srcptr + mad24(y, srcstep, srcoffset + x_*PIXSIZE))); |
||||
WORKTYPE data2 = convertToWT(*(const PIXTYPE*)(srcptr + mad24(y_, srcstep, srcoffset + x*PIXSIZE))); |
||||
WORKTYPE data3 = convertToWT(*(const PIXTYPE*)(srcptr + mad24(y_, srcstep, srcoffset + x_*PIXSIZE))); |
||||
WORKTYPE val = mul24((WORKTYPE)mul24(U1, V1), data0) + mul24((WORKTYPE)mul24(U, V1), data1) + |
||||
mul24((WORKTYPE)mul24(U1, V), data2) + mul24((WORKTYPE)mul24(U, V), data3); |
||||
|
||||
PIXTYPE uval = convertToDT((val + (1<<(CAST_BITS-1)))>>CAST_BITS); |
||||
#else |
||||
float u1 = 1.f-u; |
||||
float v1 = 1.f-v; |
||||
WORKTYPE data0 = convertToWT(*(const PIXTYPE*)(srcptr + mad24(y, srcstep, srcoffset + x*PIXSIZE))); |
||||
WORKTYPE data1 = convertToWT(*(const PIXTYPE*)(srcptr + mad24(y, srcstep, srcoffset + x_*PIXSIZE))); |
||||
WORKTYPE data2 = convertToWT(*(const PIXTYPE*)(srcptr + mad24(y_, srcstep, srcoffset + x*PIXSIZE))); |
||||
WORKTYPE data3 = convertToWT(*(const PIXTYPE*)(srcptr + mad24(y_, srcstep, srcoffset + x_*PIXSIZE))); |
||||
PIXTYPE uval = u1 * v1 * s_data1 + u * v1 * s_data2 + u1 * v *s_data3 + u * v *s_data4; |
||||
#endif |
||||
|
||||
if(dx < dstcols && dy < dstrows) |
||||
{ |
||||
PIXTYPE* dst = (PIXTYPE*)(dstptr + mad24(dy, dststep, dstoffset + dx*PIXSIZE)); |
||||
dst[0] = uval; |
||||
} |
||||
} |
||||
|
||||
#elif defined INTER_NEAREST |
||||
|
||||
__kernel void resizeNN(__global const uchar* srcptr, int srcstep, int srcoffset, |
||||
int srcrows, int srccols, |
||||
__global uchar* dstptr, int dststep, int dstoffset, |
||||
int dstrows, int dstcols, |
||||
float ifx, float ify) |
||||
{ |
||||
int dx = get_global_id(0); |
||||
int dy = get_global_id(1); |
||||
|
||||
if( dx < dstcols && dy < dstrows ) |
||||
{ |
||||
F s1 = dx*ifx; |
||||
F s2 = dy*ify; |
||||
int sx = min(convert_int_rtz(s1), srccols-1); |
||||
int sy = min(convert_int_rtz(s2), srcrows-1); |
||||
PIXTYPE* dst = (PIXTYPE*)(dstptr + |
||||
mad24(dy, dststep, dstoffset + dx*PIXSIZE)); |
||||
const PIXTYPE* src = (const PIXTYPE*)(srcptr + |
||||
mad24(sy, srcstep, srcoffset + sx*PIXSIZE)); |
||||
dst[0] = src[0]; |
||||
} |
||||
} |
||||
|
||||
#endif |
@ -0,0 +1,82 @@ |
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other 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*/
|
||||
|
||||
#include "test_precomp.hpp" |
||||
#include <string> |
||||
|
||||
using namespace cv; |
||||
using namespace std; |
||||
|
||||
class CV_ImgprocUMatTest : public cvtest::BaseTest |
||||
{ |
||||
public: |
||||
CV_ImgprocUMatTest() {} |
||||
~CV_ImgprocUMatTest() {} |
||||
protected: |
||||
void run(int) |
||||
{ |
||||
string imgpath = string(ts->get_data_path()) + "shared/lena.png"; |
||||
Mat img = imread(imgpath, 1), gray, smallimg, result; |
||||
UMat uimg = img.getUMat(ACCESS_READ), ugray, usmallimg, uresult; |
||||
|
||||
cvtColor(img, gray, COLOR_BGR2GRAY); |
||||
resize(gray, smallimg, Size(), 0.75, 0.75, INTER_LINEAR); |
||||
equalizeHist(smallimg, result); |
||||
|
||||
cvtColor(uimg, ugray, COLOR_BGR2GRAY); |
||||
resize(ugray, usmallimg, Size(), 0.75, 0.75, INTER_LINEAR); |
||||
equalizeHist(usmallimg, uresult); |
||||
|
||||
#if 0 |
||||
imshow("orig", uimg); |
||||
imshow("small", usmallimg); |
||||
imshow("equalized gray", uresult); |
||||
waitKey(); |
||||
destroyWindow("orig"); |
||||
destroyWindow("small"); |
||||
destroyWindow("equalized gray"); |
||||
#endif |
||||
ts->set_failed_test_info(cvtest::TS::OK); |
||||
} |
||||
}; |
||||
|
||||
TEST(Imgproc_UMat, regression) { CV_ImgprocUMatTest test; test.safe_run(); } |
@ -0,0 +1,276 @@ |
||||
#include "opencv2/objdetect.hpp" |
||||
#include "opencv2/highgui.hpp" |
||||
#include "opencv2/imgproc.hpp" |
||||
#include "opencv2/core/utility.hpp" |
||||
#include "opencv2/core/ocl.hpp" |
||||
|
||||
#include <cctype> |
||||
#include <iostream> |
||||
#include <iterator> |
||||
#include <stdio.h> |
||||
|
||||
using namespace std; |
||||
using namespace cv; |
||||
|
||||
static void help() |
||||
{ |
||||
cout << "\nThis program demonstrates the cascade recognizer. Now you can use Haar or LBP features.\n" |
||||
"This classifier can recognize many kinds of rigid objects, once the appropriate classifier is trained.\n" |
||||
"It's most known use is for faces.\n" |
||||
"Usage:\n" |
||||
"./facedetect [--cascade=<cascade_path> this is the primary trained classifier such as frontal face]\n" |
||||
" [--nested-cascade[=nested_cascade_path this an optional secondary classifier such as eyes]]\n" |
||||
" [--scale=<image scale greater or equal to 1, try 1.3 for example>]\n" |
||||
" [--try-flip]\n" |
||||
" [filename|camera_index]\n\n" |
||||
"see facedetect.cmd for one call:\n" |
||||
"./facedetect --cascade=\"../../data/haarcascades/haarcascade_frontalface_alt.xml\" --nested-cascade=\"../../data/haarcascades/haarcascade_eye.xml\" --scale=1.3\n\n" |
||||
"During execution:\n\tHit any key to quit.\n" |
||||
"\tUsing OpenCV version " << CV_VERSION << "\n" << endl; |
||||
} |
||||
|
||||
void detectAndDraw( UMat& img, Mat& canvas, CascadeClassifier& cascade, |
||||
CascadeClassifier& nestedCascade, |
||||
double scale, bool tryflip ); |
||||
|
||||
string cascadeName = "../../data/haarcascades/haarcascade_frontalface_alt.xml"; |
||||
string nestedCascadeName = "../../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml"; |
||||
|
||||
int main( int argc, const char** argv ) |
||||
{ |
||||
VideoCapture capture; |
||||
UMat frame, image; |
||||
Mat canvas; |
||||
const string scaleOpt = "--scale="; |
||||
size_t scaleOptLen = scaleOpt.length(); |
||||
const string cascadeOpt = "--cascade="; |
||||
size_t cascadeOptLen = cascadeOpt.length(); |
||||
const string nestedCascadeOpt = "--nested-cascade"; |
||||
size_t nestedCascadeOptLen = nestedCascadeOpt.length(); |
||||
const string tryFlipOpt = "--try-flip"; |
||||
size_t tryFlipOptLen = tryFlipOpt.length(); |
||||
String inputName; |
||||
bool tryflip = false; |
||||
|
||||
help(); |
||||
|
||||
CascadeClassifier cascade, nestedCascade; |
||||
double scale = 1; |
||||
|
||||
for( int i = 1; i < argc; i++ ) |
||||
{ |
||||
cout << "Processing " << i << " " << argv[i] << endl; |
||||
if( cascadeOpt.compare( 0, cascadeOptLen, argv[i], cascadeOptLen ) == 0 ) |
||||
{ |
||||
cascadeName.assign( argv[i] + cascadeOptLen ); |
||||
cout << " from which we have cascadeName= " << cascadeName << endl; |
||||
} |
||||
else if( nestedCascadeOpt.compare( 0, nestedCascadeOptLen, argv[i], nestedCascadeOptLen ) == 0 ) |
||||
{ |
||||
if( argv[i][nestedCascadeOpt.length()] == '=' ) |
||||
nestedCascadeName.assign( argv[i] + nestedCascadeOpt.length() + 1 ); |
||||
if( !nestedCascade.load( nestedCascadeName ) ) |
||||
cerr << "WARNING: Could not load classifier cascade for nested objects" << endl; |
||||
} |
||||
else if( scaleOpt.compare( 0, scaleOptLen, argv[i], scaleOptLen ) == 0 ) |
||||
{ |
||||
if( !sscanf( argv[i] + scaleOpt.length(), "%lf", &scale ) || scale > 1 ) |
||||
scale = 1; |
||||
cout << " from which we read scale = " << scale << endl; |
||||
} |
||||
else if( tryFlipOpt.compare( 0, tryFlipOptLen, argv[i], tryFlipOptLen ) == 0 ) |
||||
{ |
||||
tryflip = true; |
||||
cout << " will try to flip image horizontally to detect assymetric objects\n"; |
||||
} |
||||
else if( argv[i][0] == '-' ) |
||||
{ |
||||
cerr << "WARNING: Unknown option %s" << argv[i] << endl; |
||||
} |
||||
else |
||||
inputName = argv[i]; |
||||
} |
||||
|
||||
if( !cascade.load( cascadeName ) ) |
||||
{ |
||||
cerr << "ERROR: Could not load classifier cascade" << endl; |
||||
help(); |
||||
return -1; |
||||
} |
||||
|
||||
if( inputName.empty() || (isdigit(inputName.c_str()[0]) && inputName.c_str()[1] == '\0') ) |
||||
{ |
||||
int c = inputName.empty() ? 0 : inputName.c_str()[0] - '0'; |
||||
if(!capture.open(c)) |
||||
cout << "Capture from camera #" << c << " didn't work" << endl; |
||||
} |
||||
else |
||||
{ |
||||
if( inputName.empty() ) |
||||
inputName = "lena.jpg"; |
||||
image = imread( inputName, 1 ).getUMat(ACCESS_READ); |
||||
if( image.empty() ) |
||||
{ |
||||
if(!capture.open( inputName )) |
||||
cout << "Could not read " << inputName << endl; |
||||
} |
||||
} |
||||
|
||||
namedWindow( "result", 1 ); |
||||
|
||||
if( capture.isOpened() ) |
||||
{ |
||||
cout << "Video capturing has been started ..." << endl; |
||||
for(;;) |
||||
{ |
||||
capture >> frame; |
||||
if( frame.empty() ) |
||||
break; |
||||
|
||||
detectAndDraw( frame, canvas, cascade, nestedCascade, scale, tryflip ); |
||||
|
||||
if( waitKey( 10 ) >= 0 ) |
||||
break; |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
cout << "Detecting face(s) in " << inputName << endl; |
||||
if( !image.empty() ) |
||||
{ |
||||
detectAndDraw( image, canvas, cascade, nestedCascade, scale, tryflip ); |
||||
waitKey(0); |
||||
} |
||||
else if( !inputName.empty() ) |
||||
{ |
||||
/* assume it is a text file containing the
|
||||
list of the image filenames to be processed - one per line */ |
||||
FILE* f = fopen( inputName.c_str(), "rt" ); |
||||
if( f ) |
||||
{ |
||||
char buf[1000+1]; |
||||
while( fgets( buf, 1000, f ) ) |
||||
{ |
||||
int len = (int)strlen(buf), c; |
||||
while( len > 0 && isspace(buf[len-1]) ) |
||||
len--; |
||||
buf[len] = '\0'; |
||||
cout << "file " << buf << endl; |
||||
image = imread( buf, 1 ).getUMat(ACCESS_READ); |
||||
if( !image.empty() ) |
||||
{ |
||||
detectAndDraw( image, canvas, cascade, nestedCascade, scale, tryflip ); |
||||
c = waitKey(0); |
||||
if( c == 27 || c == 'q' || c == 'Q' ) |
||||
break; |
||||
} |
||||
else |
||||
{ |
||||
cerr << "Aw snap, couldn't read image " << buf << endl; |
||||
} |
||||
} |
||||
fclose(f); |
||||
} |
||||
} |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
void detectAndDraw( UMat& img, Mat& canvas, CascadeClassifier& cascade, |
||||
CascadeClassifier& nestedCascade, |
||||
double scale0, bool tryflip ) |
||||
{ |
||||
int i = 0; |
||||
double t = 0, scale=1; |
||||
vector<Rect> faces, faces2; |
||||
const static Scalar colors[] = |
||||
{ |
||||
Scalar(0,0,255), |
||||
Scalar(0,128,255), |
||||
Scalar(0,255,255), |
||||
Scalar(0,255,0), |
||||
Scalar(255,128,0), |
||||
Scalar(255,255,0), |
||||
Scalar(255,0,0), |
||||
Scalar(255,0,255) |
||||
}; |
||||
static UMat gray, smallImg; |
||||
|
||||
t = (double)getTickCount(); |
||||
|
||||
cvtColor( img, gray, COLOR_BGR2GRAY ); |
||||
resize( gray, smallImg, Size(), scale0, scale0, INTER_LINEAR ); |
||||
cvtColor(smallImg, canvas, COLOR_GRAY2BGR); |
||||
equalizeHist( smallImg, smallImg ); |
||||
|
||||
cascade.detectMultiScale( smallImg, faces, |
||||
1.1, 2, 0 |
||||
//|CASCADE_FIND_BIGGEST_OBJECT
|
||||
//|CASCADE_DO_ROUGH_SEARCH
|
||||
|CASCADE_SCALE_IMAGE |
||||
, |
||||
Size(30, 30) ); |
||||
if( tryflip ) |
||||
{ |
||||
flip(smallImg, smallImg, 1); |
||||
cascade.detectMultiScale( smallImg, faces2, |
||||
1.1, 2, 0 |
||||
//|CASCADE_FIND_BIGGEST_OBJECT
|
||||
//|CASCADE_DO_ROUGH_SEARCH
|
||||
|CASCADE_SCALE_IMAGE |
||||
, |
||||
Size(30, 30) ); |
||||
for( vector<Rect>::const_iterator r = faces2.begin(); r != faces2.end(); r++ ) |
||||
{ |
||||
faces.push_back(Rect(smallImg.cols - r->x - r->width, r->y, r->width, r->height)); |
||||
} |
||||
} |
||||
t = (double)getTickCount() - t; |
||||
cvtColor(smallImg, canvas, COLOR_GRAY2BGR); |
||||
|
||||
double fps = getTickFrequency()/t; |
||||
|
||||
putText(canvas, format("OpenCL: %s, fps: %.1f", ocl::useOpenCL() ? "ON" : "OFF", fps), Point(250, 50), |
||||
FONT_HERSHEY_SIMPLEX, 1, Scalar(0,255,0), 3); |
||||
|
||||
for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ ) |
||||
{ |
||||
vector<Rect> nestedObjects; |
||||
Point center; |
||||
Scalar color = colors[i%8]; |
||||
int radius; |
||||
|
||||
double aspect_ratio = (double)r->width/r->height; |
||||
if( 0.75 < aspect_ratio && aspect_ratio < 1.3 ) |
||||
{ |
||||
center.x = cvRound((r->x + r->width*0.5)*scale); |
||||
center.y = cvRound((r->y + r->height*0.5)*scale); |
||||
radius = cvRound((r->width + r->height)*0.25*scale); |
||||
circle( canvas, center, radius, color, 3, 8, 0 ); |
||||
} |
||||
else |
||||
rectangle( canvas, Point(cvRound(r->x*scale), cvRound(r->y*scale)), |
||||
Point(cvRound((r->x + r->width-1)*scale), cvRound((r->y + r->height-1)*scale)), |
||||
color, 3, 8, 0); |
||||
if( nestedCascade.empty() ) |
||||
continue; |
||||
UMat smallImgROI = smallImg(*r); |
||||
nestedCascade.detectMultiScale( smallImgROI, nestedObjects, |
||||
1.1, 2, 0 |
||||
//|CASCADE_FIND_BIGGEST_OBJECT
|
||||
//|CASCADE_DO_ROUGH_SEARCH
|
||||
//|CASCADE_DO_CANNY_PRUNING
|
||||
|CASCADE_SCALE_IMAGE |
||||
, |
||||
Size(30, 30) ); |
||||
for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ ) |
||||
{ |
||||
center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale); |
||||
center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale); |
||||
radius = cvRound((nr->width + nr->height)*0.25*scale); |
||||
circle( canvas, center, radius, color, 3, 8, 0 ); |
||||
} |
||||
} |
||||
imshow( "result", canvas ); |
||||
} |
Loading…
Reference in new issue