OpenCV  3.0.0-rc1
Open Source Computer Vision
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Image Pyramids

Goal

In this tutorial you will learn how to:

Theory

Note
The explanation below belongs to the book Learning OpenCV by Bradski and Kaehler.

Image Pyramid

Gaussian Pyramid

Note
When we reduce the size of an image, we are actually losing information of the image.

Code

This tutorial code's is shown lines below. You can also download it from here

1 
8 #include "opencv2/imgcodecs.hpp"
10 #include <math.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 
14 using namespace cv;
15 
17 Mat src, dst, tmp;
18 
19 const char* window_name = "Pyramids Demo";
20 
21 
25 int main( void )
26 {
28  printf( "\n Zoom In-Out demo \n " );
29  printf( "------------------ \n" );
30  printf( " * [u] -> Zoom in \n" );
31  printf( " * [d] -> Zoom out \n" );
32  printf( " * [ESC] -> Close program \n \n" );
33 
35  src = imread( "../data/chicky_512.png" );
36  if( src.empty() )
37  { printf(" No data! -- Exiting the program \n");
38  return -1; }
39 
40  tmp = src;
41  dst = tmp;
42 
44  namedWindow( window_name, WINDOW_AUTOSIZE );
45  imshow( window_name, dst );
46 
48  for(;;)
49  {
50  int c;
51  c = waitKey(10);
52 
53  if( (char)c == 27 )
54  { break; }
55  if( (char)c == 'u' )
56  { pyrUp( tmp, dst, Size( tmp.cols*2, tmp.rows*2 ) );
57  printf( "** Zoom In: Image x 2 \n" );
58  }
59  else if( (char)c == 'd' )
60  { pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 ) );
61  printf( "** Zoom Out: Image / 2 \n" );
62  }
63 
64  imshow( window_name, dst );
65  tmp = dst;
66  }
67 
68  return 0;
69 }
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
Size2i Size
Definition: types.hpp:308
void pyrDown(InputArray src, OutputArray dst, const Size &dstsize=Size(), int borderType=BORDER_DEFAULT)
Blurs an image and downsamples it.
Definition: highgui.hpp:138
int main(int argc, const char *argv[])
Definition: facerec_demo.cpp:67
n-dimensional dense array class
Definition: mat.hpp:726
void pyrUp(InputArray src, OutputArray dst, const Size &dstsize=Size(), int borderType=BORDER_DEFAULT)
Upsamples an image and then blurs it.
int waitKey(int delay=0)
Waits for a pressed key.

Explanation

Let's check the general structure of the program:

Results