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

Goal

In this chapter,

Theory

Normally, we used to work with an image of constant size. But in some occassions, we need to work with images of different resolution of the same image. For example, while searching for something in an image, like face, we are not sure at what size the object will be present in the image. In that case, we will need to create a set of images with different resolution and search for object in all the images. These set of images with different resolution are called Image Pyramids (because when they are kept in a stack with biggest image at bottom and smallest image at top look like a pyramid).

There are two kinds of Image Pyramids. 1) Gaussian Pyramid and 2) Laplacian Pyramids

Higher level (Low resolution) in a Gaussian Pyramid is formed by removing consecutive rows and columns in Lower level (higher resolution) image. Then each pixel in higher level is formed by the contribution from 5 pixels in underlying level with gaussian weights. By doing so, a \(M \times N\) image becomes \(M/2 \times N/2\) image. So area reduces to one-fourth of original area. It is called an Octave. The same pattern continues as we go upper in pyramid (ie, resolution decreases). Similarly while expanding, area becomes 4 times in each level. We can find Gaussian pyramids using cv2.pyrDown() and cv2.pyrUp() functions.

1 img = cv2.imread('messi5.jpg')
2 lower_reso = cv2.pyrDown(higher_reso)

Below is the 4 levels in an image pyramid.

messipyr.jpg
image

Now you can go down the image pyramid with cv2.pyrUp() function.

1 higher_reso2 = cv2.pyrUp(lower_reso)

Remember, higher_reso2 is not equal to higher_reso, because once you decrease the resolution, you loose the information. Below image is 3 level down the pyramid created from smallest image in previous case. Compare it with original image:

messiup.jpg
image

Laplacian Pyramids are formed from the Gaussian Pyramids. There is no exclusive function for that. Laplacian pyramid images are like edge images only. Most of its elements are zeros. They are used in image compression. A level in Laplacian Pyramid is formed by the difference between that level in Gaussian Pyramid and expanded version of its upper level in Gaussian Pyramid. The three levels of a Laplacian level will look like below (contrast is adjusted to enhance the contents):

lap.jpg
image

Image Blending using Pyramids

One application of Pyramids is Image Blending. For example, in image stitching, you will need to stack two images together, but it may not look good due to discontinuities between images. In that case, image blending with Pyramids gives you seamless blending without leaving much data in the images. One classical example of this is the blending of two fruits, Orange and Apple. See the result now itself to understand what I am saying:

orapple.jpg
image

Please check first reference in additional resources, it has full diagramatic details on image blending, Laplacian Pyramids etc. Simply it is done as follows:

  1. Load the two images of apple and orange
  2. Find the Gaussian Pyramids for apple and orange (in this particular example, number of levels is 6)
  3. From Gaussian Pyramids, find their Laplacian Pyramids
  4. Now join the left half of apple and right half of orange in each levels of Laplacian Pyramids
  5. Finally from this joint image pyramids, reconstruct the original image.

Below is the full code. (For sake of simplicity, each step is done separately which may take more memory. You can optimize it if you want so).

1 import cv2
2 import numpy as np,sys
3 
4 A = cv2.imread('apple.jpg')
5 B = cv2.imread('orange.jpg')
6 
7 # generate Gaussian pyramid for A
8 G = A.copy()
9 gpA = [G]
10 for i in xrange(6):
11  G = cv2.pyrDown(G)
12  gpA.append(G)
13 
14 # generate Gaussian pyramid for B
15 G = B.copy()
16 gpB = [G]
17 for i in xrange(6):
18  G = cv2.pyrDown(G)
19  gpB.append(G)
20 
21 # generate Laplacian Pyramid for A
22 lpA = [gpA[5]]
23 for i in xrange(5,0,-1):
24  GE = cv2.pyrUp(gpA[i])
25  L = cv2.subtract(gpA[i-1],GE)
26  lpA.append(L)
27 
28 # generate Laplacian Pyramid for B
29 lpB = [gpB[5]]
30 for i in xrange(5,0,-1):
31  GE = cv2.pyrUp(gpB[i])
32  L = cv2.subtract(gpB[i-1],GE)
33  lpB.append(L)
34 
35 # Now add left and right halves of images in each level
36 LS = []
37 for la,lb in zip(lpA,lpB):
38  rows,cols,dpt = la.shape
39  ls = np.hstack((la[:,0:cols/2], lb[:,cols/2:]))
40  LS.append(ls)
41 
42 # now reconstruct
43 ls_ = LS[0]
44 for i in xrange(1,6):
45  ls_ = cv2.pyrUp(ls_)
46  ls_ = cv2.add(ls_, LS[i])
47 
48 # image with direct connecting each half
49 real = np.hstack((A[:,:cols/2],B[:,cols/2:]))
50 
51 cv2.imwrite('Pyramid_blending2.jpg',ls_)
52 cv2.imwrite('Direct_blending.jpg',real)

Additional Resources

  1. Image Blending

Exercises