您好,登录后才能下订单哦!
本篇内容主要讲解“OpenCV怎么实现多幅图像显示在同一个窗口”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“OpenCV怎么实现多幅图像显示在同一个窗口”吧!
用户需要提供的参数如下:
1、图像序列的 Mat 的 Vector;
2、类似于matlab的subplot 设置;
3、单张图像显示的最大尺寸,默认为cvSize(400, 280);
因时间限制,该版代码只能批量显示同尺寸的图像。
代码如下:
/************************************************************************ * Author : Xin Yang * Date : 2014/03/21 * Address : Shenzhen Univ, School of medicine. * Email : xinyang@szu.edu.cn * 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()); }
到此,相信大家对“OpenCV怎么实现多幅图像显示在同一个窗口”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。