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