c++opencvopencv-mat

How to check if given parameter is cv::noArray()?


I want to implement a function that takes an image as optional parameter. In case an image is passed, I want to use it - otherwise I want to calculate a default value. In the OpenCV library cv::noArray() is used for this purpose. Something like this:

void doStuff(cv::InputArray candidatesMap = cv::noArray())
{
    // initialize candidatesMap if not given
    if(candidatesMap is cv::noArray())  // <-- Need help here
    {
        candidatesMap = createCandidatesMap();
    }

    // ... more code using candidatesMap
}

How can I programmatically check if the optional parameter is given or defaults to cv::noArray().

Since I didn't find any documentation, it might be helpful for others as well.


Solution

  • You can do the following to check if an cv::InputArray was assigned to cv::noArray():

    void doStuff(cv::InputOutputArray candidatesMap = cv::noArray())
    {
        // initialize candidatesMap if not given
        if(candidatesMap.empty())
        {
            candidatesMap = createCandidatesMap();
        }
    
        // ... more code using candidatesMap
    }