OpenCV 11 (Image Pyramid)

1. Image Pyramid

**Image Pyramid** is a kind of multi-scale expression in images . It is mainly used for image segmentation. It is an effective but conceptually simple structure to explain images at multiple resolutions. Simply put, the image pyramid is a collection of sub-images of the same image with different resolutions.

Image pyramids were originally used in machine vision and image compression. It is obtained through ladder down sampling, and the sampling is not stopped until a certain termination condition is reached. The base of the pyramid is a high-resolution representation of the image to be processed, while the top is a low-resolution approximation. We compare images layer by layer to a pyramid. The higher the level, the smaller the image and the lower the resolution .

**Two common types of image pyramids**

**Gaussian pyramid**: used for downsampling/downsampling, the main image pyramid
**Laplacian pyramid**: used to reconstruct the upper unsampled image from the lower level image of the pyramid, in In digital image processing, it is also the prediction residual, which can restore the image to the greatest extent and is used together with the Gaussian pyramid. 

1.1 Gaussian Pyramid

**Gaussian Pyramid** is a series of downsampled images obtained through Gaussian smoothing and subsampling.

The principle is very simple, as shown in the figure below:

Original image M * N -> processed image M/2 * N/2.

After each processing, the resulting image is 1/4 of the original.

  • Image convolution with convolution kernel -- Gaussian smoothing
  • Remove all even-numbered rows and columns -- reduce the image to 1/4 of the original size 

Note: Downsampling will lose image information.

- pyrDown downsampling

import cv2
import numpy as np


img = cv2.imread('./lena.png')

print(img.shape)
dst = cv2.pyrDown(img)

print(dst.shape)

cv2.imshow('img', img)
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

- pyrUp upsampling

  Upsampling is the opposite process of downsampling, which refers to the process of images growing from small to large .

 

 # 向上采样
  # 向下采样
  import cv2
  import numpy as np
  
  
  img = cv2.imread('./lena.png')
  
  print(img.shape)
  dst = cv2.pyrUp(img)
  
  print(dst.shape)
  
  cv2.imshow('img', img)
  cv2.imshow('dst', dst)
  cv2.waitKey(0)
  cv2.destroyAllWindows()

- Sampling reversibility study

​ Based on the principles of up and down sampling, we can find that information is lost in the process of the image becoming larger and smaller. Even if the image is changed back to its original size, the image is not the original image, but a certain amount of information has been lost. Information.

# 研究采样中图像的损失
import cv2
import numpy as np


img = cv2.imread('./lena.png')

# 先放大, 再缩小
dst = cv2.pyrUp(img)
dst = cv2.pyrDown(dst)

cv2.imshow('img', img)
cv2.imshow('dst', dst)
cv2.imshow('loss', img - dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

1.2 Laplace’s Pyramid

The downsampled image is then upsampled, and then the difference is made with the original image that has not been downsampled before to obtain the residual image (to prepare information for restoring the image) !

That is, the Laplacian pyramid is constructed by subtracting a series of images from a source image that is first reduced and then enlarged. What is retained is the residual!

The residual is the lost information.

The Laplacian pyramid is a Gaussian pyramid without a dedicated function.

The Laplacian Pyramid image only looks like the edges of the image, most of its elements are 0, used for image compression.

 

# 研究采样中图像的损失
import cv2
import numpy as np


img = cv2.imread('./lena.png')

dst = cv2.pyrDown(img)
dst = cv2.pyrUp(dst)

lap0 = img - dst
cv2.imshow('img', img)
cv2.imshow('dst', dst)
cv2.imshow('lap0', lap0)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 研究采样中图像的损失
import cv2
import numpy as np


img = cv2.imread('./lena.png')

dst = cv2.pyrDown(img)
dst = cv2.pyrUp(dst)

lap0 = img - dst
cv2.imshow('img', img)
cv2.imshow('dst', dst)
cv2.imshow('lap0', lap0)


第二层拉普拉斯效果:
dst1 = cv2.pyrDown(dst)
dst2 = cv2.pyrUp(dst1)
lap1 = dst - dst2 
cv2.imshow('lap1 ', lap1)

cv2.waitKey(0)
cv2.destroyAllWindows()

 

Guess you like

Origin blog.csdn.net/peng_258/article/details/132779070