如何在 C++ 向量中解压多个值

Ash*_*dke 0 c++ opencv vector std stdvector

我有这个函数可以从这里为我返回 RGB 格式的颜色

std::vector<cv::Vec3b> colors = find_dominant_colors(matImage, count);
Run Code Online (Sandbox Code Playgroud)

但现在我还希望函数find_dominant_colors生成的图像返回,以便我可以使用它。它生成我使用的三个图像,cv::imwrite但我希望将这三个图像返回给函数调用,以便在返回时可以直接进一步查看它,而不是为它获取目录。

如何在该行代码中解压缩多个值,例如获取图像和颜色,而不仅仅是颜色。我必须使用多个向量吗?我怎么做 ?这里使用的向量是一个 opencv 向量,用于从图像中获取 RGB 值。

编辑 :

    std::vector<cv::Vec3b> find_dominant_colors(cv::Mat img, int count) {
    const int width = img.cols;
    const int height = img.rows;
    std::vector<cv::Vec3b> colors = get_dominant_colors(root);

    cv::Mat quantized = get_quantized_image(classes, root);
    cv::Mat viewable = get_viewable_image(classes);
    cv::Mat dom = get_dominant_palette(colors);

    cv::imwrite("./classification.png", viewable);
    cv::imwrite("./quantized.png", quantized);
    cv::imwrite("./palette.png", dom);

    return colors;
}


Run Code Online (Sandbox Code Playgroud)

上面的函数将颜色返回到这里

std::vector<cv::Vec3b> colors = find_dominant_colors(matImage, count);
Run Code Online (Sandbox Code Playgroud)

我也希望它返回viewable quantized dom,我该怎么做?

Gna*_*wme 5

使用一个std::tuple.

定义您的find_dominant_colors函数以返回 a std::tuple<cv::Mat, cv::Mat, cv::Mat, cv::Vec3b>,并在return语句中执行此操作:

return std::make_tuple(quantized, viewable, dom, colors);
Run Code Online (Sandbox Code Playgroud)

在 C++17 中,您可以使用结构化绑定以方便的方式处理返回值:

auto [quantized, viewable, dom, colors] = find_dominant_colors(matImage, count);
Run Code Online (Sandbox Code Playgroud)

如果您没有 C++17,请使用std::tie处理返回的std::tuple:

cv::Mat quantized;
cv::Mat viewable;
cv::Mat dom;
cv::Vec3b colors;

std::tie(quantized, viewable, dom, colors) = find_dominant_colors(matImage, count);
Run Code Online (Sandbox Code Playgroud)

或者您可以让类型推导为您工作,并用于std::get访问返回的成员std::tuple

auto values = find_dominant_colors(matImage, count);
auto quantized = std::get<0>(values);
auto viewable = std::get<1>(values);
auto dom = std::get<2>(values);
auto colors = std::get<3>(values);
Run Code Online (Sandbox Code Playgroud)