Re: [eigen] Eigen::Matrix and cv::Mat conversions

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Hi,

I had a similar problem dealing with Eigen and OpenCV and I guess (at least for fixed-size matrices) the best way is to let Eigen do the memory handling and just make a wrapper such that OpenCV can access the matrix. Also I found out, that a very lot of OpenCV C++ methods are actually just wrappers around C methods, so I stayed with CvMat (cv::Mat does not give you any actual compile-time type checking or better performance ...)

See below what I came up with, and which 'works for me',

hth Christoph


#include <Eigen/Core>
#include <cv.h>

template<class f_type>
struct cv_f_type;

template<>
struct cv_f_type<double>
{
	enum {value = CV_64F};
};

template<>
struct cv_f_type<float>
{
	enum {value = CV_32F};
};

// add further specializations for other types if required ...

/**
 * cv_mat wraps a CvMat around an Eigen Matrix
 */
template<class f_type, int rows, int cols>
class cv_mat : public Eigen::Matrix<f_type, rows, cols, Eigen::RowMajor>
{
	typedef Eigen::Matrix<f_type, rows, cols, Eigen::RowMajor> base_type;
	enum {type_ = cv_f_type<f_type>::value};
	CvMat cv_mat_;

public:
	cv_mat()
	{
		cv_mat_ = cvMat(rows, cols, type_, base_type::data());
	}

	cv_mat(const cv_mat& oth) : base_type(oth)
	{
		cv_mat_ = cvMat(rows, cols, type_, base_type::data());
	}

	template<class Derived>
	cv_mat(const Eigen::MatrixBase<Derived> &value) : base_type(value)
	{
		cv_mat_ = cvMat(rows, cols, type_, base_type::data());
	}

	template<class Derived>
	cv_mat& operator=(const Eigen::MatrixBase<Derived> &value)
	{
		base_type::operator=(value);
		return *this;
	}

	cv_mat& operator=(const cv_mat& value)
	{
		base_type::operator=(value);
		return *this;
	}
	
	CvMat* operator&()
	{
		return &cv_mat_;
	}
	const CvMat* operator&() const
	{
		return &cv_mat_;
	}
};



--
----------------------------------------------
Dipl.-Inf. Christoph Hertzberg
Cartesium 0.051
Universität Bremen
Enrique-Schmidt-Straße 5
28359 Bremen

Tel: (+49) 421-218-64252
----------------------------------------------



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/