From 8f62a52b9bb16f32518dea8e8f6ebf4f01c9fe81 Mon Sep 17 00:00:00 2001 From: hbristow Date: Fri, 30 Aug 2013 12:39:01 +1000 Subject: [PATCH] Brought matlab module into cv namespace --- .../templates/template_class_base.cpp | 2 + .../templates/template_function_base.cpp | 2 + .../matlab/include/opencv2/matlab/bridge.hpp | 75 ++++--- modules/matlab/include/opencv2/matlab/map.hpp | 3 + .../matlab/include/opencv2/matlab/mxarray.hpp | 194 +++++++++--------- 5 files changed, 146 insertions(+), 130 deletions(-) diff --git a/modules/matlab/generator/templates/template_class_base.cpp b/modules/matlab/generator/templates/template_class_base.cpp index b2d5266fd..42d98ceb6 100644 --- a/modules/matlab/generator/templates/template_class_base.cpp +++ b/modules/matlab/generator/templates/template_class_base.cpp @@ -15,6 +15,8 @@ #include #include using namespace cv; +using namespace matlab; +using namespace bridge; namespace { diff --git a/modules/matlab/generator/templates/template_function_base.cpp b/modules/matlab/generator/templates/template_function_base.cpp index 95fb2fb22..e8e342425 100644 --- a/modules/matlab/generator/templates/template_function_base.cpp +++ b/modules/matlab/generator/templates/template_function_base.cpp @@ -15,6 +15,8 @@ #include #include using namespace cv; +using namespace matlab; +using namespace bridge; /* * {{ fun.name }} diff --git a/modules/matlab/include/opencv2/matlab/bridge.hpp b/modules/matlab/include/opencv2/matlab/bridge.hpp index a22373061..d11e7413b 100644 --- a/modules/matlab/include/opencv2/matlab/bridge.hpp +++ b/modules/matlab/include/opencv2/matlab/bridge.hpp @@ -49,6 +49,9 @@ #include #include +namespace cv { +namespace bridge { + /* * Custom typedefs * Parsed names from the hdr_parser @@ -72,10 +75,10 @@ typedef cv::Ptr Ptr_FeatureDetector; class Bridge; template -void deepCopyAndTranspose(const cv::Mat& src, MxArray& dst); +void deepCopyAndTranspose(const cv::Mat& src, matlab::MxArray& dst); template -void deepCopyAndTranspose(const MxArray& src, cv::Mat& dst); +void deepCopyAndTranspose(const matlab::MxArray& src, cv::Mat& dst); @@ -104,7 +107,7 @@ void deepCopyAndTranspose(const MxArray& src, cv::Mat& dst); * you can add your own custom type conversions. * * Because Matlab uses a homogeneous storage type, all operations are provided - * relative to Matlab's type. That is, Bridge always stores an MxArray object + * relative to Matlab's type. That is, Bridge always stores an matlab::MxArray object * and converts to and from other object types on demand. * * NOTE: for the explicit conversion function, the object name must be @@ -126,7 +129,7 @@ void deepCopyAndTranspose(const MxArray& src, cv::Mat& dst); */ class Bridge { private: - MxArray ptr_; + matlab::MxArray ptr_; public: // bridges are default constructible Bridge() {} @@ -145,11 +148,11 @@ public: // check that the object is actually of correct type before unpacking // TODO: Traverse class hierarchy? if (!ptr_.isClass(name)) { - error(std::string("Expected class ").append(std::string(name)) - .append(" but was given ").append(ptr_.className())); + matlab::error(std::string("Expected class ").append(std::string(name)) + .append(" but was given ").append(ptr_.className())); } // get the instance field - MxArray inst = ptr_.field("inst_"); + matlab::MxArray inst = ptr_.field("inst_"); Object* obj = NULL; // make sure the pointer is the correct size for the system if (sizeof(void *) == 8 && inst.ID() == mxUINT64_CLASS) { @@ -160,11 +163,11 @@ public: // 32-bit pointers obj = reinterpret_cast(inst.scalar()); } else { - error("Incorrect pointer type stored for architecture"); + matlab::error("Incorrect pointer type stored for architecture"); } // finally check if the object is NULL - conditionalError(obj, std::string("Object ").append(std::string(name)).append(std::string(" is NULL"))); + matlab::conditionalError(obj, std::string("Object ").append(std::string(name)).append(std::string(" is NULL"))); return obj; } @@ -173,10 +176,10 @@ public: // MATLAB TYPES // -------------------------------------------------------------------------- Bridge& operator=(const mxArray* obj) { ptr_ = obj; return *this; } - Bridge& operator=(const MxArray& obj) { ptr_ = obj; return *this; } - Bridge(const MxArray& obj) : ptr_(obj) {} + Bridge& operator=(const matlab::MxArray& obj) { ptr_ = obj; return *this; } + Bridge(const matlab::MxArray& obj) : ptr_(obj) {} Bridge(const mxArray* obj) : ptr_(obj) {} - MxArray toMxArray() { return ptr_; } + matlab::MxArray toMxArray() { return ptr_; } // -------------------------------------------------------------------------- @@ -187,8 +190,8 @@ public: operator cv::Mat() const { return toMat(); } template - static MxArray FromMat(const cv::Mat& mat) { - MxArray arr(mat.rows, mat.cols, mat.channels(), Matlab::Traits::ScalarType); + static matlab::MxArray FromMat(const cv::Mat& mat) { + matlab::MxArray arr(mat.rows, mat.cols, mat.channels(), matlab::Traits::ScalarType); switch (mat.depth()) { case CV_8U: deepCopyAndTranspose(mat, arr); break; case CV_8S: deepCopyAndTranspose(mat, arr); break; @@ -197,7 +200,7 @@ public: case CV_32S: deepCopyAndTranspose(mat, arr); break; case CV_32F: deepCopyAndTranspose(mat, arr); break; case CV_64F: deepCopyAndTranspose(mat, arr); break; - default: error("Attempted to convert from unknown class"); + default: matlab::error("Attempted to convert from unknown class"); } return arr; } @@ -218,7 +221,7 @@ public: case mxDOUBLE_CLASS: deepCopyAndTranspose(ptr_, mat); break; case mxCHAR_CLASS: deepCopyAndTranspose(ptr_, mat); break; case mxLOGICAL_CLASS: deepCopyAndTranspose(ptr_, mat); break; - default: error("Attempted to convert from unknown class"); + default: matlab::error("Attempted to convert from unknown class"); } return mat; } @@ -393,9 +396,13 @@ public: Ptr_FeatureDetector toPtrFeatureDetector() { return Ptr_FeatureDetector(); } operator Ptr_FeatureDetector() { return toPtrFeatureDetector(); } +}; // class Bridge -}; + +// -------------------------------------------------------------------------- +// SPECIALIZATIONS +// -------------------------------------------------------------------------- /*! * @brief template specialization for inheriting types @@ -407,7 +414,7 @@ public: * that gets mapped to an unsigned 8-bit value */ template <> -MxArray Bridge::FromMat(const cv::Mat& mat) { +matlab::MxArray Bridge::FromMat(const cv::Mat& mat) { switch (mat.depth()) { case CV_8U: return FromMat(mat); case CV_8S: return FromMat(mat); @@ -416,9 +423,9 @@ MxArray Bridge::FromMat(const cv::Mat& mat) { case CV_32S: return FromMat(mat); case CV_32F: return FromMat(mat); //NOTE: Matlab uses double as native type! case CV_64F: return FromMat(mat); - default: error("Attempted to convert from unknown class"); + default: matlab::error("Attempted to convert from unknown class"); } - return MxArray(); + return matlab::MxArray(); } /*! @@ -430,7 +437,7 @@ MxArray Bridge::FromMat(const cv::Mat& mat) { * to unsignd 8-bit value. */ template <> -cv::Mat Bridge::toMat() const { +cv::Mat Bridge::toMat() const { switch (ptr_.ID()) { case mxINT8_CLASS: return toMat(); case mxUINT8_CLASS: return toMat(); @@ -444,13 +451,13 @@ cv::Mat Bridge::toMat() const { case mxDOUBLE_CLASS: return toMat(); //NOTE: OpenCV uses float as native type! case mxCHAR_CLASS: return toMat(); case mxLOGICAL_CLASS: return toMat(); - default: error("Attempted to convert from unknown class"); + default: matlab::error("Attempted to convert from unknown class"); } return cv::Mat(); } -Bridge& Bridge::operator=(const cv::Mat& mat) { ptr_ = FromMat(mat); return *this; } -cv::Mat Bridge::toMat() const { return toMat(); } +Bridge& Bridge::operator=(const cv::Mat& mat) { ptr_ = FromMat(mat); return *this; } +cv::Mat Bridge::toMat() const { return toMat(); } // ---------------------------------------------------------------------------- @@ -459,10 +466,10 @@ cv::Mat Bridge::toMat() const { return toMat(); } template -void deepCopyAndTranspose(const cv::Mat& in, MxArray& out) { - conditionalError(static_cast(in.rows) == out.rows(), "Matrices must have the same number of rows"); - conditionalError(static_cast(in.cols) == out.cols(), "Matrices must have the same number of cols"); - conditionalError(static_cast(in.channels()) == out.channels(), "Matrices must have the same number of channels"); +void deepCopyAndTranspose(const cv::Mat& in, matlab::MxArray& out) { + matlab::conditionalError(static_cast(in.rows) == out.rows(), "Matrices must have the same number of rows"); + matlab::conditionalError(static_cast(in.cols) == out.cols(), "Matrices must have the same number of cols"); + matlab::conditionalError(static_cast(in.channels()) == out.channels(), "Matrices must have the same number of channels"); std::vector channels; cv::split(in, channels); for (size_t c = 0; c < out.channels(); ++c) { @@ -478,10 +485,10 @@ void deepCopyAndTranspose(const cv::Mat& in, MxArray& out) { } template -void deepCopyAndTranspose(const MxArray& in, cv::Mat& out) { - conditionalError(in.rows() == static_cast(out.rows), "Matrices must have the same number of rows"); - conditionalError(in.cols() == static_cast(out.cols), "Matrices must have the same number of cols"); - conditionalError(in.channels() == static_cast(out.channels()), "Matrices must have the same number of channels"); +void deepCopyAndTranspose(const matlab::MxArray& in, cv::Mat& out) { + matlab::conditionalError(in.rows() == static_cast(out.rows), "Matrices must have the same number of rows"); + matlab::conditionalError(in.cols() == static_cast(out.cols), "Matrices must have the same number of cols"); + matlab::conditionalError(in.channels() == static_cast(out.channels()), "Matrices must have the same number of channels"); std::vector channels; for (size_t c = 0; c < in.channels(); ++c) { cv::Mat outmat; @@ -499,4 +506,8 @@ void deepCopyAndTranspose(const MxArray& in, cv::Mat& out) { } + +} // namespace bridge +} // namespace cv + #endif diff --git a/modules/matlab/include/opencv2/matlab/map.hpp b/modules/matlab/include/opencv2/matlab/map.hpp index 2c32fd871..7d8d1d5cd 100644 --- a/modules/matlab/include/opencv2/matlab/map.hpp +++ b/modules/matlab/include/opencv2/matlab/map.hpp @@ -43,6 +43,7 @@ #ifndef OPENCV_MAP_HPP_ #define OPENCV_MAP_HPP_ +namespace matlab { #if __cplusplus >= 201103L // If we have C++11 support, we just want to use unordered_map @@ -84,5 +85,7 @@ public: } }; +} // namespace matlab + #endif #endif diff --git a/modules/matlab/include/opencv2/matlab/mxarray.hpp b/modules/matlab/include/opencv2/matlab/mxarray.hpp index f831dd49d..e9124d2f0 100644 --- a/modules/matlab/include/opencv2/matlab/mxarray.hpp +++ b/modules/matlab/include/opencv2/matlab/mxarray.hpp @@ -66,8 +66,6 @@ typedef std::set StringSet; * Matlab does not ship headers for the mkl functions, so we define them * here. * - * This operation is used extensively to copy between Matlab's column-major - * format and OpenCV's row-major format. */ #ifdef __cplusplus extern "C" { @@ -76,7 +74,7 @@ extern "C" { } #endif - +namespace matlab { /*! * @brief raise error if condition fails * @@ -101,96 +99,94 @@ static void error(const std::string& str) { // ---------------------------------------------------------------------------- // MATLAB TRAITS // ---------------------------------------------------------------------------- -namespace Matlab { - class DefaultTraits {}; - class InheritType {}; - static const int Dynamic = -1; +class DefaultTraits {}; +class InheritType {}; +static const int Dynamic = -1; - template class Traits { - public: - static const mxClassID ScalarType = mxUNKNOWN_CLASS; - static const mxComplexity Complex = mxCOMPLEX; - static const mxComplexity Real = mxCOMPLEX; - static std::string ToString() { return "Unknown/Unsupported"; } - }; - // bool - template<> class Traits { - public: - static const mxClassID ScalarType = mxLOGICAL_CLASS; - static std::string ToString() { return "boolean"; } - }; - // uint8_t - template<> class Traits { - public: - static const mxClassID ScalarType = mxUINT8_CLASS; - static std::string ToString() { return "uint8_t"; } - }; - // int8_t - template<> class Traits { - public: - static const mxClassID ScalarType = mxINT8_CLASS; - static std::string ToString() { return "int8_t"; } - }; - // uint16_t - template<> class Traits { - public: - static const mxClassID ScalarType = mxUINT16_CLASS; - static std::string ToString() { return "uint16_t"; } - }; - // int16_t - template<> class Traits { - public: - static const mxClassID ScalarType = mxINT16_CLASS; - static std::string ToString() { return "int16_t"; } - }; - // uint32_t - template<> class Traits { - public: - static const mxClassID ScalarType = mxUINT32_CLASS; - static std::string ToString() { return "uint32_t"; } - }; - // int32_t - template<> class Traits { - public: - static const mxClassID ScalarType = mxINT32_CLASS; - static std::string ToString() { return "int32_t"; } - }; - // uint64_t - template<> class Traits { - public: - static const mxClassID ScalarType = mxUINT64_CLASS; - static std::string ToString() { return "uint64_t"; } - }; - // int64_t - template<> class Traits { - public: - static const mxClassID ScalarType = mxINT64_CLASS; - static std::string ToString() { return "int64_t"; } - }; - // float - template<> class Traits { - public: - static const mxClassID ScalarType = mxSINGLE_CLASS; - static std::string ToString() { return "float"; } - }; - // double - template<> class Traits { - public: - static const mxClassID ScalarType = mxDOUBLE_CLASS; - static std::string ToString() { return "double"; } - }; - // char - template<> class Traits { - public: - static const mxClassID ScalarType = mxCHAR_CLASS; - static std::string ToString() { return "char"; } - }; - // inherited type - template<> class Traits { - public: - static std::string ToString() { return "Inherited type"; } - }; -} +template class Traits { +public: + static const mxClassID ScalarType = mxUNKNOWN_CLASS; + static const mxComplexity Complex = mxCOMPLEX; + static const mxComplexity Real = mxCOMPLEX; + static std::string ToString() { return "Unknown/Unsupported"; } +}; +// bool +template<> class Traits { +public: + static const mxClassID ScalarType = mxLOGICAL_CLASS; + static std::string ToString() { return "boolean"; } +}; +// uint8_t +template<> class Traits { +public: + static const mxClassID ScalarType = mxUINT8_CLASS; + static std::string ToString() { return "uint8_t"; } +}; +// int8_t +template<> class Traits { +public: + static const mxClassID ScalarType = mxINT8_CLASS; + static std::string ToString() { return "int8_t"; } +}; +// uint16_t +template<> class Traits { +public: + static const mxClassID ScalarType = mxUINT16_CLASS; + static std::string ToString() { return "uint16_t"; } +}; +// int16_t +template<> class Traits { +public: + static const mxClassID ScalarType = mxINT16_CLASS; + static std::string ToString() { return "int16_t"; } +}; +// uint32_t +template<> class Traits { +public: + static const mxClassID ScalarType = mxUINT32_CLASS; + static std::string ToString() { return "uint32_t"; } +}; +// int32_t +template<> class Traits { +public: + static const mxClassID ScalarType = mxINT32_CLASS; + static std::string ToString() { return "int32_t"; } +}; +// uint64_t +template<> class Traits { +public: + static const mxClassID ScalarType = mxUINT64_CLASS; + static std::string ToString() { return "uint64_t"; } +}; +// int64_t +template<> class Traits { +public: + static const mxClassID ScalarType = mxINT64_CLASS; + static std::string ToString() { return "int64_t"; } +}; +// float +template<> class Traits { +public: + static const mxClassID ScalarType = mxSINGLE_CLASS; + static std::string ToString() { return "float"; } +}; +// double +template<> class Traits { +public: + static const mxClassID ScalarType = mxDOUBLE_CLASS; + static std::string ToString() { return "double"; } +}; +// char +template<> class Traits { +public: + static const mxClassID ScalarType = mxCHAR_CLASS; + static std::string ToString() { return "char"; } +}; +// inherited type +template<> class Traits { +public: + static std::string ToString() { return "Inherited type"; } +}; @@ -244,7 +240,7 @@ public: * * Construct a valid 0x0 matrix (so all other methods do not need validity checks */ - MxArray() : ptr_(mxCreateDoubleMatrix(1, 1, Matlab::Traits<>::Real)), owns_(true) {} + MxArray() : ptr_(mxCreateDoubleMatrix(1, 1, matlab::Traits<>::Real)), owns_(true) {} /*! * @brief inheriting constructor @@ -265,7 +261,7 @@ public: * * This constructor explicitly creates an MxArray of the given size and type. */ - MxArray(size_t m, size_t n, size_t k, mxClassID id, mxComplexity com = Matlab::Traits<>::Real) : owns_(true) { + MxArray(size_t m, size_t n, size_t k, mxClassID id, mxComplexity com = matlab::Traits<>::Real) : owns_(true) { mwSize dims[] = { static_cast(m), static_cast(n), static_cast(k) }; ptr_ = mxCreateNumericArray(3, dims, id, com); } @@ -278,7 +274,7 @@ public: */ template static MxArray Tensor(size_t m, size_t n, size_t k=1) { - return MxArray(m, n, k, Matlab::Traits::ScalarType); + return MxArray(m, n, k, matlab::Traits::ScalarType); } /*! @@ -289,7 +285,7 @@ public: */ template static MxArray Matrix(size_t m, size_t n) { - return MxArray(m, n, 1, Matlab::Traits::ScalarType); + return MxArray(m, n, 1, matlab::Traits::ScalarType); } /*! @@ -300,7 +296,7 @@ public: */ template static MxArray Vector(size_t m) { - return MxArray(m, 1, 1, Matlab::Traits::ScalarType); + return MxArray(m, 1, 1, matlab::Traits::ScalarType); } /*! @@ -311,7 +307,7 @@ public: */ template static MxArray Scalar(ScalarType value = 0) { - MxArray s(1, 1, 1, Matlab::Traits::ScalarType); + MxArray s(1, 1, 1, matlab::Traits::ScalarType); s.real()[0] = value; return s; } @@ -653,4 +649,6 @@ public: } }; +} // namespace matlab + #endif