简体   繁体   English

如何检查给定参数是否为 cv::noArray()?

[英]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.在 OpenCV 库中cv::noArray()用于此目的。 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() .如何以编程方式检查可选参数是否已给出或默认为cv::noArray()

Since I didn't find any documentation, it might be helpful for others as well.由于我没有找到任何文档,它可能对其他人也有帮助。

You can do the following to check if an cv::InputArray was assigned to cv::noArray() :您可以执行以下操作来检查cv::InputArray已分配给cv::noArray()

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

    // ... more code using candidatesMap
}

I've checked the usage of the cv::noArray() in the OpenCV code and there is something like this:我已经检查了 OpenCV 代码中cv::noArray()的用法,并且有这样的东西:

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.但即使candidatesMap不等于noArray这并不意味着此输入不为空。 So it's also worth checking its size.所以也值得检查它的大小。

@MichałWalenciak Said that this comparison wouldn't work. @MichałWalenciak 说这种比较是行不通的。 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.要检查candidatesMap是否有效,您可以调用candidatesMap.getObj()并检查该指针是否有效。

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.一种有参数,一种没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM