将 2 Mats 的内容添加到另一个 Mat opencv c++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28590031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add the contents of 2 Mats to another Mat opencv c++
提问by fakeaccount
I just want to add the contents of 2 different Mat
s to 1 other Mat
. I tried:
我只想将 2 个不同Mat
s的内容添加到 1 other Mat
。我试过:
Mat1.copyTo(newMat);
Mat2.copyTo(newMat);
But that just seemed to overwrite the previous contents of the Mat
.
但这似乎只是覆盖了之前的内容Mat
。
This may be a simple question, but I'm lost.
这可能是一个简单的问题,但我迷路了。
回答by mcchu
It depends on what you want to add. For example, you have two 3x3 Mat:
这取决于您要添加的内容。例如,您有两个 3x3 垫子:
cv::Mat matA(3, 3, CV_8UC1, cv::Scalar(20));
cv::Mat matB(3, 3, CV_8UC1, cv::Scalar(80));
You can add matA
and matB
to a new 3x3 Mat with value 100 using matrix operation:
您可以使用矩阵运算将matA
和添加matB
到值为 100 的新 3x3 Mat 中:
auto matC = matA + matB;
Or using array operation cv::addthat does the same job:
或者使用数组操作cv::add做同样的工作:
cv::Mat matD;
cv::add(matA, matB, matD);
Or even mixingtwo images using cv::addWeighted:
或者甚至使用cv::addWeighted混合两个图像:
cv::Mat matE;
cv::addWeighted(matA, 1.0, matB, 1.0, 0.0, matE);
Sometimes you need to merge two Mat, for example create a 3x6 Mat using cv::Mat::push_back:
有时您需要合并两个 Mat,例如使用cv::Mat::push_back创建一个 3x6 Mat :
cv::Mat matF;
matF.push_back(matA);
matF.push_back(matB);
Even merge into a two-channel 3x3 Mat using cv::merge:
甚至使用cv::merge合并到双通道 3x3 Mat 中:
auto channels = std::vector<cv::Mat>{matA, matB};
cv::Mat matG;
cv::merge(channels, matG);
Think about what you want to add and choose a proper function.
想想你想添加什么并选择一个合适的功能。
回答by zedv
You can use push_back():
您可以使用 push_back():
newMat.push_back(Mat1);
newMat.push_back(Mat2);