#include "stdafx.h"
#include <stdarg.h> // va_list, va_start, va_arg, va_end
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <iostream>
using namespace std;
void cvShowMultiImages(char* title,int nChannels, int nArgs, ...)
{
// img - Used for getting the arguments
IplImage* img;
// DispImage - the image in which all the input images are to be copied
IplImage* DispImage;
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
// r - Maximum number of images in a column
// c - Maximum number of images in a row
int r, c;
// space - the spacing between images
int space_r,space_c;
// If the number of arguments is lesser than 0 or greater than 12
// return without displaying
if(nArgs <= 0) {
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
// Check whether it is NULL or not
// If it is NULL, release the image, and return
if(img == 0) {
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;
}
// Set the image ROI to display the current image
cvSetImageROI(DispImage, cvRect(x_c, y_r, size_c, size_r));
//cvSetImageROI(DispImage, cvRect(x_c, y_r, (int)( w/scale_x ), (int)( h/scale_y )));
// Resize the input image and copy the it to the Single Big Image
cvResize(img, DispImage);
// Reset the ROI in order to display the next image
cvResetImageROI(DispImage);
}
// Create a new window, and show the Single Big Image
cvNamedWindow( title, 1 );
cvShowImage( title, DispImage);
cvWaitKey(0);
// End the number of arguments
va_end(args);
// Release the Image Memory
cvReleaseImage(&DispImage);
}
int main( int argc, char** argv )
{
IplImage* src1 = cvLoadImage("1002.png" );
IplImage* src2 = cvLoadImage("1002.png" );
IplImage* src3 = cvLoadImage("1002.png" );
IplImage* src4 = cvLoadImage("1002.png" );
cvShowMultiImages("1",3,4,src1,src2,src3,src4);
system("pause");
return 0;
}