c++opencvvectormatadaptive-threshold

OpenCV / C++ - How to use adaptiveThreshold on vector instead of Mat?


I have a question about using the adaptiveThreshold function from OpenCV in C++. (Here is it's documentation.)

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

After reading this article, I am pretty clear on how this function can be used to find a good threshold for images (represented as Mat object in OpenVC).

My case is a little bit different in that I would like to use it only for a vector instead of a Mat. So basically I have a vector of double values and I need to find a good threshold and was wondering if there is an easy way to adapt the adaptiveThreshold function for that matter. I experimented with more static methods to generate a threshold like using the mean average or the median, but these don't work well in my case.

Does anyone have a suggestion on how to approach this? I am guessing I would have to adjust the src and dst parameters and somehow pass my vector, but a straightforward approach to do so did not work.


Solution

  • Create a cv::Mat that wraps the vector. The relevant constructor is declared:

    //! builds matrix from std::vector with or without copying the data
    template<typename _Tp> explicit Mat(const vector<_Tp>& vec, bool copyData=false);
    

    So you should be able to do:

    std::vector<double> vec(10, 0.0); // your vector here
    bool copyData = false;
    cv::Mat M(vec, copyData); // default is not to copy data
    

    The resulting Mat will be a single column.