如何在 C++ 向量中解压多个值
How do I get multiple values unpacked in C++ vectors
我有这个功能,可以从这里returns RGB 格式的颜色
std::vector<cv::Vec3b> colors = find_dominant_colors(matImage, count);
但现在我还想要函数 find_dominant_colors
生成的图像到 return 以便我可以使用它。它生成我使用 cv::imwrite
编写的三个图像,但我希望将这三个图像 returned 到函数调用中,以便我可以在 returned 而不是直接查看它正在为它获取目录。
如何解压缩该行代码中的多个值,例如获取图像和颜色,而不仅仅是颜色。我必须使用多个向量吗?我怎么做 ?
这里使用的向量是一个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;
}
以上函数 returns 颜色到这里
std::vector<cv::Vec3b> colors = find_dominant_colors(matImage, count);
我也想要 return viewable quantized dom
,我该怎么做?
如果你有函数
std::vector<cv::Vec3b> colors = find_dominant_colors(matImage, count);
现在你想把函数改成return"more",那么你可以声明一个数据结构
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::tuple
.
将 find_dominant_colors
函数定义为 return a std::tuple<cv::Mat, cv::Mat, cv::Mat, cv::Vec3b>
,并在 return
语句中执行此操作:
return std::make_tuple(quantized, viewable, dom, colors);
在 C++17 中,您可以使用 structured bindings 以方便的方式处理 returned 值:
auto [quantized, viewable, dom, colors] = find_dominant_colors(matImage, count);
如果您没有 C++17,请使用 std::tie
来处理 returned 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
访问 returned 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);
我有这个功能,可以从这里returns RGB 格式的颜色
std::vector<cv::Vec3b> colors = find_dominant_colors(matImage, count);
但现在我还想要函数 find_dominant_colors
生成的图像到 return 以便我可以使用它。它生成我使用 cv::imwrite
编写的三个图像,但我希望将这三个图像 returned 到函数调用中,以便我可以在 returned 而不是直接查看它正在为它获取目录。
如何解压缩该行代码中的多个值,例如获取图像和颜色,而不仅仅是颜色。我必须使用多个向量吗?我怎么做 ? 这里使用的向量是一个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;
}
以上函数 returns 颜色到这里
std::vector<cv::Vec3b> colors = find_dominant_colors(matImage, count);
我也想要 return viewable quantized dom
,我该怎么做?
如果你有函数
std::vector<cv::Vec3b> colors = find_dominant_colors(matImage, count);
现在你想把函数改成return"more",那么你可以声明一个数据结构
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::tuple
.
将 find_dominant_colors
函数定义为 return a std::tuple<cv::Mat, cv::Mat, cv::Mat, cv::Vec3b>
,并在 return
语句中执行此操作:
return std::make_tuple(quantized, viewable, dom, colors);
在 C++17 中,您可以使用 structured bindings 以方便的方式处理 returned 值:
auto [quantized, viewable, dom, colors] = find_dominant_colors(matImage, count);
如果您没有 C++17,请使用 std::tie
来处理 returned 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
访问 returned 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);