Add the contents of 2 Mats to another Mat opencv c++

て烟熏妆下的殇ゞ 提交于 2019-12-04 12:07:34

问题


I just want to add the contents of 2 different Mats to 1 other Mat. I tried:

Mat1.copyTo(newMat);
Mat2.copyTo(newMat);

But that just seemed to overwrite the previous contents of the Mat.

This may be a simple question, but I'm lost.


回答1:


It depends on what you want to add. For example, you have two 3x3 Mat:

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:

auto matC = matA + matB;

Or using array operation cv::add that does the same job:

cv::Mat matD;
cv::add(matA, matB, matD);

Or even mixing two images using 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:

cv::Mat matF;
matF.push_back(matA);
matF.push_back(matB);

Even merge into a two-channel 3x3 Mat using cv::merge:

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.




回答2:


You can use push_back():

newMat.push_back(Mat1);
newMat.push_back(Mat2);


来源:https://stackoverflow.com/questions/28590031/add-the-contents-of-2-mats-to-another-mat-opencv-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!