python + opencv图像处理(十七)——图像金字塔

图像金字塔原理
高斯金字塔与拉普拉斯金字塔
reduce = 高斯模糊+降采样
expand = 扩大 + 卷积
PyrDown:降采样
PyrUp:还原

from matplotlib import pyplot as plt 
from cv2 import cv2 as cv
import numpy as np 

# 高斯金字塔
def pyramid_demo(image):
    level = 3   #层数
    temp = image.copy()
    pyramid_images = []
    for i in range(level):
        dst = cv.pyrDown(temp)
        pyramid_images.append(dst)
        cv.imshow('pyramid_down_'+str(i),dst)
        temp = dst.copy()
    return pyramid_images

# 拉普拉斯金字塔
# 通过高斯金字塔可以构建拉普拉斯金字塔
def lapalian_demo(image):
    pyramid_images = pyramid_demo(image)
    level = len(pyramid_images)     #求层数
    for i in range(level-1,-1,-1): # 每次递减
        if (i-1) < 0:
            expand = cv.pyrUp(pyramid_images[i], dstsize = image.shape[:2])
            lpls = cv.subtract(image, expand)
            cv.imshow("lapalian_down_" + str(i), lpls)
        else:
            expand = cv.pyrUp(pyramid_images[i], dstsize = pyramid_images[i-1].shape[:2])
            lpls = cv. subtract(pyramid_images[i-1], expand)
            cv. imshow("lapalian_down_"+str(i), lpls)

if __name__ == '__main__':
	filepath = "C:\\pictures\\cat.jpg"  # 对于这样 图片必须是2^n 格式如(512*512)
	img = cv.imread(filepath)       # blue green red
	# cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
	cv.imshow("input image",img)
	
	lapalian_demo(img)
	
	cv.waitKey(0)
	cv.destroyAllWindows()

拉普拉斯:
python + opencv图像处理(十七)——图像金字塔_第1张图片
高斯:
python + opencv图像处理(十七)——图像金字塔_第2张图片

你可能感兴趣的:(opencv)