OpenCV Mat元素类型及其大小 [英] OpenCV Mat element types and their sizes

查看:1646
本文介绍了OpenCV Mat元素类型及其大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对OpenCV Mat元素类型感到困惑.这是来自文档:

I'm confused by the OpenCV Mat element types. This is from the docs:

There is a limited fixed set of primitive data types the library can operate on.
That is, array elements should have one of the following types:

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

For these basic types, the following enumeration is applied:
enum { CV_8U=0, CV_8S=1, CV_16U=2, CV_16S=3, CV_32S=4, CV_32F=5, CV_64F=6 };

众所周知,C ++标准没有定义基本类型的大小(以字节为单位),那么它们如何使用这种假设呢?我应该从CV_32S期望什么类型,它是int32_t还是int?

It's known that C++ standard doesn't define the size of basic types in bytes, so how do they use such assumptions? And what type should I expect from, let's say, CV_32S, is it int32_t or int?

推荐答案

Miki的答案开发,
在OpenCV 3中,定义已移至modules/core/include/opencv2/core/ traits.hpp ,您可以在其中找到:

Developing from Miki's answer,
In OpenCV 3 definition has moved to modules/core/include/opencv2/core/traits.hpp, where you can find:

/** @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
    };
};



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

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;
};

在大多数情况下/编译器中,使用C ++精确数据类型应该可以.使用CV_8U-> uint8_tCV_8U-> int8_t)不会有问题. >在C ++中明确定义.对于浮动(32位)和双精度(64位)也是如此.但是,确实可以完全确保使用其他数据类型(例如,使用at<>方法时)使用其他数据类型,例如:

In most of the cases/compilers you should be fine using C++ exact data types. You wouldn't have problems with single byte data types (CV_8U -> uint8_t and CV_8U -> int8_t) as unambiguously defined in C++. The same for float (32bit) and double (64bit). However, it is true that for other data types to be completely sure you use the correct data type (for example when using the at<> method) you should use for example:

typedef TypeDepth<CV_WHATEVER_YOU_USED_TO_CREATE_YOUR_MAT>::value_type access_type;
myMat.at<access_type>(y,x) = 0;

作为一个旁注,令我惊讶的是,他们决定采用这种模棱两可的方法,而不是简单地使用确切的数据类型.

As a side note, I am surprised they decided to take such an ambiguous approach, instead of simply using exact data types.

因此,关于您的最后一个问题:

Therefore, regarding your last question:

我应该从CV_32S说什么类型?

我相信OpenCV 3中最准确的答案是:

I believe the most precise answer, in OpenCV 3, is:

TypeDepth<CV_32S>::value_type

这篇关于OpenCV Mat元素类型及其大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆