您的位置 首页 > 腾讯云社区

“大脑”生长系列(九)---用户1692782

图像合并

copyTo

今天是关于图像合并相关的讲解。首先要区分一下图像合并与图像融合的概念:图像融合说的是两幅不同的图片的叠加,而图像合并说的是将两幅图像经过大小调整实现并排的效果。

想要实现图像的合并,首先要确定基准,即以哪一幅图片为基准来做合并。

1:通过resize调整图片大小;2: 以合并后的图像的大小创建Mat空间; 3:分别将合并前的图像的内容copyTo到新生成的Mat空间中。

copyTo方法的说明

void copyTo( OutputArray m ) const;

说明:copyTo是Mat类的一个接口,参数一个Rect区域,实现的功能是将Mat中的数据拷贝到指定的Rect区域。

一起来看看这段代码吧,能自己动手写一遍就更好啦。

#include <opencv2/core.hpp> #include <opencv2/imgcodecs.hpp> #include <opencv2/highgui.hpp> #include <opencv2/imgproc.hpp> #include <iostream> using namespace cv; using namespace std; int main(int argc, char *argv[]) { Mat img1 = imread("2.jpg"); Mat img2 = imread("3.jpg"); int height = img1.rows; int width1 = img1.cols; int width2 = img2.cols; /* 将高图像等比缩放与低图像一致*/ if (img1.rows > img2.rows) { height = img2.rows; width1 = img1.cols * ((float)img2.rows / img1.rows); resize(img1, img1, Size(width1, height)); } else if(img1.rows < img2.rows) { width2 = img2.cols * ((float)img1.rows / img2.rows); resize(img2, img2, Size(width2, height)); } // 创建目标图像 Mat des; des.create(height, width1 + width2, img1.type()); Mat r1 = des(Rect(0,0, width1, height)); img1.copyTo(r1); Mat r2 = des(Rect(width1, 0, width2, height)); img2.copyTo(r2); namedWindow("des"); imshow("des", des); waitKey(0); return 0; }

对这段代码的说明

(1)使用的策略是,按照两幅图像中高度较低的图像进行等比调整;

(2)使用create创建目标空间,宽为width1 + width2,高为较小图像的height;

(3)将源图像1和源图像2拷贝到目标Mat中指定的空间中。

这是笔者合并后的效果,你可以选择自己喜欢的图片做合并哦。

---来自腾讯云社区的---用户1692782

关于作者: 瞎采新闻

这里可以显示个人介绍!这里可以显示个人介绍!

热门文章

留言与评论(共有 0 条评论)
   
验证码: