python opencv 图像金字塔

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/TingHW/article/details/84527531
# 不同分辨率的图片叫做图片金字塔
# 有两种图像金字塔1)高斯金字塔 2)拉普拉斯金字塔
# 两者的简要区别:高斯金字塔用来向下降采样图像,而拉普拉斯金字塔则用来从金字塔底层图像中向上采样重建一个图像。

# 高斯金字塔 
# 我们用cv2.pyrDown()和cv2.pyrUp()函数来找高斯金字塔
# 高斯金字塔的高级(低分辨率)是从低级别(高分辨率)的图像里移除连续的行和列来形成的
import cv2
import numpy as np
img = cv2.imread('touxiang.jpg')
lower_reso = cv2.pyrDown(img)
higher_reso2 = cv2.pyrUp(img)

cv2.imshow('lower_reso',lower_reso)
cv2.imshow('higher_reso2',higher_reso2)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 一个金字塔的应用是图像混合,比如拼接中,你可能会需要把两个图像堆到一起,不过可能会因为图像不连续而不好看,
# 在这种情况下,用金字塔进行图像混合可以让图片无缝混合

# 拼接图像

import cv2
import numpy as np, sys

A = cv2.imread('smallpig.jpg')
B = cv2.imread('zaodian_smallpig.jpg')
print(B.shape)
print(A.shape)

A = cv2.resize(A, (768, 1152), interpolation=cv2.INTER_CUBIC)#图像大小转换  一次其图片像素的行数和列数要能够被(2X2X2X2X2X2)整除
B = cv2.resize(B ,(768, 1152), interpolation=cv2.INTER_CUBIC)#图像大小转换  保证其行列都是2的n次方的倍数

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

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

# generate Laplacian Pyramid for A
lpA = [gpA[5]]
for i in range(5,0,-1):
    GE = cv2.pyrUp(gpA[i])
    print(GE.shape)
    print(gpA[i-1].shape)
    L = cv2.subtract(gpA[i-1],GE)
    lpA.append(L)

# generate Laplacian Pyramid for B
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)

# Now add left and right halves of images in each level
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)

# now reconstruct
ls_ = LS[0]
for i in range(1,6):
    ls_ = cv2.pyrUp(ls_)
    ls_ = cv2.add(ls_,LS[i])

# image with direct connecting each half
real = np.hstack((A[:,:cols//2],B[:,cols//2:]))

cv2.imwrite('Pyramid_blending2.jpg',ls_)
cv2.imwrite('Direct_blending.jpg',real)

(1280, 720, 3)
(1280, 720, 3)
(72, 48, 3)
(72, 48, 3)
(144, 96, 3)
(144, 96, 3)
(288, 192, 3)
(288, 192, 3)
(576, 384, 3)
(576, 384, 3)
(1152, 768, 3)
(1152, 768, 3)





True

猜你喜欢

转载自blog.csdn.net/TingHW/article/details/84527531