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

1.3k views Asked by At

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.

3

There are 3 answers

0
Catree On BEST ANSWER

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
}
2
Michał Walenciak On

I know it doesn't answer your question, but you can solve your problem by writting cleaner code. Just write two functions. One with parameter and one without.

4
Raffallo On

I've checked the usage of the cv::noArray() in the OpenCV code and there is something like this:

if (&argument == &noArray())
    return "InputArrayOfArrays: noArray()";

in your case it would be probably:

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

But even if candidatesMap is not equal to noArray it doesn't mean that this input is not empty. So it's also worth checking its size.

@MichałWalenciak Said that this comparison wouldn't work. So you can check it in other ways. To check if candidatesMap is valid, you can call candidatesMap.getObj() and check if this pointer is valid or not.