简体   繁体   English

如何在C ++ OpenCV中使用OutputArrayOfArrays

[英]How to use OutputArrayOfArrays in C++ OpenCV

I've been trying to create a function, using OpenCV proxy's functions to accept multiple types of elements (inputArray, OutputArray, OutputArrayOfArrays). 我一直在尝试创建一个函数,使用OpenCV代理的函数来接受多种类型的元素(inputArray,OutputArray,OutputArrayOfArrays)。

So far, I've found info on how to use InputArray and OutputArray, but not on OutputArrayOfArrays. 到目前为止,我已经找到了有关如何使用InputArray和OutputArray的信息,但没有找到OutputArrayOfArrays的信息。

I've made this code in order to test the result assignment to OutputArrayOfArrays, with no avail: 我编写此代码是为了测试对OutputArrayOfArrays的结果分配,但无济于事:

//Return 3 Mats, each one filled with a different primary color
void rgb_mats(cv::OutputArrayOfArrays results){
    cv::vector<cv::Mat> bgr;
    cv::Mat B(100,100, CV_8UC3, cv::Scalar(255,0,0));
    cv::Mat G(100,100, CV_8UC3, cv::Scalar(0,255,0));
    cv::Mat R(100,100, CV_8UC3, cv::Scalar(0,0,255));
    bgr.push_back(B);
    bgr.push_back(G);
    bgr.push_back(R);
    cv::merge(bgr, results);
}

And this fails with an OpenCV assertion: 而此操作因OpenCV断言而失败:

OpenCV Error: Assertion failed (dims == 2 && (sizes[0] == 1 || sizes[1] == 1 || sizes[0]*sizes[1] == 0)) in cv::_OutputArray::create, file C:\opencv_code\sources\modules\core\src\matrix.cpp, line 1564

I expected to get 3 cv::Mats, each with one different color, to later use the function like this: 我预计会得到3个cv :: Mat,每种颜色都有一种不同的颜色,以后再使用这样的功能:

cv::vector<cv::Mat> rgb_results;
rgb_mats(rgb_results);
assert(rgb_results.size() == 3);

I have searched the docs and I didn't find any example on how to return data with an OutputArrayofArrays. 我搜索了文档,但没有找到任何有关如何使用OutputArrayofArrays返回数据的示例。

What I can change in my example in order to pass the assert? 为了通过断言,我可以在示例中进行哪些更改?

Your issue is not in OutputArrayOfArrays but with cv::merge you're trying to merge cv::vector<cv::Mat> to another object of the same type cv::vector<cv::Mat> which is not what cv::merge is suppose to do. 您的问题不在OutputArrayOfArrays而是与cv::merge您正在尝试将cv::vector<cv::Mat>合并到相同类型cv::vector<cv::Mat>另一个对象中,这不是cv::merge应该做。 Also you're declaring B , G and R as 3-channels each CV_8UC3 , so you're variable bgr or results will be of size = 9 (3*3). 同样,您将BGR声明为每个CV_8UC3为3通道,因此您是可变的bgr否则results将为size = 9(3 * 3)。

The result code would look like something like that: 结果代码看起来像这样:

#include <opencv2/highgui.hpp>
#include <vector>

template <typename T, typename ... Ts>
void insert_all(std::vector<T> &vec, Ts ... ts)
 {   
     (vec.push_back(ts), ...);
 }


void rgb_mats(cv::Mat& results)
{
    std::vector<cv::Mat> bgr;
    bgr.reserve(3);

    cv::Mat B(100,100, CV_8UC1, cv::Scalar(255,0,0));
    cv::Mat G(100,100, CV_8UC1, cv::Scalar(0,255,0));
    cv::Mat R(100,100, CV_8UC1, cv::Scalar(0,0,255));

    insert_all(bgr, B,G, R);

    cv::merge(bgr, results);
}

int main ()
{
cv::Mat rgb_results;
rgb_mats(rgb_results);
}

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

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