def hybrid(): # Read Images input_image_1 = skio.imread(INPUT_IMAGES_NAME[0]) input_image_2 = skio.imread(INPUT_IMAGES_NAME[1]) #---------------------------------------------------------------------- # Transform to float image_1 = img_as_float(input_image_1) image_2 = img_as_float(input_image_2) #---------------------------------------------------------------------- # Align images using provided functiom image_aligned_1, image_aligned_2 = align_images(image_1, image_2) #---------------------------------------------------------------------- # Divide image in color channels color_channels_1 = np.array([ image_aligned_1[:, :, 0], image_aligned_1[:, :, 1], image_aligned_1[:, :, 2] ]) color_channels_2 = np.array([ image_aligned_2[:, :, 0], image_aligned_2[:, :, 1], image_aligned_2[:, :, 2] ]) # Use color channels to compose an unique gray scale image # http://stackoverflow.com/questions/12201577/how-can-i-convert-an-rgb-image-into-grayscale-in-python image_1_grayscale = np.clip( color_channels_1[0] * 0.299 + color_channels_1[1] * 0.587 + color_channels_1[2] * 0.114, 0.0, 1.0) image_2_grayscale = np.clip( color_channels_2[0] * 0.299 + color_channels_2[1] * 0.587 + color_channels_2[2] * 0.114, 0.0, 1.0) #---------------------------------------------------------------------- # Apply the Laplacian Filter (high pass filter) to the first image by subtracting the Gaussian Filtered Image from the original low_frequency_image_1 = filter.gaussian_filter(image_1_grayscale, HIGH_FREQUENCY_BLUR_RATIO) high_frequency_image_1 = image_1_grayscale - low_frequency_image_1 # Apply the Gaussian Filter (low pass filter) to the second image low_frequency_image_2 = filter.gaussian_filter(image_2_grayscale, LOW_FREQUENCY_BLUR_RATIO) #---------------------------------------------------------------------- # Add pictures together output_image = np.clip(high_frequency_image_1 + low_frequency_image_2, 0.0, 1.0) #---------------------------------------------------------------------- # Show final image skio.imshow(output_image) skio.show()
def plots(folder): # high sf im1 = plt.imread(os.path.join(folder, 'image1.jpg')) im1 = im1[:, :, :3] # low sf im2 = plt.imread(os.path.join(folder, 'image2.jpg')) im2 = im2[:, :, :3] # Next align images (this code is provided, but may be improved) im1_aligned, im2_aligned = align_images(im1, im2) ## You will provide the code below. Sigma1 and sigma2 are arbitrary ## cutoff values for the high and low frequencies sigma1 = 2 sigma2 = 3 im1_filtered, im2_filtered, hybrid = hybrid_image(im1_aligned, im2_aligned, sigma1, sigma2) ### plot the hybrid image plt.imshow(hybrid) plt.savefig( os.path.join(folder, 'hybrid_' + str(sigma1) + '_' + str(sigma2) + '.png')) plt.imshow(im1_filtered) plt.savefig( os.path.join( folder, 'im1_filtered_' + str(sigma1) + '_' + str(sigma2) + '.png')) plt.imshow(im2_filtered) plt.savefig( os.path.join( folder, 'im2_filtered_' + str(sigma1) + '_' + str(sigma2) + '.png'))
def main(): # First load images # high sf im1 = plt.imread(args.img_high)/255. # low sf im2 = plt.imread(args.img_low)/255 # Next align images (this code is provided, but may be improved) if len(im1.shape) != len(im2.shape) or im1.shape[2] != im2.shape[2]: print("two image cannot be aligned with ease, pick another pair") return im1_aligned, im2_aligned = align_images(im1, im2) # im1_aligned = rgb2gray(im1_aligned) # im2_aligned = rgb2gray(im2_aligned) #change to single color channel hybrid = hybrid_image(im1_aligned, im2_aligned, args.cutoff1, args.cutoff2, args.filter_size) plt.imshow(hybrid) io.imsave(args.img_high[:-4]+args.img_low[:-4]+"hybride.jpg", hybrid) if args.img_high == "apple.jpg": gray_apple = rgb2gray(im1) gray_bad = rgb2gray(im2) filtered_high = high_pass(im1_aligned, args.filter_size, args.cutoff1) filtered_low = high_pass(im2_aligned, args.filter_size, args.cutoff2) io.imsave("fourier_high.jpg", fourier(gray_apple)) io.imsave("fourier_low.jpg", fourier(gray_bad)) io.imsave("fourier_filtered_high.jpg", fourier(filtered_high)) io.imsave("fourier_filtered_low.jpg", fourier(filtered_low)) io.imsave("fourier_hybride.jpg", fourier(hybrid)) ## Compute and display Gaussian and Laplacian Pyramids ## You also need to supply this function return
def hybrid_image(im1, im2, sigma1, sigma2): # Next align images (this code is provided, but may be improved) im1_aligned, im2_aligned = align_images(im1, im2) low_pass_im = low_pass(im1_aligned, sigma1) skio.imsave("./1_2/karl_filtered.png", low_pass_im) gray_low_pass = misc.imread("./1_2/karl_filtered.png", flatten=True) / 255. fourier_save("./1_2/karl_fft.png", gray_low_pass) high_pass_im = high_pass(im2_aligned, sigma2) skio.imsave("./1_2/akita_filtered.png", high_pass_im) gray_high_pass = misc.imread("./1_2/akita_filtered.png", flatten=True) / 255. fourier_save("./1_2/akita_fft.png", gray_high_pass) return np.clip(low_pass_im + high_pass_im, 0, 1)
def main(): import argparse parser = argparse.ArgumentParser() parser.add_argument('im_names', nargs='*', type=str) parser.add_argument('--show_originals', action='store_true') parser.add_argument('--plot_filters', action='store_true') parser.add_argument('--plot_fft', action='store_true') parser.add_argument('--gray', action='store_true') parser.add_argument('--restore', action='store_true') parser.add_argument('--s1', type=int, default=6) parser.add_argument('--s2', type=int, default=6) parser.add_argument('--lam', type=float, default=0.8) args = parser.parse_args() assert(len(args.im_names) == 2) if args.restore: save_file = 'pickle/{}_{}.pkl'.format(args.im_names[0], args.im_names[1]) with open(save_file, 'rb') as f: hybrid = pickle.load(f) else: im1 = plt.imread('images/{}.png'.format(args.im_names[0])) im1 = im1[:,:,:3] #only keeps 3 channels -rgb im2 = plt.imread('images/{}.png'.format(args.im_names[1])) im2 = im2[:,:,:3] im1_aligned, im2_aligned = align_images(im1, im2) if args.show_originals: generate_greyscale(im1_aligned) generate_greyscale(im1_aligned) generate_fft_plot(im1_aligned) generate_fft_plot(im2_aligned) sigma1 = args.s1 sigma2 = args.s2 save_file = 'pickle/{}_{}.pkl'.format(args.im_names[0], args.im_names[1]) hybrid = hybrid_image(im1_aligned, im2_aligned, sigma1, sigma2, lam=args.lam, plot_filters=args.plot_filters, plot_fft=args.plot_fft) with open(save_file, 'wb') as f: pickle.dump(hybrid, f) if args.gray: generate_greyscale(hybrid) else: generate_rgb(hybrid) show_scales(hybrid, gray=args.gray)
def morpher_main(start_image_path, end_image_path, gif_name): start_image = mpl.pyplot.imread(start_image_path) start_gray = sk.color.rgb2gray(start_image) end_image = mpl.pyplot.imread(end_image_path) end_gray = sk.color.rgb2gray(end_image) start_keypoints = menpo.utils.detect_landmarks(start_gray, 1) end_keypoints = menpo.utils.detect_landmarks(end_gray, 1) start_landmarks = start_keypoints[0].landmarks end_landmarks = end_keypoints[0].landmarks start_left_eye = (start_landmarks[left_eye_index][1], start_landmarks[left_eye_index][0]) start_right_eye = (start_landmarks[right_eye_index][1], start_landmarks[right_eye_index][0]) end_left_eye = (end_landmarks[left_eye_index][1], end_landmarks[left_eye_index][0]) end_right_eye = (end_landmarks[right_eye_index][1], end_landmarks[right_eye_index][0]) start_aligned, end_aligned = align_images( start_image, end_image, (start_left_eye, start_right_eye, end_left_eye, end_right_eye)) start_aligned = img_as_float(start_aligned) end_aligned = img_as_float(end_aligned) start_aligned_gray = sk.color.rgb2gray(start_aligned) end_aligned_gray = sk.color.rgb2gray(end_aligned) start_keypoints = menpo.utils.detect_landmarks(start_aligned_gray, 1) end_keypoints = menpo.utils.detect_landmarks(end_aligned_gray, 1) start_inputs = np.flip(np.array(start_keypoints[0].landmarks), 1) end_inputs = np.flip(np.array(end_keypoints[0].landmarks), 1) for i in range(len(ts)): t = ts[i] print i mid_way_s, mid_way_e = generate_mid_way(start_aligned, start_inputs, end_aligned, end_inputs, t) final_mid = t * mid_way_s + (1 - t) * mid_way_e images.append(final_mid) images.extend(images[::-1]) imageio.mimsave(gif_name, images, 'GIF', **kargs)
def hybrid_image(im1, im2, gaussian_kernel, save_intermediates=False): im1, im2 = align_images(im1, im2) # Normalize images im1 = im1 / np.max(im1) im2 = im2 / np.max(im2) # Low pass filter first image lpf_im1 = blur(im1, gaussian_kernel) lpf_im2 = blur(im2, gaussian_kernel) # Get high frequencies for image 2 hpf_im2 = im2 - lpf_im2 # Average images hybrid = (lpf_im1 + hpf_im2)/2 if save_intermediates: os.makedirs('outputs/hybrid/fft/', exist_ok=True) save_fft('outputs/hybrid/fft/im1.jpg', rgb2gray(im1)) save_fft('outputs/hybrid/fft/im2.jpg', rgb2gray(im2)) save_fft('outputs/hybrid/fft/lpf_im1.jpg', rgb2gray(lpf_im1)) save_fft('outputs/hybrid/fft/hpf_im2.jpg', rgb2gray(hpf_im2)) save_fft('outputs/hybrid/fft/hybrid.jpg', rgb2gray(hybrid)) return hybrid
def hybrid(im1_name, im2_name, sig1, sig2): # high sf im1 = plt.imread(im1_name) / 255. # low sf im2 = plt.imread(im2_name) / 255. # Next align images (this code is provided, but may be improved) im1_aligned, im2_aligned = align_images(im1, im2) im1_aligned = rgb2gray(im1_aligned) im2_aligned = rgb2gray(im2_aligned) orig = scipy.ndimage.filters.gaussian_filter(im1, 1) low_pass = scipy.ndimage.filters.gaussian_filter(im1, 5) high_pass = orig - low_pass final = low_pass + high_pass sigma1 = sig1 sigma2 = sig2 hybrid = hybrid_image(im1_aligned, im2_aligned, sigma1, sigma2) plt.imshow(hybrid, cmap="Greys_r") plt.show()
def hybrid(im1_name, im2_name, sig1, sig2): # high sf im1 = plt.imread(im1_name)/255. # low sf im2 = plt.imread(im2_name)/255. # Next align images (this code is provided, but may be improved) im1_aligned, im2_aligned = align_images(im1, im2) im1_aligned = rgb2gray(im1_aligned) im2_aligned = rgb2gray(im2_aligned) orig = scipy.ndimage.filters.gaussian_filter(im1, 1) low_pass = scipy.ndimage.filters.gaussian_filter(im1, 5) high_pass = orig - low_pass final = low_pass + high_pass sigma1 = sig1 sigma2 = sig2 hybrid = hybrid_image(im1_aligned, im2_aligned, sigma1, sigma2) plt.imshow(hybrid, cmap="Greys_r") plt.show()
import matplotlib.pyplot as plt from align_image_code import align_images # First load images # high sf im1 = plt.imread('data/DerekPicture.jpg') / 255. # low sf im2 = plt.imread('data/nutmeg.jpg') / 255 # Next align images (this code is provided, but may be improved) im1_aligned, im2_aligned = align_images(im1, im2) ## You will provide the code below. Sigma1 and sigma2 are arbitrary ## cutoff values for the high and low frequencies sigma1 = 2 sigma2 = 2 # hybrid = hybrid_image(im1, im2, sigma1, sigma2) # plt.imshow(hybrid) # plt.show ## Compute and display Gaussian and Laplacian Pyramids ## You also need to supply this function N = 5 # suggested number of pyramid levels (your choice) # pyramids(hybrid, N)
im = plt.imread(file) img3 = laplacian_filter(im, 10, 4.0) plt.imshow(img3) plt.show() skio.imsave("part0_unsharp_builtin.jpg", img2) skio.imsave("part0_unsharp_coded.jpg", img3) # high sf if part1: im1 = plt.imread("./sup2.jpg") / 255.0 # plt.imshow(gray, cmap = plt.get_cmap('gray')) # low sf im2 = plt.imread("./darrell.jpg") / 255.0 # Next align images (this code is provided, but may be improved) im1_aligned, im2_aligned = align_images(im1, im2) if debug: plt.imshow(im1_aligned) plt.figure() plt.imshow(im2_aligned) plt.show() manual = True if manual: sigma1 = 1.5 filter_n = 5 G_HPF = np.zeros([filter_n, filter_n]) for x in range(filter_n): for y in range(filter_n): weight = 1.0 / (2.0 * np.pi * pow(sigma1, 2)) exp_weight = -(pow((x - filter_n // 2), 2) + pow((y - filter_n // 2), 2)) / (2.0 * pow(sigma1, 2))
path = BASE_DIR + '/%s.jpg' % name cv2.imwrite(path, img) if __name__ == '__main__': from settings import BASE_DIR from align_image_code import align_images import matplotlib.pyplot as plt from base_service import * # im1 = plt.imread(BASE_DIR + '/img/man.jpg') print im1.shape # im2 = plt.imread(BASE_DIR + '/img/cat.jpg') re2, re1 = align_images(im2, im1) print re1.shape save_plt_img(re1, "img", "align1") save_plt_img(re2, "img", "align2") plt.close() a1 = read_cv2_img("align1") a2 = read_cv2_img("align2") print a1.shape hig = high_pass_filter(a2, 15) low = low_pass_filter(a1, 15) re = hybrid_img(hig, low) save_photo(re, "result") cv2.imshow("result", re) # save_plt_img(re, "img", "result") cv2.waitKey(0)
def hybrid(): # Read Images input_image_1 = skio.imread(INPUT_IMAGES_NAME[0]) input_image_2 = skio.imread(INPUT_IMAGES_NAME[1]) #---------------------------------------------------------------------- # Transform to float image_1 = img_as_float(input_image_1) image_2 = img_as_float(input_image_2) #---------------------------------------------------------------------- # Align images using provided functiom image_aligned_1, image_aligned_2 = align_images(image_1, image_2) #---------------------------------------------------------------------- # Divide image in color channels color_channels_1 = np.array([ image_aligned_1[:, :, 0], image_aligned_1[:, :, 1], image_aligned_1[:, :, 2] ]) color_channels_2 = np.array([ image_aligned_2[:, :, 0], image_aligned_2[:, :, 1], image_aligned_2[:, :, 2] ]) #---------------------------------------------------------------------- # For each channel, do the same process as in a gray image result_channels = [] for c1, c2 in zip(color_channels_1, color_channels_2): #--------------------------------------------------------------- # Apply the Laplacian Filter (high pass filter) to the first image by subtracting the Gaussian Filtered Image from the original low_frequency_c1 = filter.gaussian_filter(c1, HIGH_FREQUENCY_BLUR_RATIO) high_frequency_c1 = c1 - low_frequency_c1 # Apply the Gaussian Filter (low pass filter) to the second image low_frequency_c2 = filter.gaussian_filter(c2, LOW_FREQUENCY_BLUR_RATIO) #--------------------------------------------------------------- # Add pictures together result = np.clip(high_frequency_c1 + low_frequency_c2, 0.0, 1.0) #--------------------------------------------------------------- # Save the processed channel result_channels.append(result) #---------------------------------------------------------------------- # Colapse every channel into the final colored image output_image = np.dstack(result_channels) # Show final image skio.imshow(output_image) skio.show()
def align(im1, im2): im1_aligned, im2_aligned = align_images(im1, im2) # plt.imshow(im1_aligned * 0.5 + im2_aligned * 0.5) # plt.show() return im1_aligned, im2_aligned