您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“OpenCV怎么實(shí)現(xiàn)多幅圖像顯示在同一個(gè)窗口”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“OpenCV怎么實(shí)現(xiàn)多幅圖像顯示在同一個(gè)窗口”吧!
用戶需要提供的參數(shù)如下:
1、圖像序列的 Mat 的 Vector;
2、類似于matlab的subplot 設(shè)置;
3、單張圖像顯示的最大尺寸,默認(rèn)為cvSize(400, 280);
因時(shí)間限制,該版代碼只能批量顯示同尺寸的圖像。
代碼如下:
/************************************************************************ * 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創(chuàng)建時(shí)的類型,CV_8UC3(8位3通道,具體可參看http://blog.csdn.net/yang_xian521/article/details/7107786)。 讀者可根據(jù)自身圖像數(shù)據(jù)類型修改,或者利用參數(shù)傳遞進(jìn)來(CV_8UC3 實(shí)質(zhì)是個(gè)整數(shù)宏)。
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()); }
到此,相信大家對(duì)“OpenCV怎么實(shí)現(xiàn)多幅圖像顯示在同一個(gè)窗口”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。