为了能更好地、更灵活地在OpenCV中实现,同一窗口内显示多幅图像,尝试了Yang Xian 提供的代码:
http://blog.csdn.net/yang_xian521/article/details/7915396
非常感谢他提供的方法框架、思路。但在使用中发现该版代码局限性仍比较大,不能灵活地控制、显示多幅图像。所以本人花时间在其基础上进行了修改,得到了更为通用的版本。用户需要提供的参数如下:
1、图像序列的 Mat 的 Vector;
2、类似于matlab的subplot 设置;
3、单张图像显示的最大尺寸,默认为cvSize(400, 280);
因时间限制,该版代码只能批量显示同尺寸的图像。
代码如下:
/************************************************************************ * Author : Xin Yang * Date : 2014/03/21 * Address : Shenzhen Univ, School of medicine. * Email : [email protected] * function: Show multiple images in one window ************************************************************************/ #include "opencv2/opencv.hpp" using namespace cv; using namespace std; void MultiImage_OneWin(const std::string& MultiShow_WinName, const vector<Mat>& SrcImg_V, CvSize SubPlot, CvSize ImgMax_Size = cvSize(400, 280)); int main(void) { vector<Mat> imgs(4); imgs[0] = imread("F:\\SA.jpg"); imgs[1] = imread("F:\\SA.jpg"); imgs[2] = imread("F:\\SA.jpg"); imgs[3] = imread("F:\\SA.jpg"); MultiImage_OneWin("Multiple Images", imgs, cvSize(2, 2), cvSize(400,280)); return 0; }
[ 修改注意 20141119 ] 注意下面代码中39行,Disp_Img创建时的类型,CV_8UC3(8位3通道,具体可参看http://blog.csdn.net/yang_xian521/article/details/7107786)。 读者可根据自身图像数据类型修改,或者利用参数传递进来(CV_8UC3 实质是个整数宏)。
void MultiImage_OneWin(const std::string& MultiShow_WinName, const vector<Mat>& SrcImg_V, CvSize SubPlot, CvSize ImgMax_Size) { //Reference : http://blog.csdn.net/yangyangyang20092010/article/details/21740373 //************* Usage *************// //vector<Mat> imgs(4); //imgs[0] = imread("F:\\SA2014.jpg"); //imgs[1] = imread("F:\\SA2014.jpg"); //imgs[2] = imread("F:\\SA2014.jpg"); //imgs[3] = imread("F:\\SA2014.jpg"); //MultiImage_OneWin("T", imgs, cvSize(2, 2), cvSize(400, 280)); //Window's image Mat Disp_Img; //Width of source image CvSize Img_OrigSize = cvSize(SrcImg_V[0].cols, SrcImg_V[0].rows); //******************** Set the width for displayed image ********************// //Width vs height ratio of source image float WH_Ratio_Orig = Img_OrigSize.width/(float)Img_OrigSize.height; CvSize ImgDisp_Size = cvSize(100, 100); if(Img_OrigSize.width > ImgMax_Size.width) ImgDisp_Size = cvSize(ImgMax_Size.width, (int)ImgMax_Size.width/WH_Ratio_Orig); else if(Img_OrigSize.height > ImgMax_Size.height) ImgDisp_Size = cvSize((int)ImgMax_Size.height*WH_Ratio_Orig, ImgMax_Size.height); else ImgDisp_Size = cvSize(Img_OrigSize.width, Img_OrigSize.height); //******************** Check Image numbers with Subplot layout ********************// int Img_Num = (int)SrcImg_V.size(); if(Img_Num > SubPlot.width * SubPlot.height) { cout<<"Your SubPlot Setting is too small !"<<endl; exit(0); } //******************** Blank setting ********************// CvSize DispBlank_Edge = cvSize(80, 60); CvSize DispBlank_Gap = cvSize(15, 15); //******************** Size for Window ********************// Disp_Img.create(Size(ImgDisp_Size.width*SubPlot.width + DispBlank_Edge.width + (SubPlot.width - 1)*DispBlank_Gap.width, ImgDisp_Size.height*SubPlot.height + DispBlank_Edge.height + (SubPlot.height - 1)*DispBlank_Gap.height), CV_8UC3); Disp_Img.setTo(0);//Background //Left top position for each image int EdgeBlank_X = (Disp_Img.cols - (ImgDisp_Size.width*SubPlot.width + (SubPlot.width - 1)*DispBlank_Gap.width))/2; int EdgeBlank_Y = (Disp_Img.rows - (ImgDisp_Size.height*SubPlot.height + (SubPlot.height - 1)*DispBlank_Gap.height))/2; CvPoint LT_BasePos = cvPoint(EdgeBlank_X, EdgeBlank_Y); CvPoint LT_Pos = LT_BasePos; //Display all images for (int i=0; i < Img_Num; i++) { //Obtain the left top position if ((i%SubPlot.width == 0) && (LT_Pos.x != LT_BasePos.x)) { LT_Pos.x = LT_BasePos.x; LT_Pos.y += (DispBlank_Gap.height + ImgDisp_Size.height); } //Writting each to Window's Image Mat imgROI = Disp_Img(Rect(LT_Pos.x, LT_Pos.y, ImgDisp_Size.width, ImgDisp_Size.height)); resize(SrcImg_V[i], imgROI, Size(ImgDisp_Size.width, ImgDisp_Size.height)); LT_Pos.x += (DispBlank_Gap.width + ImgDisp_Size.width); } //Get the screen size of computer int Scree_W = GetSystemMetrics(SM_CXSCREEN); int Scree_H = GetSystemMetrics(SM_CYSCREEN); cvNamedWindow(MultiShow_WinName.c_str(), CV_WINDOW_AUTOSIZE); cvMoveWindow(MultiShow_WinName.c_str(),(Scree_W - Disp_Img.cols)/2 ,(Scree_H - Disp_Img.rows)/2);//Centralize the window cvShowImage(MultiShow_WinName.c_str(), &(IplImage(Disp_Img))); cvWaitKey(0); cvDestroyWindow(MultiShow_WinName.c_str()); }
25张图像,SubPlot = 5*5
11张图像,SubPlot = 4*3
若出现显示结果太大,请修改 MultiImage_OneWin 函数的第4个参数,该参数对应你能承受的、单张图像最大显示尺寸。
另外,当显示图像实在很大时,可予以全尺寸、小窗部分显示,然后结合鼠标拖动进行查看。这个博客提供了可运行的鼠标拖动查看的代码:
http://kanwoerzi.iteye.com/blog/1304073
若有好心人将该模块添加进来,希望能共享一下,个人时间有限,无法顾及了。
若程序有bug,望指出。