OpenCV  4.10.0-dev
Open Source Computer Vision
Loading...
Searching...
No Matches
cv::DataType< _Tp > Class Template Reference

Template "trait" class for OpenCV primitive data types. More...

#include <opencv2/core/traits.hpp>

Collaboration diagram for cv::DataType< _Tp >:

Detailed Description

template<typename _Tp>
class cv::DataType< _Tp >

Template "trait" class for OpenCV primitive data types.

Note
Deprecated. This is replaced by "single purpose" traits: traits::Type and traits::Depth

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) };
};
...
@ value
Definition traits.hpp:289
@ fmt
Definition traits.hpp:290
Template "trait" class for OpenCV primitive data types.
Definition traits.hpp:113
#define CV_8U
Definition interface.h:73
unsigned char uchar
Definition interface.h:51
#define CV_MAKETYPE(depth, cn)
Definition interface.h:85

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
// the statement below will print 6, 2 , that is depth == CV_64F, channels == 2
cout << B.depth() << ", " << B.channels() << endl;
Template matrix class derived from Mat.
Definition mat.hpp:2246
n-dimensional dense array class
Definition mat.hpp:828
int depth() const
Returns the depth of a matrix element.
int channels() const
Returns the number of matrix channels.

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.

Note
Default values were dropped to stop confusing developers about using of unsupported types (see #7599)

The documentation for this class was generated from the following file: