摘要

本文主要介绍OpenCV中同时显示多张IplImage图像的方法(C++形式的多图显示需要修改,用vector<Mat>可能比较方便),有点类似MATLAB中的subplot,只是暂时还没有subplot那么完善,这种方法主要思想和用到的技术为:

  • 主要思想:将多张小图组合成一张大图来显示
  • 组合方式:按照图片的数量,将大图分割成特定的行列数,每一个小块放一张子图
  • 输入方式:使用 中的 type va_list ,就可定义形参数目不确定的函数了。

1. va_list

  • va_start Initialize a variable argument list (macro ) 初始化可变参数列
  • va_arg Retrieve next argument (macro ) 读取下一个参数
  • va_end End using variable argument list (macro ) 结束可变参数列的使用

    下面是va_list的一个应用例子:

void PrintFloats (int n, ...)
{
int i;
double val;
printf ("Printing floats:");
va_list vl;
va_start(vl,n);
for (i=0;i<n;i++)
{
val=va_arg(vl,double);
printf (" [%.2f]",val);
}
va_end(vl);
printf ("\n");
}

2. cvShowMultiImages

程序需要的头文件:

#include <stdio.h>  // printf
#include <stdarg.h> // va_list, va_start, va_arg, va_end
#include "cv.h"
#include "opencv2/highgui/highgui_c.h"

主体程序:

void cvShowMultiImages(char* title,int nChannels, int nArgs, ...)
{
IplImage* img; // img - Used for getting the arguments
IplImage* DispImage;// DispImage - the image in which all the input images are to be copied
int size_r,size_c; // size - the size of the images in the window
int ind; // ind - the index of the image shown in the window
int x_c, y_r; // x_c,y_r - the coordinate of top left coner of input images
int w, h; // w,h - the width and height of the image
int r, c; // r - row , c - col
int space_r,space_c;// space - the spacing between images
if(nArgs <= 0) { // If argc < 0 or argc > 12 , return without displaying
printf("Number of arguments too small..../n");
return;
}
else if(nArgs > 12) {
printf("Number of arguments too large..../n");
return;
}
// Determine the size of the image,
// and the number of rows/cols
// from number of arguments
else if (nArgs == 1) {
r = c = 1;
size_r = 480; size_c = 640 ;
}
else if (nArgs == 2) { // x_c = size_row y_r=size_col
r = 1; c = 2;
// size_r = 705; size_c = 350 ; // specail set for show the full story of lena
size_r = 405; size_c = 540 ;
}
else if ( nArgs == 3 || nArgs == 4) {
r = 2; c = 2;
size_r = 405; size_c =540 ;
}
else if (nArgs == 5 || nArgs == 6) {
r = 2; c = 3;
size_r = 360; size_c = 480;
}
else if (nArgs == 7 || nArgs == 8) {
r = 2; c = 4;
size_r = 200; size_c = 240;
}
else {
r = 3; c = 4;
size_r = 150; size_c = 200;
}
// Create a new 3 channel image to show all the input images
// cvSize(width,height)=(col,row)=(y_r,x_c)
DispImage = cvCreateImage( cvSize(30 + size_c*c,40 + size_r*r), IPL_DEPTH_8U, nChannels );
// Used to get the arguments passed
va_list args;
va_start(args, nArgs); // stdarg.h
// Loop for nArgs number of arguments
space_r = 40/(r+1);
space_c = 30/(c+1);
for (ind = 0, x_c = space_c, y_r = space_r; ind < nArgs; ind++, x_c += (space_c + size_c)) {
// Get the Pointer to the IplImage
img = va_arg(args, IplImage*); // stdarg.h
if(img == 0) {// If img == NULL, release the image, and return
printf("Invalid arguments");
cvReleaseImage(&DispImage);
return;
}
// Find the width and height of the image
w = img->width;
h = img->height;
// Used to Align the images
// i.e. Align the image to next row e.g.r=1,c=2, this row is end , we have ind%c==0 ,
// then we move to the next row,even the next row can't through the cond ind < nArgs
if( ind % c == 0 && x_c!= space_c) {
x_c = space_c;
y_r += space_r + size_r;
}
cvSetImageROI(DispImage, cvRect(x_c, y_r, size_c, size_r)); // Set the image ROI to display the current image
cvResize(img, DispImage); // Resize the input image and copy the it to the Single Big Image
cvResetImageROI(DispImage); // Reset the ROI in order to display the next image
}
cvNamedWindow( title, 1 ); // Create a new window, and show the Single Big Image
cvShowImage( title, DispImage);
cvWaitKey(0);
va_end(args); // End the number of arguments
cvReleaseImage(&DispImage); // Release the Image Memory
}

3. 程序调用方式与结果显示

应用时,请注意所有输入的图像都应该为同一个通道数:1或者3

cvShowMultiImages("Lena1",3,2,src1,edges1Color);

cvShowMultiImages("Lena2",3,2,src2,edges2Color);

显示结果为:

好吧,其实me更想展示的是 ———— The Complete Story of Lena :

reference

  1. va_list - C++ Reference
  2. opencv中文论坛

[OpenCV笔记]0.OpenCV中显示多张图像的更多相关文章

  1. 【QT】QPixmap和QImage在QLabel显示一张图像

    #include <QPixmap> void Dialog::on_Button1_clicked() { QPixmap img; img.load("1.bmp" ...

  2. Android WebView中显示一张或多张图片

    最近需要在平板中显示多张图片,调查了下,决定用WebView(说实话,我还不清楚有没有其他android控件能够显示多张图片的.....), 主要是用HTML的img来显示多张图片. google百度 ...

  3. openCV—Python(2)—— 载入、显示和保存图像

    一.函数简单介绍 1.imread-读取图像 函数原型:imread(filename, flags=None) filename:读取的图像路径名:比如:"H:\img\lena.jpg& ...

  4. OpenCV基本架构[OpenCV 笔记0]

    最近正在系统学习OpenCV,将不定期发布笔记,主要按照毛星云的<OpenCV3编程入门>的顺序学习,会参考官方教程和文档.学习工具是Xcode+CMake,会对书中一部分内容更正,并加入 ...

  5. OpenCV学习5-----使用Mat合并多张图像

    最近做实验需要对比实验结果,需要将几张图片拼在一起,直观对比. 尝试用OpenCV解决. 核心思想其实是   声明一个足够大的,正好容纳下那几张图片的mat,然后将拼图依次copy到大图片相应的位置. ...

  6. MFC中显示一张位图

    1.用类CBitmap加载位图 2.创建内存DC, 将位图选进此内存DC 3.调用BitBlt将内存DC的内容拷贝到其它DC(通知是显示DC) 例子(来自MSDN): // This OnDraw() ...

  7. 利用python进行简单的图像处理:包括打开,显示以及保存图像

    利用python进行简单的图像处理:包括打开,显示以及保存图像 利用PIL处理 PIL(python image library) 是python用于图片处理的package.但目前这个package ...

  8. 第十七周 - OpenCV 学习笔记 S1 - OpenCV 基本函数

    Imread()函数: 基本功能:读取图像到OpenCv中. 1.函数原型: Mat imwrite(const strings& filename, int flag = 1); 第一个参数 ...

  9. 【笔记】PIL 中的 Image 模块

    Image 模块提供了一个同名类(Image),也提供了一些工厂函数,包括从文件中载入图片和创建新图片.例如,以下的脚本先载入一幅图片,将它旋转 45 度角,并显示出来: 1 >>> ...

随机推荐

  1. shell脚本中使用什么工具进行计算

    1.答: expr 2. expr的用法: jello=$(expr 1 \* 3) //乘法,注意1和expr之间有空格,1与转换符\之间有空格,3和*之间有空格 jello=$(expr 1 / ...

  2. [BZOJ4137]火星商店问题

    Description 火星上的一条商业街里按照商店的编号1,2 ,…,n ,依次排列着n个商店.商店里出售的琳琅满目的商品中,每种商品都用一个非负整数val来标价.每个商店每天都有可能进一些新商品, ...

  3. java并发容器之 SynchronousQueue [转]

    SynchronousQueue 这个队列实现了 BlockingQueue接口 该队列的特点 1.容量为0,无论何时 size方法总是返回0 2. put操作阻塞,jquery插件库  直到另外一个 ...

  4. Windows窗体应用程序常用的几个类的属性、方法以及事件

    System.Diagnostics.Process 属性 public bool EnableRaisingEvents { get; set; }//获取或设置在进程终止时是否应激发 Exited ...

  5. SPOJ104 HIGH - Highways

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  6. hdu 5696 区间的价值 单调栈+rmq

    区间的价值 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  7. android 命令行签名apk文件

    签名apk 1.将apk格式改为zip格式包,然后删除原来apk里面的META-INF文件夹,之后改回apk文件格式 2.cmd命令行: jarsigner -verbose -keystore C: ...

  8. 河南省多校联盟二-C

    1281: 邪能炸弹 时间限制: 1 秒  内存限制: 128 MB提交: 222  解决: 80 题目描述 正在入侵艾泽拉斯的古尔丹偶然间得到了一颗邪能炸弹,经过研究,他发现这是一颗威力极其巨大且难 ...

  9. day5-os、sys模块

    一.概述 开发运维相关支撑系统现今已成为Devops下的一大热门领域,Python在这方面也有着自己独到的优势.这类场景以及其他一些场景下,需要调用一些操作系统的接口,这就涉及到今天要讲述的OS模块和 ...

  10. Django开发BUG "Model class WH_auth.models.User doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS."

    当进行数据库迁移的时候发生问题,报错如下:RuntimeError: Model class WH_auth.models.User doesn't declare an explicit app_l ...