【问题标题】:How do I get multiple values unpacked in C++ vectors如何在 C++ 向量中解压缩多个值
【发布时间】:2020-05-01 10:24:42
【问题描述】:

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

std::vector<cv::Vec3b> colors = find_dominant_colors(matImage, count);

但现在我还希望函数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;
}


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

std::vector<cv::Vec3b> colors = find_dominant_colors(matImage, count);

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

【问题讨论】:

  • 完全不清楚你要的是什么。
  • 你只能从一个函数中返回一个值,但你可以写一个struct,成员数量不限
  • 请补充更多细节,目前问题还不清楚。你想从函数中得到什么?
  • @Qix 代码在处理完图像后返回颜色值。现在我还希望它与颜色值一起返回图像,但是由于返回的颜色被解压缩到 opencv 向量中,我如何定义另一个向量,以便在同一个函数调用中获取图像。
  • @AshwinPhadke 用文字描述代码只会让这更加混乱。显示您想要的代码,并显示所涉及的类型是什么。

标签: c++ opencv vector std stdvector


【解决方案1】:

使用std::tuple

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

return std::make_tuple(quantized, viewable, dom, colors);

在 C++17 中,您可以使用structured bindings 方便地处理返回值:

auto [quantized, viewable, dom, colors] = find_dominant_colors(matImage, count);

如果你没有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);

或者你可以让类型推导为你工作,并使用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);

【讨论】:

  • 优先于定义新的数据结构。 :-)
  • @Gupta 基本上这是在声明一个新的数据结构,主要区别在于您不能为元组元素提供自定义名称,当然,当您可以使用 C++17 时,结构化绑定很不错,这是一个权衡
  • @Gnawme 我收到类型转换错误,如果不使用 C++17,我该如何处理返回值? .我也尝试将其声明为单个变量,但没有成功。
  • 我针对非 C++17 扩展了我的答案,希望对您有所帮助。
  • @Gnawme 在此处检查您的编辑之前,我按照函数返回参数的顺序使用了 std::tie,并且类似地在 main 中声明了它们,但错误仍然存​​在。它是否与定义我在std::tuple&lt;cv::Mat, cv::Mat, cv::Mat, cv::Vec3b&gt; find_dominant_colors(cv::Mat img, int count) { code }中定义的函数有关?
【解决方案2】:

如果你有一个函数

std::vector<cv::Vec3b> colors = find_dominant_colors(matImage, count);

现在你想改变函数返回“更多”,那么你可以声明一个数据结构

struct find_dominant_colors_result {
    std::vector<cv::Vec3b> colors;
    cv::Mat quantized;
    cv::Mat viewable;
    cv::Mat dom;
};

现在调用看起来像这样:

find_dominant_colors_result x = find_dominant_colors(matImage, count);

或者说

auto x = find_dominant_colors(matImage, count);

虽然您必须将函数修改为

find_dominant_colors_result find_dominant_colors(cv::Mat img, int count) {
    find_dominant_color_result result;
    const int width = img.cols;
    const int height = img.rows;
    result.colors = get_dominant_colors(root);

    result.quantized = get_quantized_image(classes, root);
    result.viewable = get_viewable_image(classes);
    result.dom = get_dominant_palette(result.colors);

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

    return result;
}

【讨论】:

  • ` 要求从“std::vector<:vec char> >”转换为非标量类型“find_dominant_colors_result” 333 | find_dominant_colors_result x = find_dominant_colors(matImage, count); `
  • @AshwinPhadke 当然,您必须调整的不仅仅是函数调用。您还必须调整功能
猜你喜欢
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2011-07-06
  • 2011-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多