|
1 | 1 | Image Pyramids {#tutorial_pyramids}
|
2 | 2 | ==============
|
3 | 3 |
|
| 4 | +@prev_tutorial{tutorial_morph_lines_detection} |
| 5 | +@next_tutorial{tutorial_threshold} |
| 6 | + |
4 | 7 | Goal
|
5 | 8 | ----
|
6 | 9 |
|
7 | 10 | In this tutorial you will learn how to:
|
8 | 11 |
|
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 |
10 | 13 | image.
|
11 | 14 |
|
12 | 15 | Theory
|
|
19 | 22 | -# *Upsize* the image (zoom in) or
|
20 | 23 | -# *Downsize* it (zoom out).
|
21 | 24 | - 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 |
23 | 26 | first the use of **Image Pyramids**, which are widely applied in a huge range of vision
|
24 | 27 | applications.
|
25 | 28 |
|
@@ -52,89 +55,147 @@ Theory
|
52 | 55 | predecessor. Iterating this process on the input image \f$G_{0}\f$ (original image) produces the
|
53 | 56 | entire pyramid.
|
54 | 57 | - 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$) |
56 | 59 | - First, upsize the image to twice the original in each dimension, wit the new even rows and
|
57 | 60 | - Perform a convolution with the same kernel shown above (multiplied by 4) to approximate the
|
58 | 61 | values of the "missing pixels"
|
59 | 62 | - 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 |
61 | 64 | code below:
|
62 | 65 |
|
63 | 66 | @note When we reduce the size of an image, we are actually *losing* information of the image.
|
64 | 67 |
|
65 | 68 | Code
|
66 | 69 | ----
|
67 | 70 |
|
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 |
70 | 84 |
|
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 |
72 | 90 |
|
73 | 91 | Explanation
|
74 | 92 | -----------
|
75 | 93 |
|
76 | 94 | Let's check the general structure of the program:
|
77 | 95 |
|
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 |
81 | 123 |
|
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 |
90 | 125 |
|
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 |
93 | 129 |
|
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 |
96 | 133 |
|
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 |
98 | 137 |
|
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: |
102 | 140 |
|
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 |
105 | 145 | 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. |
122 | 183 |
|
123 | 184 | Results
|
124 | 185 | -------
|
125 | 186 |
|
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$, |
128 | 189 | hence a downsample won't generate any error (\f$512 = 2^{9}\f$). The original image is shown below:
|
129 | 190 |
|
130 | 191 | 
|
131 | 192 |
|
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: |
133 | 194 |
|
134 | 195 | 
|
135 | 196 |
|
136 | 197 | - 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 |
138 | 199 | is now:
|
139 | 200 |
|
140 | 201 | 
|
0 commit comments