Opencv Python 简明教程
OpenCV Python - Image Pyramids
有时,我们可能需要将图像转换为不同于其原始大小的大小。为此,您可以将图像放大(放大)或缩小(缩小)。
Occasionally, we may need to convert an image to a size different than its original. For this, you either Upsize the image (zoom in) or Downsize it (zoom out).
图像金字塔是一组图像(由单一原始图像构建),连续向下采样指定次数。
An image pyramid is a collection of images (constructed from a single original image) successively down sampled a specified number of times.
高斯金字塔用于对图像进行下采样,而拉普拉斯金字塔则使用分辨率较低的图像金字塔中的一张图像重建一张上采样的图像。
The Gaussian pyramid is used to down sample images while the Laplacian pyramid reconstructs an up sampled image from an image lower in the pyramid with less resolution.
将金字塔视为一组图层。图像如下所示 −
Consider the pyramid as a set of layers. The image is shown below −
金字塔较高层中的图像尺寸较小。要制作高斯金字塔中下一层的图像,我们将较低水平的图像与高斯核进行卷积。
Image at the higher layer of the pyramid is smaller in size. To produce an image at the next layer in the Gaussian pyramid, we convolve a lower level image with a Gaussian kernel.
\frac{1}{16}\begin{bmatrix}1 & 4 & 6 & 4 & 1 \\4 & 16 & 24 & 16 & 4 \\6 & 24 & 36 & 24 & 6 \\4 & 16 & 24 & 16 & 4 \\1 & 4 & 6 & 4 & 1\end{bmatrix}
现在移除所有偶数行的行和列。得到的图像将是其前身面积的 1/4。对原始图像进行此过程的迭代就会产生整个金字塔。
Now remove every even-numbered row and column. Resulting image will be 1/4th the area of its predecessor. Iterating this process on the original image produces the entire pyramid.
为了使图像变大,需要使用零填充列。首先,将图像放大至每个维度都为原来的两倍,获得新的偶数行,然后使用核进行卷积以近似缺失像素的值。
To make the images bigger, the columns filled with zeros. First, upsize the image to double the original in each dimension, with the new even rows and then perform a convolution with the kernel to approximate the values of the missing pixels.
cv.pyrUp() 函数会使原始尺寸加倍; cv.pyrDown() 函数会使原始尺寸减半。
The cv.pyrUp() function doubles the original size and cv.pyrDown() function decreases it to half.
Example
以下程序根据用户输入的“I”或“o”分别调用 pyrUp() 和 pyrDown() 函数。
Following program calls pyrUp() and pyrDown() functions depending on user input “I” or “o” respectively.
请注意,当我们减小图像尺寸时,图像的信息就会丢失。一旦缩小尺寸,再重新缩放回原始尺寸时,我们将丢失部分信息,且新图像的分辨率远低于原始图像。
Note that when we reduce the size of an image, information of the image is lost. Once, we scale down and if we rescale it to the original size, we lose some information and the resolution of the new image is much lower than the original one.
import sys
import cv2 as cv
filename = 'chicky_512.png'
src = cv.imread(filename)
while 1:
print ("press 'i' for zoom in 'o' for zoom out esc to stop")
rows, cols, _channels = map(int, src.shape)
cv.imshow('Pyramids', src)
k = cv.waitKey(0)
if k == 27:
break
elif chr(k) == 'i':
src = cv.pyrUp(src, dstsize=(2 * cols, 2 * rows))
elif chr(k) == 'o':
src = cv.pyrDown(src, dstsize=(cols // 2, rows // 2))
cv.destroyAllWindows()