Ask Your Question

Is it possible to convert the CvMat's type to the underlying C++ type?

asked 2013-03-19 03:16:12 -0500

uvts_cvs gravatar image

Hello, if I have a cv::Mat m I can get its type via CV_MAT_TYPE(m.type()), is it possible to get the underlying C++ type (like float)?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-03-19 04:55:35 -0500

Vladislav Vinogradov gravatar image

No. In general case Mat's type is unknown in compilation time.

You can use template class Mat_:

template <typename T> void func(Mat_<T> img);

cv::Mat_<float> mat;
mat.create(rows, cols);

or use switch/if construction:

switch (mat.type())
case CV_8UC1:
        uchar* ptr = mat.ptr<uchar>();

case CV_8UC3:
        Vec3b* ptr = mat.ptr<Vec3b>();

or use table of pointer to functions

typedef void (*func_ptr_t)(Mat img);
func_ptr_t funcs[7][4] = 
    {&func_8u_c1, &func_8u_c2, ...},
    {&func_32f_c1, &func_32f_c2, ...}
funcs[img.depth()][img.channels() - 1](img);
edit flag offensive delete link more

Question Tools


Asked: 2013-03-19 03:16:12 -0500

Seen: 355 times

Last updated: Mar 19 '13