#图像金字塔融合
import numpy as np
import matplotlib.pyplot as plt
from skimage import data, transform, color,exposure, img_as_float, io
import os
class Fusion(object):
def __init__(self,AddressofFolder):
self.folder_name = AddressofFolder
self.n_images = None
self.images = self.load_images(self.folder_name)
self.pyramids = self.get_laplacian()
self.ls = None
self.stack_ls()
def load_images(self,path):
names = os.listdir(path)
self.n_images = len(names)
images = [io.imread(os.path.join(path,n)) for n in names]
new_ = []
for i in images:
new_.append(transform.resize(i,(600,600,3)))
return new_
def get_laplacian(self):
ls = []
for i in self.images:
lp = list(transform.pyramid_laplacian(i, downscale=2, multichannel=True))
ls.append(lp)
return ls
def stack_ls(self):
layers = len(self.pyramids[0])
n1 = self.n_images//2
n2 = self.n_images - n1
ls1_imgs = []
for l in range(0,layers):
temp = self.pyramids[0][l]
for i in range(1,n1):
temp = np.hstack((temp[:,:],self.pyramids[i][l][:,:]))
ls1_imgs.append(temp)
#[print(i.shape) for i in ls1_imgs]
ls1_shape = [l.shape for l in ls1_imgs]
for i in range(n1,self.n_images):
for l in range(0,layers):
if i!=self.n_images-1:
self.pyramids[i][l] = transform.resize(self.pyramids[i][l],(self.pyramids[i][l].shape[0],max(1,ls1_shape[l][1]//n2)))
else:
w = ls1_shape[l][1] - ls1_shape[l][1]//n2*(n2-1)
#h = ls1_shape[l][0] - ls1_shape[l][0]//n2*(n2-1)
self.pyramids[i][l] = transform.resize(self.pyramids[i][l],(self.pyramids[i][l].shape[0],w))
[print(i[0].shape) for i in self.pyramids]
ls2_imgs = []
for l in range(0,layers):
temp = self.pyramids[n1][l]
for i in range(n1+1,self.n_images):
temp = np.hstack((temp[:,:],self.pyramids[i][l][:,:]))
ls2_imgs.append(temp)
self.ls = []
[print(i.shape) for i in ls1_imgs]
[print(i.shape) for i in ls2_imgs]
for l1,l2 in zip(ls1_imgs,ls2_imgs):
l = np.vstack((l1[:,:],l2[:,:]))
self.ls.append(l)
print(len(self.ls))
[print(i.shape) for i in self.ls]
def sfusion(self):
plt.figure(dpi=300)
new_image = self.ls[-1]
for i in range(len(self.ls) - 1):
temp_img = self.ls[len(self.ls) - 2 - i]
# gs_tmp = GS[len(LS)-2-i]
# z = temp_img.shape[0]
# o = temp_img.shape[1]
# new_image = transform.resize(new_image,(rows,cols))
new_image = transform.rescale(new_image, 2, multichannel=True)
temp_img = transform.resize(temp_img, (new_image.shape[0], new_image.shape[1]))
# gs_tmp = transform.resize(gs_tmp, (new_image.shape[0], new_image.shape[1]))
new_image = new_image + temp_img
self.fusion_image = new_image
plt.imshow(new_image*6)
plt.axis("off")
plt.savefig("C:/Users/zhuyupeng/Desktop/image/")
# plt.show()
def CollageCreate(AddressofFolder):
f = Fusion(AddressofFolder)
print([i.shape for i in f.images])
f.sfusion()
if __name__ == '__main__':
CollageCreate("C:/Users/zhuyupeng/Desktop/image")