1. 程式人生 > >python opencv 影象金字塔

python opencv 影象金字塔

# 不同解析度的圖片叫做圖片金字塔
# 有兩種影象金字塔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