type traits for OpenCV data types

Яoi*_*ois 2 c++ templates opencv type-traits

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);
// ...
}
Run Code Online (Sandbox Code Playgroud)

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?

Ste*_*ing 5

opencv 确实有这个任务的类型特征,它叫做 cv::DataType,你不需要自己创建一个

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");
Run Code Online (Sandbox Code Playgroud)