Bootstrap

6-图像金字塔与轮廓检测


6.图像金字塔与轮廓检测

(1)图像金字塔定义

  • 高斯金字塔
  • 拉普拉斯金字塔
    在这里插入图片描述

高斯金字塔:向下采样方法(缩小)
在这里插入图片描述

高斯金字塔:向上采样方法(放大)
在这里插入图片描述



(2)金字塔制作方法

import cv2
import matplotlib.pyplot as plt
import numpy as np

img = cv2.imread('lena.jpg')
print(img.shape)
cv2.imshow('img',img)
cv2.waitKey()
cv2.destroyAllWindows()

img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# 使用Matplotlib显示图像
plt.imshow(img_rgb)
plt.title(f"img_Shape: {
     img.shape}")
plt.show()
(263, 263, 3)

在这里插入图片描述


up = cv2.pyrUp(img)
print(up.shape)
cv2.imshow('up',up)
cv2.waitKey()
cv2.destroyAllWindows()

img_rgb = cv2.cvtColor(up, cv2.COLOR_BGR2RGB)

# 使用Matplotlib显示图像
plt.imshow(img_rgb)
plt.title(f"up_Shape: {
     up.shape}")
plt.show()
(526, 526, 3)

在这里插入图片描述


down = cv2.pyrDown(img)
print(down.shape)
cv2.imshow('down',down)
cv2.waitKey()
cv2.destroyAllWindows()

img_rgb = cv2.cvtColor(down, cv2.COLOR_BGR2RGB)

# 使用Matplotlib显示图像
plt.imshow(img_rgb)
plt.title(f"down_Shape: {
     down.shape}")
plt.show()
(132, 132, 3)

在这里插入图片描述

#先进行上采样再下采样
up = cv2.pyrUp(img)
up_down = cv2.pyrDown(up)
cv2.imshow('up_down',down)
cv2.waitKey()
cv2.destroyAllWindows()

# 使用Matplotlib显示图像
img_rgb = cv2.cvtColor(up_down, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.title(f"up_down_Shape: {
     up_down.shape}")
plt.show()

在这里插入图片描述


res = np.hstack((img,up_down))
cv2.imshow('res',res)
cv2.waitKey()
cv2.destroyAllWindows()

# 使用Matplotlib显示图像
img_rgb = cv2.cvtColor(res, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt
;