template class cv::DataType
Overview
Template “trait” class for OpenCV primitive data types. More…
#include <traits.hpp> template <typename _Tp> class DataType { public: // typedefs typedef value_type channel_type; typedef _Tp value_type; typedef value_type vec_type; typedef value_type work_type; // enums enum { generic_type = 1, depth = -1, channels = 1, fmt = 0, type = CV_MAKETYPE(depth, channels), }; };
Detailed Documentation
Template “trait” class for OpenCV primitive data types.
A primitive OpenCV data type is one of unsigned char, bool, signed char, unsigned short, signed short, int, float, double, or a tuple of values of one of these types, where all the values in the tuple have the same type. Any primitive type from the list can be defined by an identifier in the form CV_<bit-depth>{U|S|F}C(<number_of_channels>), for example: uchar CV_8UC1, 3-element floating-point tuple CV_32FC3, and so on. A universal OpenCV structure that is able to store a single instance of such a primitive data type is Vec. Multiple instances of such a type can be stored in a std::vector, Mat, Mat_, SparseMat, SparseMat_, or any other container that is able to store Vec instances.
The DataType class is basically used to provide a description of such primitive data types without adding any fields or methods to the corresponding classes (and it is actually impossible to add anything to primitive C/C++ data types). This technique is known in C++ as class traits. It is not DataType itself that is used but its specialized versions, such as:
template<> class DataType<uchar> { typedef uchar value_type; typedef int work_type; typedef uchar channel_type; enum { channel_type = CV_8U, channels = 1, fmt='u', type = CV_8U }; }; ... template<typename _Tp> DataType<std::complex<_Tp> > { typedef std::complex<_Tp> value_type; typedef std::complex<_Tp> work_type; typedef _Tp channel_type; // DataDepth is another helper trait class enum { depth = DataDepth<_Tp>::value, channels=2, fmt=(channels-1)*256+DataDepth<_Tp>::fmt, type=CV_MAKETYPE(depth, channels) }; }; ...
The main purpose of this class is to convert compilation-time type information to an OpenCV-compatible data type identifier, for example:
// allocates a 30x40 floating-point matrix Mat A(30, 40, DataType<float>::type); Mat B = Mat_<std::complex<double> >(3, 3); // the statement below will print 6, 2 , that is depth == CV_64F, channels == 2 cout << B.depth() << ", " << B.channels() << endl;
So, such traits are used to tell OpenCV which data type you are working with, even if such a type is not native to OpenCV. For example, the matrix B initialization above is compiled because OpenCV defines the proper specialized template class DataType <complex<_Tp> > . This mechanism is also useful (and used in OpenCV this way) for generic algorithms implementations.