OpenCV 图像金字塔的使用

案例来源于© Fu Xianjun. All Rights Reserved

一高斯金字塔

导包

import cv2 
import numpy as np

 高斯金字塔

#高斯金字塔
def pyramid_demo(image,level):
    temp = image.copy()
    pyramid_images = []
    for i in range(level):
        dst = cv2.pyrDown(temp)
        pyramid_images.append(dst)
        cv2.imshow("pyramid_down_"+str(i), dst)
        temp = dst.copy()
    return pyramid_images[level-1]

演示效果

src = cv2.imread("lena.jpg")
cv2.imshow("input image", src)
pyramid_demo(src,4)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

二拉普拉斯金字塔的构建

# 拉普拉斯金字塔构建
G0 = cv2.imread("lena.bmp")
cv2.imshow("input image",G0)
G1=cv2.pyrDown(G0)
G2=cv2.pyrDown(G1)
G3=cv2.pyrDown(G2)
G4=cv2.pyrDown(G3)
L0 = cv2.subtract(G0,cv2.pyrUp(G1))
L1 = cv2.subtract(G1,cv2.pyrUp(G2))
L2 = cv2.subtract(G2,cv2.pyrUp(G3))
L3 = cv2.subtract(G3,cv2.pyrUp(G4))
cv2.imshow("G1",G1)
cv2.imshow("G2",G2)
cv2.imshow("G3",G3)
cv2.imshow("G4",G4)
cv2.waitKey(0)
cv2.destroyAllWindows()

三利用金字塔实现图像拼接

import cv2
import numpy as np
A = cv2.imread('apple.png')
A = cv2.resize(A,(256,256),interpolation=cv2.INTER_CUBIC) 
B = cv2.imread('orange.png')
B = cv2.resize(B,(256,256),interpolation=cv2.INTER_CUBIC)
# 生成高斯金字塔
G = A.copy()
gpA = [G]
for i in range(5):
    G = cv2.pyrDown(G)
    gpA.append(G)
    
G = B.copy()
gpB = [G]
for i in range(5):
    G = cv2.pyrDown(G)
    gpB.append(G)
# 产生Laplacian金字塔
lpA = [gpA[5]]
for i in range(5,0,-1):
    GE = cv2.pyrUp(gpA[i])
    L = cv2.subtract(gpA[i-1],GE)
    lpA.append(L)

lpB = [gpB[5]]
for i in range(5,0,-1):
    GE = cv2.pyrUp(gpB[i])
    L = cv2.subtract(gpB[i-1],GE)
    lpB.append(L)
# 合并
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)
# 重新构建图像
ls_ = LS[0]
for i in range(1,6):
    ls_ = cv2.pyrUp(ls_)
    ls_ = cv2.add(ls_, LS[i])
# 连接
real = np.hstack((A[:,:cols//2],B[:,cols//2:]))
cv2.imshow("apple",A)
cv2.imshow("orange",B)
cv2.imshow("LS",ls_)
cv2.imshow("Real",real)
cv2.waitKey()
cv2.destroyAllWindows()

结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值