如何使用 Python 中的 OpenCV 为图像找到拉普拉斯金字塔?

opencvpythonserver side programmingprogramming

我们可以从高斯金字塔中形成拉普拉斯金字塔。OpenCV 不提供任何构建拉普拉斯金字塔的特定函数。

在拉普拉斯金字塔中,图像看起来仅像边缘图像。拉普拉斯金字塔用于图像压缩和图像增强。

拉普拉斯金字塔中的一层由高斯金字塔中该层与高斯金字塔中其上层的扩展版本之间的差异形成。

要在高斯金字塔中创建一个层,我们应用cv2.pyrDown()cv2.pyrUp()函数。

步骤

要构建三层拉普拉斯金字塔,请按照以下步骤操作 -

  • 导入所需的库。

  • 加载输入图像。

  • 然后构建具有三个级别的高斯金字塔。

  • 对于拉普拉斯金字塔,高斯金字塔中某一层与其在高斯金字塔中的上一层的扩展版本之间的差异形成一层。高斯金字塔中的最后一层在拉普拉斯金字塔中保持不变。

  • 重复步骤4,构建拉普拉斯金字塔的所有层。

  • 创建窗口以显示所有层并显示它们。

我们将在下面的示例中使用以下图像作为输入文件

示例

在下面的Python3程序中,我们创建了输入图像car.jpg的三层拉普拉斯金字塔。

import cv2 # Load the image img = cv2.imread('car.jpg') lower = img.copy() # Create a Gaussian Pyramid gaussian_pyr = [lower] for i in range(3): lower = cv2.pyrDown(lower) gaussian_pyr.append(lower) # Last level of Gaussian remains same in Laplacian laplacian_top = gaussian_pyr[-1] # Create a Laplacian Pyramid laplacian_pyr = [laplacian_top] for i in range(3,0,-1): size = (gaussian_pyr[i - 1].shape[1], gaussian_pyr[i - 1].shape[0]) gaussian_expanded = cv2.pyrUp(gaussian_pyr[i], dstsize=size) laplacian = cv2.subtract(gaussian_pyr[i-1], gaussian_expanded) laplacian_pyr.append(laplacian) # create three windows to display three layers of images cv2.namedWindow('Layer 1', cv2.WINDOW_AUTOSIZE) cv2.namedWindow('Layer 2', cv2.WINDOW_AUTOSIZE) cv2.namedWindow('Layer 3', cv2.WINDOW_AUTOSIZE) # display all three layers cv2.imshow('Layer 1',laplacian_pyr[3]) cv2.imshow('Layer 2',laplacian_pyr[2]) cv2.imshow('Layer 3',laplacian_pyr[1]) cv2.waitKey(0) cv2.destroyAllWindows()

输出

执行上述代码后,将打开三个输出窗口,每个窗口显示拉普拉斯金字塔中的特定层。

请注意,第 2 层图像是第 1 层图像的四分之一。同样,第 3 层图像是第 2 层图像的四分之一。


相关文章