python+opencv图像金字塔融合

图像金字塔操作,又分为高斯金字塔和拉普拉斯金字塔,高斯金字塔简单的理解为图像的downsample和upsample.

拉普拉斯金字塔是图像A  - up(down(A))的操作,看上去全是边缘。


#!/usr/bin/env python

import cv2

img = cv2.imread('frame.jpg')
cv2.imshow('src', img)

lower_reso= cv2.pyrDown(img)
print lower_reso.shape
cv2.imshow('downsample', lower_reso)

higher_reso = cv2.pyrUp(lower_reso)
print higher_reso.shape
cv2.imshow('upsample', higher_reso)

cv2.imshow('laplace', img - higher_reso)

cv2.waitKey(0)

两张图像直接拼接和高斯laplace金字塔融合比较:

#!/usr/bin/env python

import cv2
import numpy as np
import sys


A = cv2.imread('frame.jpg')
B = cv2.imread('book.jpg')

#generate gaussian pyramid for A
G=A.copy()
gpA=[G]
for i in xrange(6):
    G=cv2.pyrDown(G)
    gpA.append(G)

#generate gaussian pyramid for B
G=B.copy()
gpB=[G]
for i in xrange(6):
    G=cv2.pyrDown(G)
    gpB.append(G)

#generate laplace pyramid for A
lpA = [gpA[5]]
for i in xrange(5, 0, -1):
    GE = cv2.pyrUp(gpA[i])
    L = cv2.subtract(gpA[i-1], GE)
    lpA.append(L)

#generate laplace pyramid for B
lpB = [gpB[5]]
for i in xrange(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 xrange(1,6):
    ls_ = cv2.pyrUp(ls_)
    ls_ = cv2.add(ls_, LS[i])

real = np.hstack((A[:,:cols/2],B[:,cols/2:]))

cv2.imshow('real', real)
cv2.imshow('lap blend', ls_)
cv2.waitKey(0)


评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值