nburk
nburk

Reputation: 22731

What's the interpretation of `type` for a std::vector instead of a cv::Mat in OpenCV and how can I change it? (C++)

I want to use the adaptiveThreshold function from OpenCV which is defined in the documentation as follows:

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

Instead of using a Mat as an input, I want to use a vector<double>. This should be possible, as I read the following in the 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).

I am using the following code:

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

However, when running the code I get the following error:

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

Now, I understand the call fails because the type of the input is not actually CV_8UC1. But I don't know how to solve this issue. I thought the type property is only relevant to Mat objects, I don't know how to interpret it for vector.

I also am not sure where to read about this issue in the docs. I found the following statement in the docs for Mat, but this doesn't help me a lot to solve my issue:

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.

Update: The above says that type actually is an Array type, but what does this mean? How can I make it so that my vector<double> gets the type CV_8UC1 that is required to use adaptiveThreshold?

Another update: After reading in the Learning OpenCV book from O'Reilly, I learned the following:

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.

So, it's obvious that in my case the vector<double> is not of type CV_8UC1 because double is clearly not an _unsigned 8-bit integer. However, I can just normalize these values which I just did. The result is a vector<int> that only has values between 0 and 255. Thus, it should be of type CV_8UC1, right? However, I am still getting the same error...

Upvotes: 2

Views: 632

Answers (1)

alitemel
alitemel

Reputation: 107

Types do not match. int and unsigned char are different. Try to cast vector<int> to vector<unsigned char> and then it should work.

Upvotes: 1

Related Questions