From 0b365f6aa564766c236189d577d0c33b1209ca35 Mon Sep 17 00:00:00 2001 From: yao Date: Fri, 8 Feb 2013 11:41:46 +0800 Subject: [PATCH 1/3] add +-*/ operators to oclMat --- modules/ocl/include/opencv2/ocl/ocl.hpp | 11 ++++++++++ modules/ocl/src/arithm.cpp | 29 +++++++++++++++++++++++++ modules/ocl/src/matrix_operations.cpp | 25 +++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/modules/ocl/include/opencv2/ocl/ocl.hpp b/modules/ocl/include/opencv2/ocl/ocl.hpp index 5e4b143f8..e86207d50 100644 --- a/modules/ocl/include/opencv2/ocl/ocl.hpp +++ b/modules/ocl/include/opencv2/ocl/ocl.hpp @@ -219,6 +219,11 @@ namespace cv oclMat operator()( Range rowRange, Range colRange ) const; oclMat operator()( const Rect &roi ) const; + oclMat& operator+=( const oclMat& m ); + oclMat& operator-=( const oclMat& m ); + oclMat& operator*=( const oclMat& m ); + oclMat& operator/=( const oclMat& m ); + //! returns true if the oclMatrix data is continuous // (i.e. when there are no gaps between successive rows). // similar to CV_IS_oclMat_CONT(cvoclMat->type) @@ -468,6 +473,12 @@ namespace cv CV_EXPORTS oclMat operator ^ (const oclMat &src1, const oclMat &src2); CV_EXPORTS void cvtColor(const oclMat &src, oclMat &dst, int code , int dcn = 0); + //! Mathematics operators + CV_EXPORTS oclMat operator + (const oclMat &src1, const oclMat &src2); + CV_EXPORTS oclMat operator - (const oclMat &src1, const oclMat &src2); + CV_EXPORTS oclMat operator * (const oclMat &src1, const oclMat &src2); + CV_EXPORTS oclMat operator / (const oclMat &src1, const oclMat &src2); + //////////////////////////////// Filter Engine //////////////////////////////// /*! diff --git a/modules/ocl/src/arithm.cpp b/modules/ocl/src/arithm.cpp index de8f4343b..e43cf5096 100644 --- a/modules/ocl/src/arithm.cpp +++ b/modules/ocl/src/arithm.cpp @@ -12,6 +12,7 @@ // // 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) 2010-2012, Multicoreware, Inc., all rights reserved. // Third party copyrights are property of their respective owners. // // @Authors @@ -2152,6 +2153,34 @@ cv::ocl::oclMat cv::ocl::operator ^ (const oclMat &src1, const oclMat &src2) return dst; } +cv::ocl::oclMat cv::ocl::operator + (const oclMat &src1, const oclMat &src2) +{ + oclMat dst; + add(src1, src2, dst); + return dst; +} + +cv::ocl::oclMat cv::ocl::operator - (const oclMat &src1, const oclMat &src2) +{ + oclMat dst; + subtract(src1, src2, dst); + return dst; +} + +cv::ocl::oclMat cv::ocl::operator * (const oclMat &src1, const oclMat &src2) +{ + oclMat dst; + multiply(src1, src2, dst); + return dst; +} + +cv::ocl::oclMat cv::ocl::operator / (const oclMat &src1, const oclMat &src2) +{ + oclMat dst; + divide(src1, src2, dst); + return dst; +} + ////////////////////////////////////////////////////////////////////////////// /////////////////////////////// transpose //////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// diff --git a/modules/ocl/src/matrix_operations.cpp b/modules/ocl/src/matrix_operations.cpp index b2b8b5ff2..7b90218d9 100644 --- a/modules/ocl/src/matrix_operations.cpp +++ b/modules/ocl/src/matrix_operations.cpp @@ -12,10 +12,12 @@ // // 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) 2010-2012, Multicoreware, Inc., all rights reserved. // Third party copyrights are property of their respective owners. // // @Authors // Niko Li, newlife20080214@gmail.com +// Yao Wang, bitwangyaoyao@gmail.com // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: @@ -1020,4 +1022,27 @@ void cv::ocl::oclMat::release() refcount = 0; } +oclMat& cv::ocl::oclMat::operator+=( const oclMat& m ) +{ + add(*this, m, *this); + return *this; +} + +oclMat& cv::ocl::oclMat::operator-=( const oclMat& m ) +{ + subtract(*this, m, *this); + return *this; +} + +oclMat& cv::ocl::oclMat::operator*=( const oclMat& m ) +{ + multiply(*this, m, *this); + return *this; +} + +oclMat& cv::ocl::oclMat::operator/=( const oclMat& m ) +{ + divide(*this, m, *this); + return *this; +} #endif /* !defined (HAVE_OPENCL) */ From 69fd2d82735f14b88b9cb3b08eb5cdd527fdb9e8 Mon Sep 17 00:00:00 2001 From: yao Date: Sat, 16 Feb 2013 11:05:23 +0800 Subject: [PATCH 2/3] add oclMatExpr class to prevent extra allocations --- .../include/opencv2/ocl/matrix_operations.hpp | 28 ++++++++++- modules/ocl/include/opencv2/ocl/ocl.hpp | 25 +++++----- modules/ocl/src/arithm.cpp | 46 ++++++++++++++----- 3 files changed, 73 insertions(+), 26 deletions(-) diff --git a/modules/ocl/include/opencv2/ocl/matrix_operations.hpp b/modules/ocl/include/opencv2/ocl/matrix_operations.hpp index 4b617ce07..13b19bed6 100644 --- a/modules/ocl/include/opencv2/ocl/matrix_operations.hpp +++ b/modules/ocl/include/opencv2/ocl/matrix_operations.hpp @@ -48,9 +48,27 @@ namespace cv namespace ocl { - ////////////////////////////////////OpenCL kernel strings////////////////////////// - //extern const char *convertC3C4; + enum + { + MAT_ADD, + MAT_SUB, + MAT_MUL, + MAT_DIV + }; + + class oclMatExpr + { + public: + oclMatExpr(const oclMat& _a, const oclMat& _b, int _op) + : a(_a), b(_b), op(_op){} + operator oclMat() const; + void assign(oclMat& m) const; + + protected: + int op; + oclMat a, b; + }; //////////////////////////////////////////////////////////////////////// //////////////////////////////// oclMat //////////////////////////////// //////////////////////////////////////////////////////////////////////// @@ -237,6 +255,12 @@ namespace cv return *this; } + oclMat& oclMat::operator = (const oclMatExpr& expr) + { + expr.assign(*this); + return *this; + } + /* Fixme! To be supported in OpenCL later. */ #if 0 template inline oclMat::operator DevMem2D_() const diff --git a/modules/ocl/include/opencv2/ocl/ocl.hpp b/modules/ocl/include/opencv2/ocl/ocl.hpp index e86207d50..83cfb5a26 100644 --- a/modules/ocl/include/opencv2/ocl/ocl.hpp +++ b/modules/ocl/include/opencv2/ocl/ocl.hpp @@ -119,6 +119,7 @@ namespace cv Impl *impl; }; + class CV_EXPORTS oclMatExpr; //////////////////////////////// oclMat //////////////////////////////// class CV_EXPORTS oclMat { @@ -152,7 +153,7 @@ namespace cv oclMat &operator = (const oclMat &m); //! assignment operator. Perfom blocking upload to device. oclMat &operator = (const Mat &m); - + oclMat& operator = (const oclMatExpr& expr); //! pefroms blocking upload data to oclMat. void upload(const cv::Mat &m); @@ -296,6 +297,7 @@ namespace cv int wholecols; }; + ///////////////////// mat split and merge ///////////////////////////////// //! Compose a multi-channel array from several single-channel arrays // Support all types @@ -459,25 +461,24 @@ namespace cv // supports all types CV_EXPORTS void bitwise_xor(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat()); CV_EXPORTS void bitwise_xor(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat()); - //! computes convolution of two images - - //! support only CV_32FC1 type - - CV_EXPORTS void convolve(const oclMat &image, const oclMat &temp1, oclMat &result); - //! Logical operators CV_EXPORTS oclMat operator ~ (const oclMat &src); CV_EXPORTS oclMat operator | (const oclMat &src1, const oclMat &src2); CV_EXPORTS oclMat operator & (const oclMat &src1, const oclMat &src2); CV_EXPORTS oclMat operator ^ (const oclMat &src1, const oclMat &src2); - CV_EXPORTS void cvtColor(const oclMat &src, oclMat &dst, int code , int dcn = 0); //! Mathematics operators - CV_EXPORTS oclMat operator + (const oclMat &src1, const oclMat &src2); - CV_EXPORTS oclMat operator - (const oclMat &src1, const oclMat &src2); - CV_EXPORTS oclMat operator * (const oclMat &src1, const oclMat &src2); - CV_EXPORTS oclMat operator / (const oclMat &src1, const oclMat &src2); + CV_EXPORTS oclMatExpr operator + (const oclMat &src1, const oclMat &src2); + CV_EXPORTS oclMatExpr operator - (const oclMat &src1, const oclMat &src2); + CV_EXPORTS oclMatExpr operator * (const oclMat &src1, const oclMat &src2); + CV_EXPORTS oclMatExpr operator / (const oclMat &src1, const oclMat &src2); + + //! computes convolution of two images + //! support only CV_32FC1 type + CV_EXPORTS void convolve(const oclMat &image, const oclMat &temp1, oclMat &result); + + CV_EXPORTS void cvtColor(const oclMat &src, oclMat &dst, int code , int dcn = 0); //////////////////////////////// Filter Engine //////////////////////////////// diff --git a/modules/ocl/src/arithm.cpp b/modules/ocl/src/arithm.cpp index e43cf5096..b9174e7b5 100644 --- a/modules/ocl/src/arithm.cpp +++ b/modules/ocl/src/arithm.cpp @@ -2153,34 +2153,56 @@ cv::ocl::oclMat cv::ocl::operator ^ (const oclMat &src1, const oclMat &src2) return dst; } -cv::ocl::oclMat cv::ocl::operator + (const oclMat &src1, const oclMat &src2) +cv::ocl::oclMatExpr cv::ocl::operator + (const oclMat &src1, const oclMat &src2) { - oclMat dst; - add(src1, src2, dst); + oclMatExpr dst(src1, src2, cv::ocl::MAT_ADD); return dst; } -cv::ocl::oclMat cv::ocl::operator - (const oclMat &src1, const oclMat &src2) +cv::ocl::oclMatExpr cv::ocl::operator - (const oclMat &src1, const oclMat &src2) { - oclMat dst; - subtract(src1, src2, dst); + oclMatExpr dst(src1, src2, cv::ocl::MAT_SUB); return dst; } -cv::ocl::oclMat cv::ocl::operator * (const oclMat &src1, const oclMat &src2) +cv::ocl::oclMatExpr cv::ocl::operator * (const oclMat &src1, const oclMat &src2) { - oclMat dst; - multiply(src1, src2, dst); + oclMatExpr dst(src1, src2, cv::ocl::MAT_MUL); return dst; } -cv::ocl::oclMat cv::ocl::operator / (const oclMat &src1, const oclMat &src2) +cv::ocl::oclMatExpr cv::ocl::operator / (const oclMat &src1, const oclMat &src2) { - oclMat dst; - divide(src1, src2, dst); + oclMatExpr dst(src1, src2, cv::ocl::MAT_DIV); return dst; } +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; + } +} + +oclMatExpr::operator oclMat() const +{ + oclMat m; + assign(m); + return m; +} + ////////////////////////////////////////////////////////////////////////////// /////////////////////////////// transpose //////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// From 9613135e8dad2a1beed03d190b5f9facf2f55f7f Mon Sep 17 00:00:00 2001 From: yao Date: Sat, 16 Feb 2013 18:41:43 +0800 Subject: [PATCH 3/3] fix compiling errors on Linux more operators use oclMatExpr --- .../include/opencv2/ocl/matrix_operations.hpp | 18 ++++--- modules/ocl/include/opencv2/ocl/ocl.hpp | 12 ++--- modules/ocl/src/arithm.cpp | 48 +++++++++---------- 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/modules/ocl/include/opencv2/ocl/matrix_operations.hpp b/modules/ocl/include/opencv2/ocl/matrix_operations.hpp index 13b19bed6..fcd847cb6 100644 --- a/modules/ocl/include/opencv2/ocl/matrix_operations.hpp +++ b/modules/ocl/include/opencv2/ocl/matrix_operations.hpp @@ -12,6 +12,7 @@ // // 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) 2010-2012, Multicoreware, 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, @@ -51,23 +52,28 @@ namespace cv enum { - MAT_ADD, + MAT_ADD = 1, MAT_SUB, MAT_MUL, - MAT_DIV + MAT_DIV, + MAT_NOT, + MAT_AND, + MAT_OR, + MAT_XOR }; - class oclMatExpr + class CV_EXPORTS oclMatExpr { public: + oclMatExpr() : a(oclMat()), b(oclMat()), op(0) {} oclMatExpr(const oclMat& _a, const oclMat& _b, int _op) - : a(_a), b(_b), op(_op){} + : a(_a), b(_b), op(_op) {} operator oclMat() const; void assign(oclMat& m) const; protected: - int op; oclMat a, b; + int op; }; //////////////////////////////////////////////////////////////////////// //////////////////////////////// oclMat //////////////////////////////// @@ -255,7 +261,7 @@ namespace cv return *this; } - oclMat& oclMat::operator = (const oclMatExpr& expr) + inline oclMat& oclMat::operator = (const oclMatExpr& expr) { expr.assign(*this); return *this; diff --git a/modules/ocl/include/opencv2/ocl/ocl.hpp b/modules/ocl/include/opencv2/ocl/ocl.hpp index 83cfb5a26..57d18d1ed 100644 --- a/modules/ocl/include/opencv2/ocl/ocl.hpp +++ b/modules/ocl/include/opencv2/ocl/ocl.hpp @@ -153,7 +153,7 @@ namespace cv oclMat &operator = (const oclMat &m); //! assignment operator. Perfom blocking upload to device. oclMat &operator = (const Mat &m); - oclMat& operator = (const oclMatExpr& expr); + oclMat &operator = (const oclMatExpr& expr); //! pefroms blocking upload data to oclMat. void upload(const cv::Mat &m); @@ -463,10 +463,10 @@ namespace cv CV_EXPORTS void bitwise_xor(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat()); //! Logical operators - CV_EXPORTS oclMat operator ~ (const oclMat &src); - CV_EXPORTS oclMat operator | (const oclMat &src1, const oclMat &src2); - CV_EXPORTS oclMat operator & (const oclMat &src1, const oclMat &src2); - CV_EXPORTS oclMat operator ^ (const oclMat &src1, const oclMat &src2); + CV_EXPORTS oclMatExpr operator ~ (const oclMat &src); + CV_EXPORTS oclMatExpr operator | (const oclMat &src1, const oclMat &src2); + CV_EXPORTS oclMatExpr operator & (const oclMat &src1, const oclMat &src2); + CV_EXPORTS oclMatExpr operator ^ (const oclMat &src1, const oclMat &src2); //! Mathematics operators CV_EXPORTS oclMatExpr operator + (const oclMat &src1, const oclMat &src2); @@ -478,7 +478,7 @@ namespace cv //! support only CV_32FC1 type CV_EXPORTS void convolve(const oclMat &image, const oclMat &temp1, oclMat &result); - CV_EXPORTS void cvtColor(const oclMat &src, oclMat &dst, int code , int dcn = 0); + CV_EXPORTS void cvtColor(const oclMat &src, oclMat &dst, int code , int dcn = 0); //////////////////////////////// Filter Engine //////////////////////////////// diff --git a/modules/ocl/src/arithm.cpp b/modules/ocl/src/arithm.cpp index b9174e7b5..f98b14358 100644 --- a/modules/ocl/src/arithm.cpp +++ b/modules/ocl/src/arithm.cpp @@ -2125,56 +2125,44 @@ void cv::ocl::bitwise_xor(const oclMat &src1, const Scalar &src2, oclMat &dst, c bitwise_scalar( src1, src2, dst, mask, kernelName, &arithm_bitwise_xor_scalar); } -cv::ocl::oclMat cv::ocl::operator ~ (const oclMat &src) +oclMatExpr cv::ocl::operator ~ (const oclMat &src) { - oclMat dst; - bitwise_not(src, dst); - return dst; + return oclMatExpr(src, oclMat(), MAT_NOT); } -cv::ocl::oclMat cv::ocl::operator | (const oclMat &src1, const oclMat &src2) +oclMatExpr cv::ocl::operator | (const oclMat &src1, const oclMat &src2) { - oclMat dst; - bitwise_or(src1, src2, dst); - return dst; + return oclMatExpr(src1, src2, MAT_OR); } -cv::ocl::oclMat cv::ocl::operator & (const oclMat &src1, const oclMat &src2) +oclMatExpr cv::ocl::operator & (const oclMat &src1, const oclMat &src2) { - oclMat dst; - bitwise_and(src1, src2, dst); - return dst; + return oclMatExpr(src1, src2, MAT_AND); } -cv::ocl::oclMat cv::ocl::operator ^ (const oclMat &src1, const oclMat &src2) +oclMatExpr cv::ocl::operator ^ (const oclMat &src1, const oclMat &src2) { - oclMat dst; - bitwise_xor(src1, src2, dst); - return dst; + return oclMatExpr(src1, src2, MAT_XOR); } cv::ocl::oclMatExpr cv::ocl::operator + (const oclMat &src1, const oclMat &src2) { - oclMatExpr dst(src1, src2, cv::ocl::MAT_ADD); - return dst; + return oclMatExpr(src1, src2, cv::ocl::MAT_ADD); } cv::ocl::oclMatExpr cv::ocl::operator - (const oclMat &src1, const oclMat &src2) { - oclMatExpr dst(src1, src2, cv::ocl::MAT_SUB); - return dst; + return oclMatExpr(src1, src2, cv::ocl::MAT_SUB); } cv::ocl::oclMatExpr cv::ocl::operator * (const oclMat &src1, const oclMat &src2) { - oclMatExpr dst(src1, src2, cv::ocl::MAT_MUL); - return dst; + return oclMatExpr(src1, src2, cv::ocl::MAT_MUL); } cv::ocl::oclMatExpr cv::ocl::operator / (const oclMat &src1, const oclMat &src2) { - oclMatExpr dst(src1, src2, cv::ocl::MAT_DIV); - return dst; + return oclMatExpr(src1, src2, cv::ocl::MAT_DIV); } void oclMatExpr::assign(oclMat& m) const @@ -2193,6 +2181,18 @@ void oclMatExpr::assign(oclMat& m) const case MAT_DIV: divide(a, b, m); break; + 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; } }