1. 向下采样
OpenCV 提供了函数 cv2.pyrDown(),用于实现图像高斯金字塔操作中的向下采样,其语法 形式为:
dst = cv2.pyrDown( src[, dstsize[, borderType]] )
其中:
- dst 为目标图像。
- src 为原始图像。
- dstsize 为目标图像的大小。
- borderType 为边界类型,默认值为 BORDER_DEFAULT ,且这里仅支持BORDER_DEFAULT。
【例 11.1】使用函数 cv2.pyrDown()对一幅图像进行向下采样,观察采样的结果。
import cv2
import numpy as np
img = cv2.imread("/Users/zhaofeier/Desktop/源代码及图像/chapter11/lena.bmp",0)
r1 = cv2.pyrDown(img)
r2 = cv2.pyrDown(r1)
r3 = cv2.pyrDown(r2)
print("img.shape=",img.shape)
print("r1.shape=",r1.shape)
print("r2.shape=",r2.shape)
print("r3.shape=",r3.shape)
cv2.imshow("img",img)
cv2.imshow("result1",r1)
cv2.imshow("result2",r2)
cv2.imshow("result3",r3)
cv2.waitKey()
cv2.destroyAllWindows()
2. 向上采样
在 OpenCV 中,使用函数 cv2.pyrUp()实现图像金字塔操作中的向上采样,其语法形式如下:
dst = cv2.pyrUp( src[, dstsize[, borderType]] )
其中:
- dst 为目标图像。
- src 为原始图像。
- dstsize 为目标图像的大小。
- borderType 为边界类型,默认值为 BORDER_DEFAULT ,且这里仅支持BORDER_DEFAULT。
img = cv2.imread(“/Users/zhaofeier/Desktop/源代码及图像/chapter11/lenas.bmp”)
r1 = cv2.pyrUp(img)
r2 = cv2.pyrUp(r1)
r3 = cv2.pyrUp(r2)
print("img.shape=",img.shape)
print("r1.shape=",r1.shape)
print("r2.shape=",r2.shape)
print("r3.shape=",r3.shape)
cv2.imshow("img",img)
cv2.imshow("result1",r1)
cv2.imshow("result2",r2)
cv2.imshow("result3",r3)
cv2.waitKey()
cv2.destroyAllWindows()