3

I have a template method in which a Mat object is constructed. The type of this matrix depends on the template implementation:

template <typename T>
void createMatrixAndDoStuff(int rows, int cols){
// ...
Mat A(rows,cols,getCVtype<T>::value);
// ...
}

In this case I use a basic trait, getCVType<T>::value will return CV_32F if T=float, etc. I know this is silly, because I could have used Mat_<T>(rows,cols) and forget about using traits for this. But it made me think: is there any available trait (or any template stuff) in OpenCV to infer type macros (CV_32F,CV_8U,...) in compile time from types?

3
  • AFAIK there are not, but you could easily implement them your own. Commented Aug 27, 2014 at 9:17
  • @GermánDiago that's what I thought in the first place, I already implemented this getCVtype trait thingy :) But still, it is strange that no methods are provided in the OpenCV API, isn't it? Commented Aug 27, 2014 at 9:22
  • 1
    Well, the API was coming from C mostly, so I wouldn't be surprised if they don't provide that. Commented Aug 27, 2014 at 10:11

1 Answer 1

6

opencv do have the type traits for this task, it called cv::DataType, you do not need to create one by yourself

static_assert(cv::DataType<float>::type == CV_32F, 
              "cv::DataType<float>::type == CV_32F");
static_assert(cv::DataType<uchar>::type == CV_8U, 
              "cv::DataType<uchar>::type == CV_8U");
static_assert(cv::DataType<cv::Vec3b>::type == CV_8UC3, 
              "cv::DataType<cv::Vec3b>::type == CV_8UC3");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.