c++ - `type` 对于 std::vector 而不是 OpenCV 中的 cv::Mat 的解释是什么?我该如何更改它? (C++)

标签 c++ opencv vector mat

我想使用 adaptiveThreshold来自 OpenCV 的函数,在 documentation 中定义如下:

void adaptiveThreshold(InputArray src, OutputArray dst, double maxValue, int adaptiveMethod, int thresholdType, int blockSize, double C)

而不是使用 Mat作为输入,我想使用 vector<double> .这应该是可能的,因为我在 documentation 中阅读了以下内容:

When you see in the reference manual or in OpenCV source code a function that takes InputArray, it means that you can actually pass Mat, Matx, vector<T> etc. (see above the complete list).

我正在使用以下代码:

vector<double> diffs; // <initialized with a number of double values>
double maxValue = 255.0; // values in diffs above the threshold will be set to 255.0
vector<double> out; // stores the output values (either 0 or 255.0)
adaptiveThreshold(diffs, out, maxValue, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 3, 0);

但是,在运行代码时出现以下错误:

OpenCV Error: Assertion failed (src.type() == CV_8UC1) in adaptiveThreshold, file /Users/nburk/Developer/SDKs/opencv-2.4.10/modules/imgproc/src/thresh.cpp, line 796

libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Users/nburk/Developer/SDKs/opencv-2.4.10/modules/imgproc/src/thresh.cpp:796: error: (-215) src.type() == CV_8UC1 in function adaptiveThreshold

现在,我知道调用失败是因为 type输入的实际上不是 CV_8UC1 .但我不知道如何解决这个问题。我以为 type属性仅与 Mat 有关对象,我不知道如何解释 vector .

我也不确定在文档中的何处阅读有关此问题的信息。我在 docs 中找到了以下语句对于 Mat ,但这对解决我的问题没有多大帮助:

type – Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices.

更新:上面说type实际上是一个数组类型,但是这是什么意思呢?我怎样才能使我的 vector<double>获取类型 CV_8UC1这是使用 adaptiveThreshold 所必需的?

另一个更新: 在阅读了 O'ReillyLearning OpenCV 一书后,我了解到以下内容:

type can be any of a long list of predefined types of the form: CV_<bit_depth>(S|U|F) C<number_of_channels>. Thus, the matrix could consist of 32-bit floats (CV_32FC1), of un-signed integer 8-bit triplets (CV_8UC3), or of countless other elements.

所以,很明显,在我的例子中,vector<double>不是 CV_8UC1 类型因为double显然不是 _unsigned 8-bit integer .但是,我可以将我刚刚做的这些值标准化。结果是 vector<int>只有 0 之间的值和 255 .因此,它应该是 CV_8UC1 类型, 正确的?但是,我仍然遇到同样的错误...

最佳答案

类型不匹配。 intunsigned char是不同的。试投vector<int>vector<unsigned char>然后它应该可以工作。

关于c++ - `type` 对于 std::vector 而不是 OpenCV 中的 cv::Mat 的解释是什么?我该如何更改它? (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29693261/

相关文章:

C++继承函数覆盖

c++ - 在 C++ 中反射(reflect)一个类的继承树?

c++ - 使用 OpenCV 在图像中进行对象注释

c++ - 使用 opencv 时找不到 lopencv_core

android - 如何在使用 Gradle 时使用 opencv?

c++ - vector 作为 pthread_create 的输入

c++ - initializer_list、构造函数和支撑初始化

c++ - Visual Studio 2013 "A task was cancelled"

c++ - 如何从 C++ 中的 vector 中删除节点

c++ - 为了支持 move 语义,函数参数应该由 unique_ptr、value 还是 rvalue 获取?