| OpenCV 5.0.0alpha Open Source Computer Vision

Loading...
Searching...
No Matches
cv::DataType< _Tp > Class Template Reference
Core functionality » Basic structures

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) };
};
...
_Tp
cv::DataDepth::value
@ value
Definition traits.hpp:349
cv::DataDepth::fmt
@ fmt
Definition traits.hpp:350
cv::DataType
Template "trait" class for OpenCV primitive data types.
Definition traits.hpp:113
CV_8U
#define CV_8U
Definition interface.h:76
uchar
unsigned char uchar
Definition interface.h:51
CV_MAKETYPE
#define CV_MAKETYPE(depth, cn)
Definition interface.h:96

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;
cv::Mat_
Template matrix class derived from Mat.
Definition mat.hpp:2518
cv::Mat
n-dimensional dense array class
Definition mat.hpp:951
cv::Mat::depth
int depth() const
Returns the depth of a matrix element.
cv::Mat::channels
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:

- opencv2/core/traits.hpp