OpenCV-Python官方文档中文翻译20:Image Pyramids

Image Pyramids

Goal

In this chapter,

  • We will learn about Image Pyramids
  • We will use Image pyramids to create a new fruit, “Orapple”
  • We will see these functions: cv.pyrUp(), cv.pyrDown()
  • 关于图像金字塔
  • 使用图像金字塔创建一个新的水果"Orapple"
  • 函数 cv.pyrUp(),cv.pyrDown()

Theory

Normally, we used to work with an image of constant size. But on some occasions, we need to work with (the same) images in different resolution. For example, while searching for something in an image, like face, we are not sure at what size the object will be present in said image. In that case, we will need to create a set of the same image with different resolutions and search for object in all of them. These set of images with different resolutions are called Image Pyramids (because when they are kept in a stack with the highest resolution image at the bottom and the lowest resolution image at top, it looks 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×N image becomes M/2×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 cv.pyrDown() and cv.pyrUp() functions.

通常我们用恒定尺寸的图片。但是在有的场景下我们需要处理不同分辨率的图像。例如在图片搜寻某物的时候不确定物体的尺寸。在那种情况,我们需要创建一组不同分辨率的同样照片,在它们之中寻找物体。这一组不同分辨率的图片就叫做图片金字塔(因为它们堆叠起来最高分辨率的在底部,最高分辨率的在顶部,看起来就像金字塔一样)。

两类:1.高斯金字塔。2.拉普拉斯金字塔

高斯金字塔高层是通过删除低层的连续行和列来组成的。每个高层的像素由低层的5个像素高斯加权而成。这样做使MxN的图片变为了M/2xN/2。面积减少到四分之一。一层一层往上继续。用函数cv.pyrDown()和cv.pyrUp()找到高斯金字塔。

img = cv.imread("messi5.jpg")
lower_reso = cv.pyrDown(higher_reso)

Below is the 4 levels in an image pyramid.

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-x5AZ01mj-1614271372854)(https://docs.opencv.org/4.5.1/messipyr.jpg)]

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

higher_reso2 = cv.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:

一旦降低分辨率,你就失去了信息。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EU86BTEw-1614271372859)(https://docs.opencv.org/4.5.1/messiup.jpg)]

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):

拉普拉斯金字塔由高斯金字塔组成。没有专用函数。拉普拉斯金字塔仅像边缘图像。大多数元素为0。用于图像压缩。拉普拉斯金字塔的一层是由相对应的高斯金字塔的那一层和扩展版本的高斯金字塔的高层的差组成的。拉普拉斯的三层如下所示(调整对比度来加强内容):

img

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:

在图像拼接中,你需要将两个图像堆叠在一起,但是由于图像间的不连续可能看起来不太好。在这种情况下使用金字塔可以无缝融合。经典例子:融合橘子和苹果。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CqTRTp2n-1614271372867)(https://docs.opencv.org/4.5.1/orapple.jpg)]

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).

完整代码(为了简单,每步独立进行,可自行优化)

import cv2 as cv
import numpy as np,sys
A = cv.imread('apple.jpg')
B = cv.imread('orange.jpg')
#给A 生成高斯金字塔
G = A.copy()
gpA = [G]
for i in xrange(6):
	G = cv.pyrDown(G)
	gpA.append(G)
#为B 生成高斯金字塔
G = B.copy()
gpB = [G]
for i in xrange(6)
	G = cv.pyrDown(G)
	 gpB.append(G)
#给A生成拉普拉斯金字塔
lpA = [gpA[5]]
for i in xrange(5,0,-1):
	GE = cv.pyrUp(gpA[i])
	L = cv.subtract(gpA[i-1],GE)
	lpA.append(L)
# generate Laplacian Pyramid for B
lpB = [gpB[5]]
for i in xrange(5,0,-1):
	GE = cv.pyrUp(gpB[i])
	L = cv.subtract(gpB[i-1],GE)
	lpB.append(L)
# Now add left and right halves of images in each level

LS = []

for la,lb in zip(lpA,lpB):

    rows,cols,dpt = la.shape
    ls = np.hstack((la[:,0:cols/2], lb[:,cols/2:]))

	LS.append(ls)
# now reconstruct

ls_ = LS[0]

for i in xrange(1,6):
	ls_ = cv.pyrUp(ls_)
	ls_ = cv.add(ls_,LS[i])
#image with direct connecting each half
real = np.hstack((A[:,:cols/2],B[:,cols/2:]))
cv.imwrite("Pyramid_blending2.jpg",ls_)
cv.imwrite('Direct_blending.jpg',real)
	

Additional Resources

  1. Image Blending

Exercises

### 回答1: 抱歉,我是一名语言模型,不具备展示示例的功能。但是,我可以提供一些相关的资料供您参考。关于 Python 对图像进行高斯模糊再利用反卷积算法进行无损还原的示例,您可以参考以下资料: 1. OpenCV 中的图像恢复:https://docs.opencv.org/master/de/d67/tutorial_py_pyramids.html 2. 图像去模糊——Matt McCormick 博客:https://blog.kitware.com/deblurring-images-with-opencv/ 3. 用 Python 实现的反卷积:https://stackoverflow.com/questions/30132339/python-opencv-deconvolution 希望这些资料可以对您有所帮助! ### 回答2: 高斯模糊和反卷积算法是数字图像处理中常用的技术。下面是一个使用Python进行图像高斯模糊和反卷积的无损还原示例。 首先,我们需要导入相关的库和模块。我们可以使用OpenCV库来加载和处理图像,使用SciPy库中的ndimage模块来进行高斯模糊,使用scikit-image库中的restoration模块来进行反卷积。 ``` import cv2 from scipy import ndimage from skimage import restoration ``` 接下来,我们加载原始图像并显示出来。 ``` image = cv2.imread('input.jpg') cv2.imshow("Original Image", image) cv2.waitKey(0) ``` 然后,我们使用ndimage模块中的gaussian_filter函数来对图像进行高斯模糊处理。这可以模拟图像的模糊效果。 ``` blurred_image = ndimage.gaussian_filter(image, sigma=3) cv2.imshow("Blurred Image", blurred_image) cv2.waitKey(0) ``` 现在,我们将使用restoration模块中的richardson_lucy函数进行反卷积处理。这个函数可以尝试恢复原始图像。 ``` deblurred_image = restoration.richardson_lucy(blurred_image, image.ndim * 2) cv2.imshow("Deblurred Image", deblurred_image) cv2.waitKey(0) ``` 最后,我们保存结果并展示。 ``` cv2.imwrite("output.jpg", deblurred_image) cv2.destroyAllWindows() ``` 通过运行上述代码,我们可以将原始图像进行高斯模糊处理,然后再利用反卷积算法进行无损还原。这样可以恢复原始图像的一部分细节,提高图像质量。 ### 回答3: Python中可以使用OpenCV库对图像进行高斯模糊和无损还原操作。 首先,我们导入OpenCV库并读取原始图像。然后,我们使用高斯模糊函数`cv2.GaussianBlur()`来对图像进行模糊处理。该函数需要指定模糊半径和模糊的标准差。 接下来,我们可以将模糊处理后的图像用于反卷积算法。在OpenCV中,可以使用`cv2.dft()`函数进行图像的傅里叶变换,并将其转换为复数数组。然后,我们可以应用反卷积算法来对模糊图像进行无损还原。 最后,我们可以通过逆傅里叶变换将无损还原的图像转回到空域,并将其显示出来。 以下是一个示例代码: ```python import cv2 # 读取原始图像 image = cv2.imread('image.jpg') # 高斯模糊处理 blur_image = cv2.GaussianBlur(image, (0, 0), 3) # 反卷积算法 dft = cv2.dft(np.float32(blur_image), flags=cv2.DFT_COMPLEX_OUTPUT) restored_dft = np.zeros_like(dft) restored_dft[:, :, 0] = dft[:, :, 0] / (dft[:, :, 0]**2 + dft[:, :, 1]**2) restored_dft[:, :, 1] = dft[:, :, 1] / (dft[:, :, 0]**2 + dft[:, :, 1]**2) restored_image = cv2.idft(restored_dft, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT) # 显示无损还原的图像 cv2.imshow('Restored Image', restored_image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 需要注意的是,在实际应用中,反卷积算法的效果可能会受到多种因素的影响,例如模糊过程中存在的噪声、图像分辨率等。因此,构建一个准确的无损还原模型需要充分考虑这些因素,并进行相应的优化和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值