2013-03-13 19:52:13 +08:00
|
|
|
#ifndef OPENCV_BRIDGE_HPP_
|
|
|
|
#define OPENCV_BRIDGE_HPP_
|
|
|
|
|
|
|
|
#include "mex.h"
|
2013-06-23 14:26:27 +08:00
|
|
|
#include <vector>
|
2013-06-23 03:38:40 +08:00
|
|
|
#include <opencv2/core.hpp>
|
2013-06-25 03:42:28 +08:00
|
|
|
#include <ext/hash_map>
|
2013-06-23 03:38:40 +08:00
|
|
|
|
2013-06-23 14:26:27 +08:00
|
|
|
/*
|
|
|
|
* Custom typedefs
|
|
|
|
* Parsed names from the hdr_parser
|
|
|
|
*/
|
|
|
|
typedef std::vector<cv::Mat> vector_Mat;
|
|
|
|
typedef std::vector<cv::Point> vector_Point;
|
|
|
|
typedef std::vector<int> vector_int;
|
2013-06-27 15:25:48 +08:00
|
|
|
typedef std::vector<float> vector_float;
|
2013-06-28 02:45:19 +08:00
|
|
|
typedef std::vector<unsigned char> vector_uchar;
|
2013-06-23 14:26:27 +08:00
|
|
|
|
2013-06-25 03:42:28 +08:00
|
|
|
|
|
|
|
void conditionalError(bool expr, const std::string& str) {
|
|
|
|
if (!expr) mexErrMsgTxt(std::string("condition failed: ").append(str).c_str());
|
|
|
|
}
|
|
|
|
|
2013-06-23 03:38:40 +08:00
|
|
|
/*!
|
|
|
|
* @class Bridge
|
|
|
|
* @brief Type conversion class for converting OpenCV and native C++ types
|
|
|
|
*
|
|
|
|
* Bridge provides an interface for converting between OpenCV/C++ types
|
|
|
|
* to Matlab's mxArray format.
|
|
|
|
*
|
|
|
|
* Each type conversion requires three operators:
|
|
|
|
* // conversion from ObjectType --> Bridge
|
|
|
|
* Bridge& operator=(const ObjectType&);
|
|
|
|
* // implicit conversion from Bridge --> ObjectType
|
|
|
|
* operator ObjectType();
|
|
|
|
* // explicit conversion from Bridge --> ObjectType
|
2013-06-23 14:26:27 +08:00
|
|
|
* ObjectType toObjectType();
|
2013-06-23 03:38:40 +08:00
|
|
|
*
|
|
|
|
* The bridging class provides common conversions between OpenCV types,
|
|
|
|
* std and stl types to Matlab's mxArray format. By inheriting Bridge,
|
|
|
|
* you can add your own custom type conversions.
|
|
|
|
*
|
2013-06-23 14:26:27 +08:00
|
|
|
* NOTE: for the explicit conversion function, the object name must be
|
|
|
|
* in UpperCamelCase, for example:
|
|
|
|
* int --> toInt
|
|
|
|
* my_object --> MyObject
|
|
|
|
* my_Object --> MyObject
|
|
|
|
* myObject --> MyObject
|
|
|
|
* this is because the binding generator standardises the calling syntax.
|
|
|
|
*
|
2013-06-23 03:38:40 +08:00
|
|
|
* Bridge attempts to make as few assumptions as possible, however in
|
|
|
|
* some cases where 1-to-1 mappings don't exist, some assumptions are necessary.
|
|
|
|
* In particular:
|
|
|
|
* - conversion from of a 2-channel Mat to an mxArray will result in a complex
|
|
|
|
* output
|
|
|
|
* - conversion from multi-channel interleaved Mats will result in
|
|
|
|
* multichannel planar mxArrays
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class Bridge {
|
2013-06-25 03:42:28 +08:00
|
|
|
private:
|
|
|
|
mxArray* ptr_;
|
2013-06-23 03:38:40 +08:00
|
|
|
public:
|
|
|
|
// bridges are default constructible
|
|
|
|
Bridge() {}
|
|
|
|
virtual ~Bridge() {}
|
|
|
|
|
2013-06-25 03:42:28 +08:00
|
|
|
/*! @brief unpack an object from Matlab into C++
|
|
|
|
*
|
|
|
|
* this function checks whether the given bridge is derived from an
|
|
|
|
* object in Matlab. If so, it converts it to a (platform dependent)
|
|
|
|
* pointer to the underlying C++ object.
|
|
|
|
*
|
|
|
|
* NOTE! This function assumes that the C++ pointer is stored in inst_
|
|
|
|
*/
|
|
|
|
template <typename Object>
|
|
|
|
Object* getObjectByName(const char* name) {
|
|
|
|
// check that the object is actually of correct type before unpacking
|
|
|
|
// TODO: Traverse class hierarchy?
|
|
|
|
if (!mxIsClass(ptr_, name)) {
|
|
|
|
const char* actual = mxGetClassName(ptr_);
|
|
|
|
mexErrMsgTxt(std::string("Expected class ").append(std::string(name))
|
|
|
|
.append(" but was given ").append(std::string(actual)).c_str());
|
|
|
|
}
|
|
|
|
// get the instance field
|
|
|
|
mxArray* inst = mxGetField(ptr_, 0, "inst_");
|
|
|
|
Object* obj = NULL;
|
|
|
|
// make sure the pointer is the correct size for the system
|
|
|
|
if (sizeof(void *) == 8 && mxIsClass(inst, "uint64")) {
|
|
|
|
// 64-bit pointers
|
|
|
|
// TODO: Do we REALLY REALLY need to reinterpret_cast?
|
|
|
|
obj = reinterpret_cast<Object *>(
|
|
|
|
reinterpret_cast<uint64_t *>(mxGetData(inst))[0]);
|
|
|
|
} else if (sizeof(void *) == 4 && mxIsClass(inst, "uint32")) {
|
|
|
|
// 32-bit pointers
|
|
|
|
obj = reinterpret_cast<Object *>(
|
|
|
|
reinterpret_cast<uint32_t *>(mxGetData(inst))[0]);
|
|
|
|
} else {
|
|
|
|
mexErrMsgTxt("Incorrect pointer type stored for architecture");
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally check if the object is NULL
|
|
|
|
if (!obj) mexErrMsgTxt(std::string("Object ").append(std::string(name)).append(std::string(" is NULL")).c_str());
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2013-06-23 03:38:40 +08:00
|
|
|
// --------------------------- mxArray --------------------------------------
|
2013-06-23 14:26:27 +08:00
|
|
|
Bridge& operator=(const mxArray* obj) { return *this; }
|
2013-06-23 03:38:40 +08:00
|
|
|
Bridge(const mxArray* obj) {}
|
2013-06-23 14:26:27 +08:00
|
|
|
mxArray* toMxArray() { return NULL; }
|
2013-06-23 03:38:40 +08:00
|
|
|
|
|
|
|
// --------------------------- cv::Mat --------------------------------------
|
2013-06-23 14:26:27 +08:00
|
|
|
Bridge& operator=(const cv::Mat& obj) { return *this; }
|
2013-06-23 03:38:40 +08:00
|
|
|
cv::Mat toMat() { return cv::Mat(); }
|
2013-06-25 03:42:28 +08:00
|
|
|
operator cv::Mat() { return toMat(); }
|
2013-06-23 03:38:40 +08:00
|
|
|
|
2013-06-23 14:26:27 +08:00
|
|
|
// -------------------- vector_Mat --------------------------------
|
|
|
|
Bridge& operator=(const vector_Mat& obj) { return *this; }
|
|
|
|
vector_Mat toVectorMat() { return vector_Mat(); }
|
2013-06-25 03:42:28 +08:00
|
|
|
operator vector_Mat() { return toVectorMat(); }
|
2013-06-23 14:26:27 +08:00
|
|
|
|
2013-06-23 03:38:40 +08:00
|
|
|
// --------------------------- int --------------------------------------
|
2013-06-23 14:26:27 +08:00
|
|
|
Bridge& operator=(const int& obj) { return *this; }
|
2013-06-23 03:38:40 +08:00
|
|
|
int toInt() { return 0; }
|
2013-06-25 03:42:28 +08:00
|
|
|
operator int() { return toInt(); }
|
2013-06-23 14:26:27 +08:00
|
|
|
|
|
|
|
// --------------------------- vector_int ----------------------------------
|
|
|
|
Bridge& operator=(const vector_int& obj) { return *this; }
|
|
|
|
vector_int toVectorInt() { return vector_int(); }
|
2013-06-25 03:42:28 +08:00
|
|
|
operator vector_int() { return toVectorInt(); }
|
2013-06-23 14:26:27 +08:00
|
|
|
|
2013-06-27 15:25:48 +08:00
|
|
|
// --------------------------- vector_float ----------------------------------
|
|
|
|
Bridge& operator=(const vector_float& obj) { return *this; }
|
|
|
|
vector_float toVectorFloat() { return vector_float(); }
|
|
|
|
operator vector_float() { return toVectorFloat(); }
|
|
|
|
|
2013-06-23 14:26:27 +08:00
|
|
|
// --------------------------- string --------------------------------------
|
|
|
|
Bridge& operator=(const std::string& obj) { return *this; }
|
|
|
|
std::string toString() { return ""; }
|
2013-06-25 03:42:28 +08:00
|
|
|
operator std::string() { return toString(); }
|
2013-06-23 14:26:27 +08:00
|
|
|
|
|
|
|
// --------------------------- bool --------------------------------------
|
|
|
|
Bridge& operator=(const bool& obj) { return *this; }
|
|
|
|
bool toBool() { return 0; }
|
2013-06-25 03:42:28 +08:00
|
|
|
operator bool() { return toBool(); }
|
2013-06-23 14:26:27 +08:00
|
|
|
|
|
|
|
// --------------------------- double --------------------------------------
|
|
|
|
Bridge& operator=(const double& obj) { return *this; }
|
|
|
|
double toDouble() { return 0; }
|
2013-06-25 03:42:28 +08:00
|
|
|
operator double() { return toDouble(); }
|
2013-06-23 14:26:27 +08:00
|
|
|
|
|
|
|
// -------------------------- Point --------------------------------------
|
|
|
|
Bridge& operator=(const cv::Point& obj) { return *this; }
|
|
|
|
cv::Point toPoint() { return cv::Point(); }
|
2013-06-25 03:42:28 +08:00
|
|
|
operator cv::Point() { return toPoint(); }
|
2013-06-23 14:26:27 +08:00
|
|
|
|
2013-06-27 15:25:48 +08:00
|
|
|
// -------------------------- Point2f ------------------------------------
|
|
|
|
Bridge& operator=(const cv::Point2f& obj) { return *this; }
|
|
|
|
cv::Point2f toPoint2f() { return cv::Point2f(); }
|
|
|
|
operator cv::Point2f() { return toPoint2f(); }
|
|
|
|
|
|
|
|
// -------------------------- Point2d ------------------------------------
|
|
|
|
Bridge& operator=(const cv::Point2d& obj) { return *this; }
|
|
|
|
cv::Point2d toPoint2d() { return cv::Point2d(); }
|
|
|
|
operator cv::Point2d() { return toPoint2d(); }
|
|
|
|
|
2013-06-23 14:26:27 +08:00
|
|
|
// -------------------------- Size ---------------------------------------
|
|
|
|
Bridge& operator=(const cv::Size& obj) { return *this; }
|
|
|
|
cv::Size toSize() { return cv::Size(); }
|
2013-06-25 03:42:28 +08:00
|
|
|
operator cv::Size() { return toSize(); }
|
2013-06-23 14:26:27 +08:00
|
|
|
|
2013-06-27 15:25:48 +08:00
|
|
|
// -------------------------- Moments ---------------------------------------
|
|
|
|
Bridge& operator=(const cv::Moments& obj) { return *this; }
|
|
|
|
cv::Moments toMoments() { return cv::Moments(); }
|
|
|
|
operator cv::Moments() { return toMoments(); }
|
|
|
|
|
2013-06-23 14:26:27 +08:00
|
|
|
// ------------------------ vector_Point ------------------------------------
|
|
|
|
Bridge& operator=(const vector_Point& obj) { return *this; }
|
|
|
|
vector_Point toVectorPoint() { return vector_Point(); }
|
2013-06-25 03:42:28 +08:00
|
|
|
operator vector_Point() { return toVectorPoint(); }
|
2013-06-23 14:26:27 +08:00
|
|
|
|
2013-06-28 02:45:19 +08:00
|
|
|
// ------------------------ vector_uchar -------------------------------------
|
|
|
|
Bridge& operator=(const vector_uchar& obj) { return *this; }
|
|
|
|
vector_uchar toVectorUchar() { return vector_uchar(); }
|
|
|
|
operator vector_uchar() { return toVectorUchar(); }
|
|
|
|
|
2013-06-23 14:26:27 +08:00
|
|
|
// -------------------------- Scalar --------------------------------------
|
|
|
|
Bridge& operator=(const cv::Scalar& obj) { return *this; }
|
|
|
|
cv::Scalar toScalar() { return cv::Scalar(); }
|
2013-06-25 03:42:28 +08:00
|
|
|
operator cv::Scalar() { return toScalar(); }
|
2013-06-23 14:26:27 +08:00
|
|
|
|
|
|
|
// -------------------------- Rect --------------------------------------
|
|
|
|
Bridge& operator=(const cv::Rect& obj) { return *this; }
|
|
|
|
cv::Rect toRect() { return cv::Rect(); }
|
2013-06-25 03:42:28 +08:00
|
|
|
operator cv::Rect() { return toRect(); }
|
2013-06-23 14:26:27 +08:00
|
|
|
|
|
|
|
// ---------------------- RotatedRect ------------------------------------
|
|
|
|
Bridge& operator=(const cv::RotatedRect& obj) { return *this; }
|
|
|
|
cv::RotatedRect toRotatedRect() { return cv::RotatedRect(); }
|
2013-06-25 03:42:28 +08:00
|
|
|
operator cv::RotatedRect() { return toRotatedRect(); }
|
2013-06-23 14:26:27 +08:00
|
|
|
|
|
|
|
// ---------------------- TermCriteria -----------------------------------
|
|
|
|
Bridge& operator=(const cv::TermCriteria& obj) { return *this; }
|
|
|
|
cv::TermCriteria toTermCriteria() { return cv::TermCriteria(); }
|
2013-06-25 03:42:28 +08:00
|
|
|
operator cv::TermCriteria() { return toTermCriteria(); }
|
2013-06-23 14:26:27 +08:00
|
|
|
|
|
|
|
// ---------------------- RNG -----------------------------------
|
|
|
|
Bridge& operator=(const cv::RNG& obj) { return *this; }
|
2013-06-25 03:42:28 +08:00
|
|
|
/*! @brief explicit conversion to cv::RNG()
|
|
|
|
*
|
|
|
|
* Converts a bridge object to a cv::RNG(). We explicitly assert that
|
|
|
|
* the object is an RNG in matlab space before attempting to deference
|
|
|
|
* its pointer
|
|
|
|
*/
|
|
|
|
cv::RNG toRNG() {
|
|
|
|
return (*getObjectByName<cv::RNG>("RNG"));
|
|
|
|
}
|
|
|
|
operator cv::RNG() { return toRNG(); }
|
2013-06-23 14:26:27 +08:00
|
|
|
|
2013-06-23 03:38:40 +08:00
|
|
|
};
|
2013-03-13 19:52:13 +08:00
|
|
|
|
|
|
|
#endif
|