def main(black_image, white_image, mask, out_path, min_depth=MIN_DEPTH): """Apply pyramid blending to each color channel of the input images """ # Convert to double and normalize the mask to the range [0..1] # to avoid arithmetic overflow issues black_image = np.atleast_3d(black_image).astype(np.float) white_image = np.atleast_3d(white_image).astype(np.float) mask_img = np.atleast_3d(mask).astype(np.float) / 255. shape = mask_img.shape[1::-1] min_size = min(black_image.shape[:2]) depth = int(np.log2(min_size)) - min_depth gauss_pyr_mask = [gaussPyramid(ch, depth) for ch in np.rollaxis(mask_img, -1)] gauss_pyr_black = [gaussPyramid(ch, depth) for ch in np.rollaxis(black_image, -1)] gauss_pyr_white = [gaussPyramid(ch, depth) for ch in np.rollaxis(white_image, -1)] viz_pyramid(gauss_pyr_mask, shape, path.join(out_path, 'gauss_pyr_mask'), norm=True) viz_pyramid(gauss_pyr_black, shape, path.join(out_path, 'gauss_pyr_black')) viz_pyramid(gauss_pyr_white, shape, path.join(out_path, 'gauss_pyr_white')) lapl_pyr_black = [laplPyramid(ch) for ch in gauss_pyr_black] lapl_pyr_white = [laplPyramid(ch) for ch in gauss_pyr_white] viz_pyramid(lapl_pyr_black, shape, path.join(out_path, 'lapl_pyr_black'), norm=True) viz_pyramid(lapl_pyr_white, shape, path.join(out_path, 'lapl_pyr_white'), norm=True) outpyr = [blend(*x) for x in zip(lapl_pyr_white, lapl_pyr_black, gauss_pyr_mask)] outimg = [[collapse(x)] for x in outpyr] viz_pyramid(outpyr, shape, path.join(out_path, 'outpyr'), norm=True) viz_pyramid(outimg, shape, path.join(out_path, 'outimg'))
def multiresolution(img_stack, weight_map): """This function implements the multiresolution method described in "Exposure Fusion" by T. Mertens et al. It uses the laplacian and gaussian pyramids for blending the input images according to their weight maps (instead of the mask) to produces seamless final artifacts. """ no_images = len(img_stack) img_list = [] weight_list = [] new_img_list = [] new_weight_list = [] outpyr = [] for f in range(no_images): img = np.atleast_3d(img_stack[f]).astype(np.float) weight = np.atleast_3d(weight_map[f]).astype(np.float) shape = weight.shape[1::-1] min_size = min(img.shape[:2]) depth = int(np.log2(min_size)) - MIN_DEPTH gauss_pyr_weight = [gaussPyramid(ch, depth) for ch in np.rollaxis(weight, -1)] gauss_pyr = [gaussPyramid(ch, depth) for ch in np.rollaxis(img, -1)] lapl_pyr = [laplPyramid(ch) for ch in gauss_pyr] img_list.append(lapl_pyr) weight_list.append(gauss_pyr_weight) viz_pyramid(gauss_pyr_weight, shape, path.join(OUT_FOLDER, 'gauss_pyr_weight' + str(f)), norm=True) viz_pyramid(lapl_pyr, shape, path.join(OUT_FOLDER, 'lapl_pyr_img' + str(f)), norm=True) """ This 'for' changes (no_images X no_channels X no_layers) order to (no_channels X no_images X no_layers) so that the already implemented blend function can be used for all the three channels.""" for ch in range(3): new_img_list.append([]) new_weight_list.append([]) for img in range(no_images): new_img_list[ch].append(img_list[img][ch][:]) new_weight_list[ch].append(weight_list[img][0][:]) outpyr.append(blend(new_img_list[ch], new_weight_list[ch])) outimg = [[collapse(x)] for x in outpyr] viz_pyramid(outpyr, shape, path.join(OUT_FOLDER, 'outpyr'), norm=True) viz_pyramid(outimg, shape, path.join(OUT_FOLDER, 'outimg'))
def run_blend(black_image, white_image, mask): """Compute the blend of two images along the boundaries of the mask. Assume all images are float dtype, and return a float dtype. """ # Automatically figure out the size; at least 16x16 at the highest level min_size = min(black_image.shape) depth = int(np.log2(min_size)) - 4 gauss_pyr_mask = blend.gaussPyramid(mask, depth) gauss_pyr_black = blend.gaussPyramid(black_image, depth) gauss_pyr_white = blend.gaussPyramid(white_image, depth) lapl_pyr_black = blend.laplPyramid(gauss_pyr_black) lapl_pyr_white = blend.laplPyramid(gauss_pyr_white) outpyr = blend.blend(lapl_pyr_white, lapl_pyr_black, gauss_pyr_mask) img = blend.collapse(outpyr) return (gauss_pyr_black, gauss_pyr_white, gauss_pyr_mask, lapl_pyr_black, lapl_pyr_white, outpyr, [img])
aux = output[len(output) - 1] for j in range(len(output) - 1, 0, -1): aux = pPyr.pyrExpand(aux, output[j - 1]) i += 1 cv2.imwrite('output/p1-2-3-{}.png'.format(k), aux) k += 1 # 2.4 img1 = cv2.imread('input/p1-2-4-0.png') img2 = cv2.imread('input/p1-2-4-1.png') b_mask = cv2.imread('input/p1-2-4-2.png') output = bl.blend(img1, img2, b_mask, 4) cv2.imwrite('output/p1-2-4-0.png', output) img1 = cv2.imread('input/p1-2-4-3.png') img2 = cv2.imread('input/p1-2-4-4.png') b_mask = cv2.imread('input/p1-2-4-5.png') output = bl.blend(img1, img2, b_mask, 4) cv2.imwrite('output/p1-2-4-1.png', output) #3.1 # Importing in grayscale for cleaner code input = cv2.imread('input/p1-1-0.png', 0) percents = [-1, 25, 50, 75, 100]
art_path = sys.argv[2] #check if face in photo and art if not check_face(photo_path): print("The photo must include a face") exit() has_face = check_face(art_path) #if face find face in both and map it if has_face: photo_features = find_features(photo_path) art_features = find_features(art_path) img1 = np.asarray(imageio.imread(photo_path)) img2 = np.asarray(imageio.imread(art_path)) H = compute_homography(photo_features, art_features) warped = warp_image(img1, img2, H) blended = blend(warped, img2, art_features) plt.imshow(blended) plt.show() #if not then do texture mapping else: face = np.asarray(imageio.imread(photo_path)) portrait = np.asarray(imageio.imread(art_path)) final = texture_transfer.textureTransfer(face, portrait) plt.imshow(final) plt.show()