原图1:
原图2:
合并后效果:
代码如下:
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat img1 = imread("D:/tupian/gril2.jpg");
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", img1);
Mat img2 = imread("D:/tupian/ruanbin.jpg");
namedWindow("img2");
imshow("img2", img2);
int height = img1.rows;//随便设置一个图片的行为高
int weight1 = img1.cols;
int weight2 = img2.cols;
if (img1.rows > img2.rows)
{
height = img2.rows;
weight1 = img1.cols*((float)img2.rows/(float)img1.rows);//按比例缩小或放大
resize(img1, img1, Size(weight1, height));
}
else if(img1.rows<img2.rows)
{
weight2 = img2.cols * ((float)img1.rows / (float)img2.rows);//同理
resize(img2, img2, Size(weight2, height));
}
Mat des;
des.create(height, weight1 + weight2, img1.type());//创建一新的图片用来放置两个图片的合并
Mat r1 = des(Rect(0, 0, weight1, height));//将图片放在新创建的图片合适位置
img1.copyTo(r1);//将图1复制到该区域
Mat r2 = des(Rect(weight1, 0, weight2, height));
img2.copyTo(r2);
namedWindow("des");
imshow("des", des);
imwrite("D:/tupian/xiaoguo.jpg",des);
waitKey(0);
return 0;
}