#Part 1.4 #Multiresolution blending computes a gentle seam between the #two images #seperately at each band of frequencies, resulting in a #much smoother seam. import scipy apple = skio.imread('apple.jpeg') # apple = scipy.misc.imresize(apple, 0.25) apple = apple/255.0 # apple = skimage.util.invert(apple) #need this for the grayscale version apple_bw = skimage.color.rgb2gray(apple) orange = skio.imread('orange.jpeg') # orange = scipy.misc.imresize(orange, apple.shape) orange = orange/255.0 # orange = skimage.util.invert(orange) orange_bw = skimage.color.rgb2gray(orange) def blend(im1, im2, mask, N): result = [] mask_gaussians = gaussian_pyramid(mask, N) im1_laplacians = laplacian_pyramid(im1, N) im1_gaussians = gaussian_pyramid(im1, N) im2_laplacians = laplacian_pyramid(im2, N) im2_gaussians = gaussian_pyramid(im2, N) for i in range(N): mask1 = mask_gaussians[i] mask2 = 1-mask1 band1 = im1_laplacians[i] band2 = im2_laplacians[i] if i == N-1: #last laplacian stack is the rest of the gaussian band1 = im1_gaussians[i] band2 = im2_gaussians[i] band1 = scipy.misc.imresize(band1, mask1.shape) band2 = scipy.misc.imresize(band2, mask2.shape) band1 = np.multiply(mask1, band1) band2 = np.multiply(mask2, band2) band_i = band1+band2 result.append(band_i) blended = reduce(lambda a,b: a + b, result) return blended def make_mask(image_size): left = np.ones((image_size[0], image_size[1]/2)) right = np.zeros((image_size[0], image_size[1]/2)) mask = np.concatenate([left, right], axis = 1) return mask def blend_color(im1, im2, mask, N): #split the image into color channels im1_red = im1[:,:,0] im2_red = im2[:,:,0] im1_green = im1[:,:,1] im2_green = im2[:,:,1] im1_blue = im1[:,:,2] im2_blue = im2[:,:,2] red = blend(im1_red, im2_red, mask, N) green = blend(im1_green, im2_green, mask, N) blue = blend(im1_blue, im2_blue, mask, N) blended = np.dstack([red, green, blue]) blended = np.clip(blended, 0, 1) return blended mask = make_mask(apple.shape) # irreg_mask = skio.imread('mask_s.jpg')/255.0 # irreg_mask = scipy.misc.imresize(irreg_mask, 0.25) # oraple_bw = blend(apple_bw, orange_bw, mask, 5) # plt.figure() # plt.axis('off') # plt.imshow(skimage.util.invert(oraple_bw)) # oraple_color = blend_color(apple, orange, mask, 5) # plt.figure() # # plt.axis('off') # plt.imshow(oraple_color)