gpt4 book ai didi

OpenCV Mat 元素类型及其大小

转载 作者:太空宇宙 更新时间:2023-11-03 20:36:47 26 4
gpt4 key购买 nike

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

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?

最佳答案

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

/** @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 )不会有问题 unambiguously defined in C++ . float (32bit) and double (64bit) 相同.但是,对于其他数据类型,确实可以完全确保您使用了正确的数据类型(例如,当使用 at<> 方法时),您应该使用例如:

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

附带说明一下,我很惊讶他们决定采用这种模棱两可的方法,而不是简单地使用确切的数据类型。

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

What type should I expect from, let's say, CV_32S?

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

TypeDepth<CV_32S>::value_type

关于OpenCV Mat 元素类型及其大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15245262/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com