Depth in some opencv methods

Question:

What is the depth parameter that appears in many opencv functions? For example from filter2D() , what I understand from what ta in the documentation would be the number of bits to represent the intensity, is that correct?

Answer:

depth

It is the Data Type of each element of the image, that is, the accuracy of each pixel. The more bits per pixel, the better the representation of color differences and intensities.

On the other hand, this can add more complexity when creating a Thresholding segmentation, as the color/intensity variation is also greater.

So the choice of depth depends on the desired application.

Data Types

Official documentation explains the reason to use these types of Data Types.

In version 4.0.0-beta:

  • 8-bit unsigned integer (uchar)
  • 8-bit signed integer (schar)
  • 16-bit unsigned integer (ushort)
  • 16-bit signed integer (short)
  • 16-bit floating-point number (float)
  • 32-bit signed integer (int)
  • 32-bit floating-point number (float)
  • 64-bit floating-point number (double)

On Github you can find the Data Types of depth in the following code:

/** @brief A helper class for cv::DataType
The class is specialized for each fundamental numerical data type supported by OpenCV. It provides
DataDepth<T>::value constant.
*/
template<typename _Tp> class DataDepth
{
public:
    enum
    {
        value = DataType<_Tp>::depth,
        fmt   = DataType<_Tp>::fmt
    };
};


#ifdef OPENCV_TRAITS_ENABLE_DEPRECATED

template<int _depth> class TypeDepth
{
#ifdef OPENCV_TRAITS_ENABLE_LEGACY_DEFAULTS
    enum { depth = CV_USRTYPE1 };
    typedef void value_type;
#endif
};

template<> class TypeDepth<CV_8U>
{
    enum { depth = CV_8U };
    typedef uchar value_type;
};

template<> class TypeDepth<CV_8S>
{
    enum { depth = CV_8S };
    typedef schar value_type;
};

template<> class TypeDepth<CV_16U>
{
    enum { depth = CV_16U };
    typedef ushort value_type;
};

template<> class TypeDepth<CV_16S>
{
    enum { depth = CV_16S };
    typedef short value_type;
};

template<> class TypeDepth<CV_32S>
{
    enum { depth = CV_32S };
    typedef int value_type;
};

template<> class TypeDepth<CV_32F>
{
    enum { depth = CV_32F };
    typedef float value_type;
};

template<> class TypeDepth<CV_64F>
{
    enum { depth = CV_64F };
    typedef double value_type;
};

template<> class TypeDepth<CV_16F>
{
    enum { depth = CV_16F };
    typedef float16_t value_type;
};

#endif

//! @}

Depending on the OpenCV version there are 7 Data Types, plus the legacy Data Type CV_USRTYPE1

Or there are 8 Data Types from version 4.x, with the addition of the new CV_16F

Python

In OpenCV 3.4.3 and Python 3.x some attributes can be obtained with [(i,type(getattr(cv2,i))) for i in dir(cv2)]

And the list of data types of depth :

import cv2
import re


depths = [i for i in dir(cv2) if re.search('^CV_(?:\d+[A-Z]{1}|USRTYPE1)$', i, re.IGNORECASE)]
print(depths)

Which returns: ['CV_16S', 'CV_16U', 'CV_32F', 'CV_32S', 'CV_64F', 'CV_8S', 'CV_8U']

Note that depth and type are different . Because type is the combination of depth + number of channels.

ddepth

ddepth is the depth value of the destination image, so this must be some of the depth or -1, which means the same depth as the source image.

Scroll to Top