我一直在使用 c 风格的 api 来生成 opencv 类型代码。例如:
cv::Mat(h, w, CV_8UC2);
CV_8UC2 是在 types_c.h 中定义的宏(已弃用?):
#define CV_MAKETYPE(depth,cn) (CV_MAT_DEPTH(depth) + (((cn)-1) << CV_CN_SHIFT))
c++ api中有没有类似类型代码生成函数,类似
Mat m(w,h, cv::Type(Vec<unsigned char, 2>).typecode()) ?
最佳答案
正如我在评论中所说,CV_MAKETYPE
并未被弃用,据我所知,这是生成这些“类型代码”的标准方式。
但是(只是为了好玩),可以通过使用 TMP 来实现另一种更像 C++ 的生成任意代码的方法(仍在编译时)。 ...
template <int depth,
int cn>
struct make_type
{
enum {
// (yes, it is exactly the same expression used by CV_MAKETYPE)
value = ((depth) & CV_MAT_DEPTH_MASK) + (((cn)-1) << CV_CN_SHIFT)
};
};
// You can check that it works exactly the same as good, old `CV_MAKETYPE`
cout << make_type<CV_8U,2>::value << " "<< CV_MAKETYPE(CV_8U,2) << endl;
...但是不要这样做。虽然 tmp 既有趣又神奇,但 CV_MAKETYPE
是在这种情况下做事的正确方法。
编辑:OpenCV 有自己的类型特征实用程序。在 core/traits.hpp
中我们可以找到 DataType
类:
The DataType class is basically used to provide a description of ... 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 ... The main purpose of this class is to convert compilation-time type information to an OpenCV-compatible data type identifier ...
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.
关于c++ - 如何生成 cv::Mat 类型的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30939637/