// exercise9.1单窗口显示多图像.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
/*****************************鼠标写字功能***************************/
#include<opencv.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
void on_mouse(int event, int x, int y, int flags, void *para);
void showManyImages(const vector<Mat>&srcImages, Size imageSize);
int main() {
vector<Mat>srcImage(4);
srcImage[0] = imread("d:\\img\\1.jpg");
srcImage[1] = imread("d:\\img\\1.jpg");
srcImage[2] = imread("d:\\img\\1.jpg");
srcImage[3] = imread("d:\\img\\1.jpg");
for (int i = 0; i < srcImage.size(); i++) {
if (srcImage[i].empty()) {
cout << "read error" << endl;
return -1;
}
}
showManyImages(srcImage,Size(300, 300));//
waitKey(0);
system("pause");
return 0;
}
void showManyImages(const vector<Mat>&srcImages, Size imageSize) {//第二个参数在哪?
int nNumImages = srcImages.size();
Size nSizeWindows;
if (nNumImages > 12) {
cout << "no more tha 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 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);
}
//设置小图像尺寸,间隙,边界
int nShowImageSize = 200;
int nSplitLineSize = 15;
int nAroundLineSize = 50;
//创建输出图像,图像大小根据输入源来确定
const int imagesHeight = nShowImageSize * nSizeWindows.width + nAroundLineSize + (nSizeWindows.width - 1)*nSplitLineSize;
const int imagesWidth = nShowImageSize * nSizeWindows.height + nAroundLineSize + (nSizeWindows.height - 1)*nSplitLineSize;
cout << imagesWidth << " " << imagesHeight << endl;
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;
cout << posX << " " << 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 = showWindowsImages(Rect(tempPosX, tempPosY, nShowImageSize, nShowImageSize));
resize(srcImages[i], tempImage, Size(nShowImageSize, nShowImageSize));//利用resize函数实现图像缩放
tempPosX += (nSplitLineSize + nShowImageSize);
}//for
//namedWindow("单窗口显示多图片",WINDOW_GUI_EXPANDED);
imshow("单窗口显示多图片", showWindowsImages);
}
exercise9. 单窗口显示多图像
最新推荐文章于 2024-11-12 15:18:48 发布