Digital image processing (21): image pyramid (Gaussian pyramid and Laplacian pyramids)

Disclaimer: This article is a blogger original article, reproduced, please indicate the link. https://blog.csdn.net/zaishuiyifangxym/article/details/90167880

table of Contents

1 Introduction image pyramid

2 down-sampling --pyrDown ()

2.1 Basic Theory

Code Example 2.2

3 upsampled --pyrUp ()

3.1 Basic Theory

Code Example 3.2

4 Laplacian pyramid

4.1 Basic Theory

Code Example 4.2

Reference material


 

1 Introduction image pyramid

A plurality of image resolution shows a concept of an effective and simple structure is an image pyramid . Pyramid image initially used for machine vision and image compression, an image pyramid is a series of gold arranged in the shape, the resolution gradually reduce the set of images.

As shown at FIG., It includes four images, these layers of the image pyramid metaphor. The image pyramid can be obtained by Echelon sampling down until it reaches a termination condition stopped sampling, sampling down, the higher the level, the smaller the image, the lower the resolution.

 

Generating an image pyramid includes two main ways: downsampling and upsampling .

As shown below:

Down-sampling: the image from G0 to G1, G2, G3, increasingly lower image resolution process;

Upsampling: the image from G3 to G2, G1, G0, image resolution increasing process.

It will be introduced in turn down-sampling  and  upsampling .

 


Down-sampling 2 - pyrDown ()

2.1 Basic Theory

In the down-sampled image, in general, in two steps:

(1) image Gi Gaussian convolution kernel ( Gaussian filtering );

(2) Remove all the even-numbered rows and columns.

 

Wherein Gaussian kernel convolution ( Gaussian filtering ) for the whole image is a weighted average process, each pixel value of the pixel values by its own and other neighborhood (different weights) after the weighted average. Common 3 \times5 3 and \times5 Gaussian kernel as follows:

                                                                              K(3,3)=\frac{1}{16}\times \left[ \begin{matrix} 1 & 2 & 1 \\ 2 & 4 & 2 \\ 1 & 2 & 1 \\ \end{matrix} \right]

                                                                     K(5,5)=\frac{1}{273}\times \left[ \begin{matrix} 1 & 4 & 7 & 4 & 1 \\ 4 & 16 & 26 & 16 & 4 \\ 7 & 26 & 41 & 26 & 7 \\ 4 & 16 & 26 & 16 & 4 \\ 1 & 4 & 7 & 4 & 1 \\ \end{matrix} \right]

Gaussian filtering for the detailed procedure, reference may blog: image smoothing (mean filter, median filter and Gaussian filtering)

 

As shown below, after down-sampling, the original image  G_i having M \timesafter N pixels, for down-sampling, the resulting image  G_i+1 having M / 2  \times N / 2 pixels, only one quarter of the original. Through the input of the original image kept iterative steps above you will get the whole pyramid.

Note : Because each even-numbered down sampling will delete rows and columns, so it will not stop the loss of image information.

 

The following is a sampling of the image down, you can see the image shrinking.

 

 

Code Example 2.2

In OpenCV, the down-sampling function used is pyrDown () , a function of usage is as follows:

dst = pyrDown(src[, dst[, dstsize[, borderType]]])

Among them, the parameters:

src denotes input image;

dst indicates an output image and the input image having the same size and type;

dstsize size of the output image, the default value Size ();

borderType represents pixels outside the push method, see cv :: bordertypes.

 

(1) a downsampling

Code as follows:

# -*- coding: utf-8 -*-
import cv2
import numpy as np
import matplotlib.pyplot as plt

#读取原始图像
img = cv2.imread('zxp.jpg')

#图像向下取样
r = cv2.pyrDown(img)

#显示图像
cv2.imshow('original', img)
cv2.imshow('PyrDown', r)

cv2.waitKey()
cv2.destroyAllWindows()

 

Operating results as shown below:

 

(2) a plurality of times down-sampling

Code as follows:

# -*- coding: utf-8 -*-
import cv2
import numpy as np
import matplotlib.pyplot as plt

#读取原始图像
img = cv2.imread('zxp.jpg')

#图像向下取样
r1 = cv2.pyrDown(img)
r2 = cv2.pyrDown(r1)
r3 = cv2.pyrDown(r2)

#显示图像
cv2.imshow('original', img)
cv2.imshow('PyrDown1', r1)
cv2.imshow('PyrDown2', r2)
cv2.imshow('PyrDown3', r3)

cv2.waitKey()
cv2.destroyAllWindows()

 

Operating results as shown below:

 


3 upsampled --pyrUp ()

3.1 Basic Theory

在图像向上取样是由小图像不断放图像的过程。它将图像在每个方向上扩大为原图像的2倍,新增的行和列均用0来填充,并使用与“向下取样”相同的卷积核乘以4,再与放大后的图像进行卷积运算,以获得“新增像素”的新值。

 

如图下图所示,它在原始像素45、123、89、149之间各新增了一行和一列值为0的像素。

 

如下图所示,为图像的向上采样和向下采样的例子。

注:向上取样和向下取样无法互逆的。

 

 

3.2 代码示例

在OpenCV中,向上取样使用 pyrUp() 函数,其函数用法如下所示:

dst = pyrUp(src[, dst[, dstsize[, borderType]]])

其中,参数:

src 表示输入图像;

dst 表示输出图像,和输入图像具有一样的尺寸和类型;

dstsize 表示输出图像的大小,默认值为Size();

borderType 表示像素外推方法,详见cv::bordertypes 。

 

代码如下所示:

# -*- coding: utf-8 -*-
import cv2
import numpy as np
import matplotlib.pyplot as plt

#读取原始图像
img = cv2.imread('zxp_PyrDown2.jpg')

#图像向上取样
r = cv2.pyrUp(img)

#显示图像
cv2.imshow('original', img)
cv2.imshow('PyrUp', r)
cv2.waitKey()
cv2.destroyAllWindows()

 

运行结果如下图所示:

 


4 Laplacian 金字塔

4.1 基础理论

前面提到的均是高斯金字塔(使用高斯核),下面介绍拉普拉斯(Laplacian) 金字塔拉普拉斯(Laplacian) 金字塔是在高斯金字塔的基础上新的金字塔。

如下图所示,拉普拉斯(Laplacian) 金字塔的表达式:

 

拉普拉斯每一层表示如下图所示:

 

下图高斯金字塔和拉普拉斯金字塔 交叉使用得到不同的图像。

 

 

 

4.2 代码示例

 

(1) 拉普拉斯第0层

代码如下所示:

# -*- coding: utf-8 -*-
import cv2
import numpy as np
import matplotlib.pyplot as plt

#读取原始图像
img = cv2.imread('lena.tiff')

#图像向下取样
r1 = cv2.pyrDown(img)

#图像向上取样
r2 = cv2.pyrUp(r1)

# 拉普拉斯第0层
LapPyr0 =img-r2



#显示图像
cv2.imshow('original', img)
cv2.imshow('LapPyr', LapPyr0)
cv2.waitKey()
cv2.destroyAllWindows()

 

运行结果如下图所示:

 

 

(2) 拉普拉斯第0层和拉普拉斯第1层

代码如下所示:

# -*- coding: utf-8 -*-
import cv2
import numpy as np
import matplotlib.pyplot as plt

#读取原始图像
img = cv2.imread('lena.tiff')

#图像向下取样
r1 = cv2.pyrDown(img)

#图像向上取样
r2 = cv2.pyrUp(r1)

# 拉普拉斯第0层
LapPyr0 =img-r2



#图像向下取样
r3 = cv2.pyrDown(r1)

#图像向上取样
r4 = cv2.pyrUp(r3)

# 拉普拉斯第1层
LapPyr1 =r1-r4

#显示图像
cv2.imshow('original', img)
cv2.imshow('LapPyr0', LapPyr0)
cv2.imshow('LapPyr1', LapPyr1)
cv2.waitKey()
cv2.destroyAllWindows()

 

运行结果如下图所示:

 


参考资料

[1] https://blog.csdn.net/Eastmount/article/details/89341077

[2] Python+OpenCV图像处理

[3] 冈萨雷斯. 数字图像处理(第三版) 

Guess you like

Origin blog.csdn.net/zaishuiyifangxym/article/details/90167880