opencv将多张图像放在一张大图里

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/love_image_xie/article/details/87610802
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/core/core.hpp>
#include<cmath>
#include<iostream>
#include<vector>
using namespace std;
using namespace cv;

//在一幅图像上显示多张图像
void showManyImages(vector<Mat>& src, Size imgSize)
{
	int nNumImages = src.size();
	Size nSizeWindows;
	if (nNumImages > 12)
	{
		cout << "Not more than 12 images!" << endl;
		return;
	}
	//根据图片数量确定分割小窗口的排布
	switch (nNumImages)
	{
	case 1:nSizeWindows = Size(1, 1); break;
	case 2:nSizeWindows = Size(2, 1); break;
	case 3:
	case 4:nSizeWindows = Size(2, 2); break;
	case 6:nSizeWindows = Size(3, 2); break;
	case 8:nSizeWindows = Size(4, 2); break;
	case 9:nSizeWindows = Size(3, 3); break;
	default:nSizeWindows = Size(4, 3); break;
	}
	//设置图像的大小,图像之间的间隔以及边界
	int nShowImageSize = 200;
	int nSplitLineSize = 15;
	int nAroundLineSize = 50;
	//创建输出图像的大小
	int imageWidth = nShowImageSize*nSizeWindows.width + (nShowImageSize - 1)*nSplitLineSize + nAroundLineSize;
	int imageHeight = nShowImageSize*nSizeWindows.height + (nShowImageSize - 1)*nSplitLineSize + nAroundLineSize;
	Mat showWindowImages(imageHeight, imageWidth, CV_8UC3, Scalar::all(0));
	//提取小图像左上角坐标
	int posX = nAroundLineSize / 2;
	int posY = nAroundLineSize / 2;
	cout << "posX: " << posX << " posY: " << posY << endl;
	int tempPosX = posX;
	int tempPosY = posY;
	//将每张小图整合到大图中
	for (int i = 0; i < nNumImages; i++)
	{
		if ((i%nSizeWindows.width == 0) && (tempPosX != posX))
		{
			tempPosX = posX;
			tempPosY += (nSplitLineSize + nShowImageSize);
		}
		//利用Rect区域将小图像置于大图像相应区域
		Mat tempImage = showWindowImages(Rect(tempPosX, tempPosY, nShowImageSize, nShowImageSize));
		//利用resize实现图像缩放
		resize(src[i],tempImage,Size(nShowImageSize,nShowImageSize));
		tempPosX += nSplitLineSize + nShowImageSize;
	}
	imshow("showWindowImages",showWindowImages);
	imwrite("E:\\研究生\\学习材料\\学习书籍\\OpenCV图像处理编程实例 - 源码 - 20160801\\《OpenCV图像处理编程实例 - 源码 - 20160801\\images\\flowermerge.jpg", showWindowImages);
}

int main()
{
	vector<Mat> src(9);
	src[0] = imread("E:\\研究生\\学习材料\\学习书籍\\OpenCV图像处理编程实例-源码-20160801\\《OpenCV图像处理编程实例-源码-20160801\\images\\flower.jpg");
	src[1] = imread("E:\\研究生\\学习材料\\学习书籍\\OpenCV图像处理编程实例-源码-20160801\\《OpenCV图像处理编程实例-源码-20160801\\images\\flower.jpg");
	src[2] = imread("E:\\研究生\\学习材料\\学习书籍\\OpenCV图像处理编程实例-源码-20160801\\《OpenCV图像处理编程实例-源码-20160801\\images\\flower.jpg");
	src[3] = imread("E:\\研究生\\学习材料\\学习书籍\\OpenCV图像处理编程实例-源码-20160801\\《OpenCV图像处理编程实例-源码-20160801\\images\\flower2.jpg");
	src[4] = imread("E:\\研究生\\学习材料\\学习书籍\\OpenCV图像处理编程实例-源码-20160801\\《OpenCV图像处理编程实例-源码-20160801\\images\\flower2.jpg");
	src[5] = imread("E:\\研究生\\学习材料\\学习书籍\\OpenCV图像处理编程实例-源码-20160801\\《OpenCV图像处理编程实例-源码-20160801\\images\\flower3.jpg");
	src[6] = imread("E:\\研究生\\学习材料\\学习书籍\\OpenCV图像处理编程实例-源码-20160801\\《OpenCV图像处理编程实例-源码-20160801\\images\\flower3.jpg");
	src[7] = imread("E:\\研究生\\学习材料\\学习书籍\\OpenCV图像处理编程实例-源码-20160801\\《OpenCV图像处理编程实例-源码-20160801\\images\\flower4.jpg");
	src[8] = imread("E:\\研究生\\学习材料\\学习书籍\\OpenCV图像处理编程实例-源码-20160801\\《OpenCV图像处理编程实例-源码-20160801\\images\\flower4.jpg");
	for (int i = 0; i < src.size(); i++)
	{
		if (!src[i].data)
			return -1;
	}
	
	showManyImages(src, Size(512, 400));
	waitKey(0);
	return 0;
}



posX: 25 posY: 25

生成的图如下:

 

展开阅读全文

没有更多推荐了,返回首页