Skip to content

Commit 0851528

Browse files
committed
Tutorial Image Pyramids
1 parent f7d85bf commit 0851528

File tree

6 files changed

+298
-121
lines changed

6 files changed

+298
-121
lines changed
Lines changed: 109 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
Image Pyramids {#tutorial_pyramids}
22
==============
33

4+
@prev_tutorial{tutorial_morph_lines_detection}
5+
@next_tutorial{tutorial_threshold}
6+
47
Goal
58
----
69

710
In this tutorial you will learn how to:
811

9-
- Use the OpenCV functions @ref cv::pyrUp and @ref cv::pyrDown to downsample or upsample a given
12+
- Use the OpenCV functions **pyrUp()** and **pyrDown()** to downsample or upsample a given
1013
image.
1114

1215
Theory
@@ -19,7 +22,7 @@ Theory
1922
-# *Upsize* the image (zoom in) or
2023
-# *Downsize* it (zoom out).
2124
- Although there is a *geometric transformation* function in OpenCV that -literally- resize an
22-
image (@ref cv::resize , which we will show in a future tutorial), in this section we analyze
25+
image (**resize** , which we will show in a future tutorial), in this section we analyze
2326
first the use of **Image Pyramids**, which are widely applied in a huge range of vision
2427
applications.
2528

@@ -52,89 +55,147 @@ Theory
5255
predecessor. Iterating this process on the input image \f$G_{0}\f$ (original image) produces the
5356
entire pyramid.
5457
- The procedure above was useful to downsample an image. What if we want to make it bigger?:
55-
columns filled with zeros (\f$0\f$)
58+
columns filled with zeros (\f$0 \f$)
5659
- First, upsize the image to twice the original in each dimension, wit the new even rows and
5760
- Perform a convolution with the same kernel shown above (multiplied by 4) to approximate the
5861
values of the "missing pixels"
5962
- These two procedures (downsampling and upsampling as explained above) are implemented by the
60-
OpenCV functions @ref cv::pyrUp and @ref cv::pyrDown , as we will see in an example with the
63+
OpenCV functions **pyrUp()** and **pyrDown()** , as we will see in an example with the
6164
code below:
6265

6366
@note When we reduce the size of an image, we are actually *losing* information of the image.
6467

6568
Code
6669
----
6770

68-
This tutorial code's is shown lines below. You can also download it from
69-
[here](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/ImgProc/Pyramids.cpp)
71+
This tutorial code's is shown lines below.
72+
73+
@add_toggle_cpp
74+
You can also download it from
75+
[here](https://raw.githubusercontent.com/opencv/opencv/master/samples/cpp/tutorial_code/ImgProc/Pyramids/Pyramids.cpp)
76+
@include samples/cpp/tutorial_code/ImgProc/Pyramids/Pyramids.cpp
77+
@end_toggle
78+
79+
@add_toggle_java
80+
You can also download it from
81+
[here](https://raw.githubusercontent.com/opencv/opencv/master/samples/java/tutorial_code/ImgProc/Pyramids/Pyramids.java)
82+
@include samples/java/tutorial_code/ImgProc/Pyramids/Pyramids.java
83+
@end_toggle
7084

71-
@include samples/cpp/tutorial_code/ImgProc/Pyramids.cpp
85+
@add_toggle_python
86+
You can also download it from
87+
[here](https://raw.githubusercontent.com/opencv/opencv/master/samples/python/tutorial_code/imgProc/Pyramids/pyramids.py)
88+
@include samples/python/tutorial_code/imgProc/Pyramids/pyramids.py
89+
@end_toggle
7290

7391
Explanation
7492
-----------
7593

7694
Let's check the general structure of the program:
7795

78-
- Load an image (in this case it is defined in the program, the user does not have to enter it
79-
as an argument)
80-
@snippet cpp/tutorial_code/ImgProc/Pyramids.cpp load
96+
#### Load an image
97+
98+
@add_toggle_cpp
99+
@snippet cpp/tutorial_code/ImgProc/Pyramids/Pyramids.cpp load
100+
@end_toggle
101+
102+
@add_toggle_java
103+
@snippet java/tutorial_code/ImgProc/Pyramids/Pyramids.java load
104+
@end_toggle
105+
106+
@add_toggle_python
107+
@snippet python/tutorial_code/imgProc/Pyramids/pyramids.py load
108+
@end_toggle
109+
110+
#### Create window
111+
112+
@add_toggle_cpp
113+
@snippet cpp/tutorial_code/ImgProc/Pyramids/Pyramids.cpp show_image
114+
@end_toggle
115+
116+
@add_toggle_java
117+
@snippet java/tutorial_code/ImgProc/Pyramids/Pyramids.java show_image
118+
@end_toggle
119+
120+
@add_toggle_python
121+
@snippet python/tutorial_code/imgProc/Pyramids/pyramids.py show_image
122+
@end_toggle
81123

82-
- Create a Mat object to store the result of the operations (*dst*) and one to save temporal
83-
results (*tmp*).
84-
@code{.cpp}
85-
Mat src, dst, tmp;
86-
/* ... */
87-
tmp = src;
88-
dst = tmp;
89-
@endcode
124+
#### Loop
90125

91-
- Create a window to display the result
92-
@snippet cpp/tutorial_code/ImgProc/Pyramids.cpp create_window
126+
@add_toggle_cpp
127+
@snippet cpp/tutorial_code/ImgProc/Pyramids/Pyramids.cpp loop
128+
@end_toggle
93129

94-
- Perform an infinite loop waiting for user input.
95-
@snippet cpp/tutorial_code/ImgProc/Pyramids.cpp infinite_loop
130+
@add_toggle_java
131+
@snippet java/tutorial_code/ImgProc/Pyramids/Pyramids.java loop
132+
@end_toggle
96133

97-
Our program exits if the user presses *ESC*. Besides, it has two options:
134+
@add_toggle_python
135+
@snippet python/tutorial_code/imgProc/Pyramids/pyramids.py loop
136+
@end_toggle
98137

99-
- **Perform upsampling (after pressing 'u')**
100-
@snippet cpp/tutorial_code/ImgProc/Pyramids.cpp pyrup
101-
We use the function @ref cv::pyrUp with three arguments:
138+
Perform an infinite loop waiting for user input.
139+
Our program exits if the user presses **ESC**. Besides, it has two options:
102140

103-
- *tmp*: The current image, it is initialized with the *src* original image.
104-
- *dst*: The destination image (to be shown on screen, supposedly the double of the
141+
- **Perform upsampling - Zoom 'i'n (after pressing 'i')**
142+
143+
We use the function **pyrUp()** with three arguments:
144+
- *src*: The current and destination image (to be shown on screen, supposedly the double of the
105145
input image)
106-
- *Size( tmp.cols*2, tmp.rows\*2 )\* : The destination size. Since we are upsampling,
107-
@ref cv::pyrUp expects a size double than the input image (in this case *tmp*).
108-
- **Perform downsampling (after pressing 'd')**
109-
@snippet cpp/tutorial_code/ImgProc/Pyramids.cpp pyrdown
110-
Similarly as with @ref cv::pyrUp , we use the function @ref cv::pyrDown with three arguments:
111-
112-
- *tmp*: The current image, it is initialized with the *src* original image.
113-
- *dst*: The destination image (to be shown on screen, supposedly half the input
114-
image)
115-
- *Size( tmp.cols/2, tmp.rows/2 )* : The destination size. Since we are upsampling,
116-
@ref cv::pyrDown expects half the size the input image (in this case *tmp*).
117-
- Notice that it is important that the input image can be divided by a factor of two (in
118-
both dimensions). Otherwise, an error will be shown.
119-
- Finally, we update the input image **tmp** with the current image displayed, so the
120-
subsequent operations are performed on it.
121-
@snippet cpp/tutorial_code/ImgProc/Pyramids.cpp update_tmp
146+
- *Size( tmp.cols*2, tmp.rows\*2 )* : The destination size. Since we are upsampling,
147+
**pyrUp()** expects a size double than the input image (in this case *src*).
148+
149+
@add_toggle_cpp
150+
@snippet cpp/tutorial_code/ImgProc/Pyramids/Pyramids.cpp pyrup
151+
@end_toggle
152+
153+
@add_toggle_java
154+
@snippet java/tutorial_code/ImgProc/Pyramids/Pyramids.java pyrup
155+
@end_toggle
156+
157+
@add_toggle_python
158+
@snippet python/tutorial_code/imgProc/Pyramids/pyramids.py pyrup
159+
@end_toggle
160+
161+
- **Perform downsampling - Zoom 'o'ut (after pressing 'o')**
162+
163+
We use the function **pyrDown()** with three arguments (similarly to **pyrUp()**):
164+
- *src*: The current and destination image (to be shown on screen, supposedly half the input
165+
image)
166+
- *Size( tmp.cols/2, tmp.rows/2 )* : The destination size. Since we are upsampling,
167+
**pyrDown()** expects half the size the input image (in this case *src*).
168+
169+
@add_toggle_cpp
170+
@snippet cpp/tutorial_code/ImgProc/Pyramids/Pyramids.cpp pyrdown
171+
@end_toggle
172+
173+
@add_toggle_java
174+
@snippet java/tutorial_code/ImgProc/Pyramids/Pyramids.java pyrdown
175+
@end_toggle
176+
177+
@add_toggle_python
178+
@snippet python/tutorial_code/imgProc/Pyramids/pyramids.py pyrdown
179+
@end_toggle
180+
181+
Notice that it is important that the input image can be divided by a factor of two (in both dimensions).
182+
Otherwise, an error will be shown.
122183

123184
Results
124185
-------
125186

126-
- After compiling the code above we can test it. The program calls an image **chicky_512.jpg**
127-
that comes in the *samples/data* folder. Notice that this image is \f$512 \times 512\f$,
187+
- The program calls by default an image [chicky_512.png](https://raw.githubusercontent.com/opencv/opencv/master/samples/data/chicky_512.png)
188+
that comes in the `samples/data` folder. Notice that this image is \f$512 \times 512\f$,
128189
hence a downsample won't generate any error (\f$512 = 2^{9}\f$). The original image is shown below:
129190

130191
![](images/Pyramids_Tutorial_Original_Image.jpg)
131192

132-
- First we apply two successive @ref cv::pyrDown operations by pressing 'd'. Our output is:
193+
- First we apply two successive **pyrDown()** operations by pressing 'd'. Our output is:
133194

134195
![](images/Pyramids_Tutorial_PyrDown_Result.jpg)
135196

136197
- Note that we should have lost some resolution due to the fact that we are diminishing the size
137-
of the image. This is evident after we apply @ref cv::pyrUp twice (by pressing 'u'). Our output
198+
of the image. This is evident after we apply **pyrUp()** twice (by pressing 'u'). Our output
138199
is now:
139200

140201
![](images/Pyramids_Tutorial_PyrUp_Result.jpg)

doc/tutorials/imgproc/table_of_content_imgproc.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ In this section you will learn about the image processing (manipulation) functio
5151

5252
- @subpage tutorial_pyramids
5353

54+
*Languages:* C++, Java, Python
55+
5456
*Compatibility:* \> OpenCV 2.0
5557

5658
*Author:* Ana Huamán

samples/cpp/tutorial_code/ImgProc/Pyramids.cpp

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @file Pyramids.cpp
3+
* @brief Sample code of image pyramids (pyrDown and pyrUp)
4+
* @author OpenCV team
5+
*/
6+
7+
#include "iostream"
8+
#include "opencv2/imgproc.hpp"
9+
#include "opencv2/imgcodecs.hpp"
10+
#include "opencv2/highgui.hpp"
11+
12+
using namespace std;
13+
using namespace cv;
14+
15+
const char* window_name = "Pyramids Demo";
16+
17+
/**
18+
* @function main
19+
*/
20+
int main( int argc, char** argv )
21+
{
22+
/// General instructions
23+
cout << "\n Zoom In-Out demo \n "
24+
"------------------ \n"
25+
" * [i] -> Zoom in \n"
26+
" * [o] -> Zoom out \n"
27+
" * [ESC] -> Close program \n" << endl;
28+
29+
//![load]
30+
const char* filename = argc >=2 ? argv[1] : "../data/chicky_512.png";
31+
32+
// Loads an image
33+
Mat src = imread( filename );
34+
35+
// Check if image is loaded fine
36+
if(src.empty()){
37+
printf(" Error opening image\n");
38+
printf(" Program Arguments: [image_name -- default ../data/chicky_512.png] \n");
39+
return -1;
40+
}
41+
//![load]
42+
43+
//![loop]
44+
for(;;)
45+
{
46+
//![show_image]
47+
imshow( window_name, src );
48+
//![show_image]
49+
char c = (char)waitKey(0);
50+
51+
if( c == 27 )
52+
{ break; }
53+
//![pyrup]
54+
else if( c == 'i' )
55+
{ pyrUp( src, src, Size( src.cols*2, src.rows*2 ) );
56+
printf( "** Zoom In: Image x 2 \n" );
57+
}
58+
//![pyrup]
59+
//![pyrdown]
60+
else if( c == 'o' )
61+
{ pyrDown( src, src, Size( src.cols/2, src.rows/2 ) );
62+
printf( "** Zoom Out: Image / 2 \n" );
63+
}
64+
//![pyrdown]
65+
}
66+
//![loop]
67+
68+
return 0;
69+
}

0 commit comments

Comments
 (0)