我想使用 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 passMat
,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
) inadaptiveThreshold
, file /Users/nburk/Developer/SDKs/opencv-2.4.10/modules/imgproc/src/thresh.cpp, line 796
libc++abi.dylib
: terminating with uncaught exception of typecv::Exception
: /Users/nburk/Developer/SDKs/opencv-2.4.10/modules/imgproc/src/thresh.cpp:796: error: (-215)src.type() == CV_8UC1
in functionadaptiveThreshold
现在,我知道调用失败是因为 type
输入的实际上不是 CV_8UC1
.但我不知道如何解决这个问题。我以为 type
属性仅与 Mat
有关对象,我不知道如何解释 vector
.
我也不确定在文档中的何处阅读有关此问题的信息。我在 docs 中找到了以下语句对于 Mat
,但这对解决我的问题没有多大帮助:
type – Array type. Use
CV_8UC1
, ...,CV_64FC4
to create 1-4 channel matrices, orCV_8UC(n)
, ...,CV_64FC(n)
to create multi-channel (up toCV_CN_MAX
channels) matrices.
更新:上面说type
实际上是一个数组类型,但是这是什么意思呢?我怎样才能使我的 vector<double>
获取类型 CV_8UC1
这是使用 adaptiveThreshold
所必需的?
另一个更新: 在阅读了 O'Reilly 的Learning 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
类型, 正确的?但是,我仍然遇到同样的错误...
最佳答案
类型不匹配。 int
和 unsigned char
是不同的。试投vector<int>
至 vector<unsigned char>
然后它应该可以工作。
关于c++ - `type` 对于 std::vector 而不是 OpenCV 中的 cv::Mat 的解释是什么?我该如何更改它? (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29693261/