How to Display more than one image in a single window:¶
There is no inbuilt support to display more than one image in OpenCV. Here is a function illustrating how to display more than one image in a single window using Intel OpenCV. The method used is to set the ROIs of a Single Big image and then resizing and copying the input images on to the Single Big Image.
1#include <cv.h>
2#include <highgui.h>
3
4#include <stdio.h>
5#include <stdarg.h>
6
7/*Function///////////////////////////////////////////////////////////////
8
9Name: cvShowManyImages
10
11Purpose: This is a function illustrating how to display more than one
12 image in a single window using Intel OpenCV
13
14Parameters: char *title: Title of the window to be displayed
15 int nArgs: Number of images to be displayed
16 ...: IplImage*, which contains the images
17
18Language: C++
19
20The method used is to set the ROIs of a Single Big image and then resizing
21and copying the input images on to the Single Big Image.
22
23This function does not stretch the image...
24It resizes the image without modifying the width/height ratio..
25
26This function can be called like this:
27
28cvShowManyImages("Images", 2, img1, img2);
29or
30cvShowManyImages("Images", 5, img2, img2, img3, img4, img5);
31
32This function can display upto 12 images in a single window.
33It does not check whether the arguments are of type IplImage* or not.
34The maximum window size is 700 by 660 pixels.
35Does not display anything if the number of arguments is less than
36 one or greater than 12.
37
38If you pass a pointer that is not IplImage*, Error will occur.
39Take care of the number of arguments you pass, and the type of arguments,
40which should be of type IplImage* ONLY.
41
42Idea was from [[BettySanchi]] of OpenCV Yahoo! Groups.
43
44If you have trouble compiling and/or executing
45this code, I would like to hear about it.
46
47You could try posting on the OpenCV Yahoo! Groups
48[url]http://groups.yahoo.com/group/OpenCV/messages/ [/url]
49
50Parameswaran,
51Chennai, India.
52
53cegparamesh[at]gmail[dot]com
54
55...
56///////////////////////////////////////////////////////////////////////*/
57
58void cvShowManyImages(char* title, int nArgs, ...) {
59
60 // img - Used for getting the arguments
61 IplImage *img;
62
63 // [[DispImage]] - the image in which input images are to be copied
64 IplImage *DispImage;
65
66 int size;
67 int i;
68 int m, n;
69 int x, y;
70
71 // w - Maximum number of images in a row
72 // h - Maximum number of images in a column
73 int w, h;
74
75 // scale - How much we have to resize the image
76 float scale;
77 int max;
78
79 // If the number of arguments is lesser than 0 or greater than 12
80 // return without displaying
81 if(nArgs <= 0) {
82 printf("Number of arguments too small....\n");
83 return;
84 }
85 else if(nArgs > 12) {
86 printf("Number of arguments too large....\n");
87 return;
88 }
89 // Determine the size of the image,
90 // and the number of rows/cols
91 // from number of arguments
92 else if (nArgs == 1) {
93 w = h = 1;
94 size = 300;
95 }
96 else if (nArgs == 2) {
97 w = 2; h = 1;
98 size = 300;
99 }
100 else if (nArgs == 3 || nArgs == 4) {
101 w = 2; h = 2;
102 size = 300;
103 }
104 else if (nArgs == 5 || nArgs == 6) {
105 w = 3; h = 2;
106 size = 200;
107 }
108 else if (nArgs == 7 || nArgs == 8) {
109 w = 4; h = 2;
110 size = 200;
111 }
112 else {
113 w = 4; h = 3;
114 size = 150;
115 }
116
117 // Create a new 3 channel image
118 [[DispImage]] = cvCreateImage( cvSize(100 + size*w, 60 + size*h), 8, 3 );
119
120 // Used to get the arguments passed
121 va_list args;
122 va_start(args, nArgs);
123
124 // Loop for nArgs number of arguments
125 for (i = 0, m = 20, n = 20; i < nArgs; i++, m += (20 + size)) {
126
127 // Get the Pointer to the IplImage
128 img = va_arg(args, IplImage*);
129
130 // Check whether it is NULL or not
131 // If it is NULL, release the image, and return
132 if(img == 0) {
133 printf("Invalid arguments");
134 cvReleaseImage(&DispImage);
135 return;
136 }
137
138 // Find the width and height of the image
139 x = img->width;
140 y = img->height;
141
142 // Find whether height or width is greater in order to resize the image
143 max = (x > y)? x: y;
144
145 // Find the scaling factor to resize the image
146 scale = (float) ( (float) max / size );
147
148 // Used to Align the images
149 if( i % w == 0 && m!= 20) {
150 m = 20;
151 n+= 20 + size;
152 }
153
154 // Set the image ROI to display the current image
155 cvSetImageROI(DispImage, cvRect(m, n, (int)( x/scale ), (int)( y/scale )));
156
157 // Resize the input image and copy the it to the Single Big Image
158 cvResize(img, DispImage);
159
160 // Reset the ROI in order to display the next image
161 cvResetImageROI(DispImage);
162 }
163
164 // Create a new window, and show the Single Big Image
165 cvNamedWindow( title, 1 );
166 cvShowImage( title, DispImage);
167
168 cvWaitKey();
169 cvDestroyWindow(title);
170
171 // End the number of arguments
172 va_end(args);
173
174 // Release the Image Memory
175 cvReleaseImage(&DispImage);
176}
177
You can use this function as in this sample program:
1int main() {
2
3 IplImage *img1 = cvLoadImage("Image01.jpg");
4
5 IplImage *img2 = cvLoadImage("Image02.jpg");
6
7 IplImage *img3 = cvLoadImage("Image03.jpg");
8
9 IplImage *img4 = cvLoadImage("Image04.jpg");
10
11 IplImage *img5 = cvLoadImage("Image05.jpg");
12
13 IplImage *img6 = cvLoadImage("Image06.jpg");
14
15 cvShowManyImages("Image", 6, img1, img2, img3, img4, img5, img6);
16
17 return 0;
18
19}
20
The method used is to set the ROIs of a Single Big image and then resizing and copying the input images on to the Single Big Image.
This function does not stretch the image... It resizes the image without modifying the width/height ratio..
This function can be called like this:
1 cvShowManyImages("Image", 2, img1, img2);
or
1 cvShowManyImages("Image", 6, img1, img2, img3, img4, img5, img6);
upto 12 images.
This function can display upto 12 images in a single window. It does not check whether the arguments are of type IplImage* or not. The maximum window size is 700 by 660 pixels. Does not display anything if the number of arguments is less than one or greater than 12.
If you pass a pointer that is not IplImage*, Error will occur. Take care of the number of arguments you pass, and the type of arguments, which should be of type IplImage* ONLY.
Idea was from BettySanchi of OpenCV Yahoo! Groups.
If you have trouble compiling and/or executing this code, I would like to hear about it.
Here is a sample ScreenShot:
Regards :) Paramesh.