您好,登錄后才能下訂單哦!
本篇內容主要講解“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。