opencv单个窗口显示多幅图像

来源:互联网 发布:qq影音 for mac 编辑:程序博客网 时间:2024/06/01 09:49

功能描述:单个窗口显示多幅图像,类似于Matlab中的函数subplot,不过没有这个函数功能强大,只是实现了显示,可以设置显示窗口的的大小,子显示窗口的数量,灰度以及二值图像。

主要函数:resize,copyto,cvtcolor

环境:windows7+vs2010+opencv2.4.8

工程地址:http://download.csdn.net/detail/chengkun183/7611889

源代码:

// MergrFrame.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "opencv2/core/core.hpp"#include "opencv2/highgui/highgui.hpp"#include "opencv2/imgproc/imgproc.hpp"#include <iostream>using namespace std;using namespace cv;#define readcamera 0;//参数为1读取摄像头的视频,参数为0读取一张图片void MergeImage(int row,//显示窗口的子窗口行数int col,//显示窗口的子窗口列数int width,//显示窗口的宽度int height,//显示窗口的高度vector<Mat>&image,//要显示的图像的向量Mat &MerImg,//显示窗口图片int isResizeImage=1//是否缩放图片,默认等于1按图像原始比例缩放到子窗口中//等于0,按子窗口的的比例显示图像);vector<Mat>image;Mat MerImg;int _tmain(int argc, _TCHAR* argv[]){#if readcameraVideoCapture cap(0);Mat frame;Mat gray,binimg,srchsv;while (cap.isOpened()){cap>>frame;Mat src=frame.clone();cvtColor(src,gray,CV_BGR2GRAY);//原始图像转换为灰度,此时为单通道,用来测试显示cvtColor(src,srchsv,CV_BGR2HSV);//原始图像转换颜色空间,此时为三通道,用来测试显示threshold(gray,binimg,180,255,CV_THRESH_BINARY);//转换为二值图像,此时为单通道,测试显示image.push_back(src);image.push_back(srchsv);image.push_back(gray);image.push_back(binimg);MergeImage(2,2,900,600,image,MerImg);image.clear();//清空向量,为下一次做准备imshow("MerImg",MerImg);char c=waitKey(40);//按esc退出if (c==27){break;}}#elseMat src=imread("lena.png");Mat gray,binimg,srchsv;cvtColor(src,gray,CV_BGR2GRAY);cvtColor(src,srchsv,CV_BGR2HSV);threshold(gray,binimg,180,255,CV_THRESH_BINARY);image.push_back(src);image.push_back(srchsv);image.push_back(gray);image.push_back(binimg);MergeImage(2,2,800,600,image,MerImg);imshow("MerImg",MerImg);waitKey(60000);#endifreturn 0; }void MergeImage(int row,int col,int width,int height,vector<Mat>&inputimage,Mat &MerImg,int isResizeImage){int EachWndWidth;int EachWndHeight;Rect RectRoi;Mat TempImg;vector<Mat>image;//检查图像子窗口和图像的数量是否相等,不等:输出提示信息if (row*col<inputimage.size()){cout<<"The number of display window is less than number of image and cannot display completely!"<<endl;} EachWndWidth=width/col;//每个子窗口的宽度EachWndHeight=height/row;//每个子窗口的高度//检查图像的通道数,将单通道图像转化成三通道用来显示for (int i=0;i<inputimage.size();i++){if (inputimage[i].channels()==1){cvtColor(inputimage[i],inputimage[i],CV_GRAY2BGR);}}//是否缩放图像if (isResizeImage){for (int i=0;i<inputimage.size();i++){float    cw=(float)EachWndWidth;    //显示区的宽float    ch=(float)EachWndHeight;   //显示区的高float    pw=(float)inputimage[i].cols; //载入图像的宽float    ph=(float)inputimage[i].rows;//载入图像的高float    cs=cw/ch;//显示区的宽高比float    ps=pw/ph;//载入图像的宽高比float    scale=(cs>ps)?(ch/ph):(cw/pw); //缩放比例因子int      rw=(int)pw*scale;//缩放后图片的宽int      rh=(int)ph*scale;//缩放后图片的高 Mat      TempMat=Mat(Size(rw,rh),CV_8UC3);Mat      DisplayMat=Mat(Size(cw,ch),CV_8UC3);Rect RectRoiRoi=Rect((cw-rw)/2,(ch-rh)/2,rw,rh);resize(inputimage[i],TempMat,Size(rw,rh));TempMat.copyTo(DisplayMat(RectRoiRoi));image.push_back(DisplayMat);}}else{for (int i=0;i<inputimage.size();i++){image.push_back(inputimage[i]);}}//显示窗口中显示的图像MerImg=Mat(height,width,CV_8UC3);for (int i=0;i<row;i++){for (int j=0;j<col;j++){RectRoi=Rect(0+j*EachWndWidth,0+i*EachWndHeight,EachWndWidth,EachWndHeight);if (i*col+j<image.size()){resize(image[i*col+j],TempImg,Size(EachWndWidth,EachWndHeight));TempImg.copyTo(MerImg(RectRoi));}}}image.clear();}

摄像头显示结果:


lena.jpg图像显示结果:


0 0