VScode+opencv——关于opencv多张图片拼接成一张图片的算法
用于显示多张图片——变量为图片组,图像尺寸,间隙,边界值,输出图片
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
//用于显示多张图片,变量为图片组,图像尺寸,间隙,边界值,输出图片
void ShowManyImages (const vector<cv::Mat>&srcImages,int nShowImageSize,int nSplitLineSize,int nAroundLineSize,Mat &outImage)
{
//图片数量
int nNumImages=srcImages.size();
//窗口大小
cv::Size nSizeWindows;
if(nNumImages>12)
{
cout<<"不要超过12张,傻逼吧你"<<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 5:
case 6:nSizeWindows=Size(3,2);break;
case 7:
case 8:nSizeWindows=Size(4,2);break;
case 9:nSizeWindows=Size(3,3);break;
default:nSizeWindows=Size(4,3);break;
}
//图像尺寸,间隙,边界值
//创建输出图像
const int imagesHeight =nShowImageSize*nSizeWindows.width+nAroundLineSize+(nSizeWindows.width-1)*nSplitLineSize;
const int imagesWidth =nShowImageSize*nSizeWindows.height+nAroundLineSize+(nSizeWindows.height-1)*nSplitLineSize;
Mat showWindowsImages(imagesWidth,imagesHeight,CV_8UC3,Scalar(0,0,0));
//提取小图像左上角x,y
int posX = (showWindowsImages.cols-(nShowImageSize*nSizeWindows.width+(nSizeWindows.width-1)*nSplitLineSize))/2;
int posY = (showWindowsImages.rows-(nShowImageSize*nSizeWindows.height+(nSizeWindows.height-1)*nSplitLineSize))/2;
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);
}
//小图像置于大图像处
Mat tempImage=showWindowsImages(Rect(tempPosX,tempPosY,nShowImageSize,nShowImageSize));
//利用resize实现图像缩放
resize(srcImages[i],tempImage,Size(nShowImageSize,nShowImageSize));
tempPosX += (nSplitLineSize+nShowImageSize);
}
outImage = showWindowsImages;
}
原文地址:https://blog.csdn.net/m0_66701835/article/details/143759064
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!