def large_1d_blur(): large_1d_blur_filter = luo_fspecial(25, 1, 10) large_blur_image = my_imfilter(test_image, large_1d_blur_filter) large_blur_image = my_imfilter(large_blur_image, large_1d_blur_filter) luo_imshow("Figure 4", large_blur_image) luo_imwrite(large_blur_image, 'large_blur_image.jpg') return large_blur_image
def main(): """function to helps debug your image filtering algorithm. """ main_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) img_path = os.path.join(main_path, 'data', 'cat.bmp') test_image = mpimg.imread(img_path) test_image = imresize(test_image, 0.7, interp='bilinear') test_image = test_image.astype(np.float32) / 255 plt.figure('Image') plt.imshow(test_image) identity_filter = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]], dtype=np.float32) identity_image = my_imfilter(test_image, identity_filter) identity_image_gt = ground_truth_imfilter(test_image, identity_filter) print('identity test') check(identity_image, identity_image_gt) ### Small blur with a box filter ### # This filter should remove some high frequencies blur_filter = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.float64) blur_filter = blur_filter.astype(np.float) / np.sum( blur_filter) # making the filter sum to 1 blur_filter = np.ones((3, 3)) / 9 blur_image = my_imfilter(test_image, blur_filter) blur_image_gt = ground_truth_imfilter(test_image, blur_filter) print('blur test') check(blur_image, blur_image_gt) ### Large blur ### #This blur would be slow to do directly, so we instead use the fact that #Gaussian blurs are separable and blur sequentially in each direction. large_2d_blur_filter = gauss2D(shape=(25, 25), sigma=10) large_blur_image = my_imfilter(test_image, large_2d_blur_filter) large_blur_image_gt = ground_truth_imfilter(test_image, large_2d_blur_filter) print('large_2d_blur test') check(large_blur_image, large_blur_image_gt) ### Oriented filter (Sobel Operator) ### sobel_filter = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) sobel_image = my_imfilter(test_image, sobel_filter) sobel_image_gt = ground_truth_imfilter(test_image, sobel_filter) #0.5 added because the output image is centered around zero otherwise and mostly black print('sobel test') check(sobel_image, sobel_image_gt) ### High pass filter (Discrete Laplacian) ### laplacian_filter = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) laplacian_image = my_imfilter(test_image, laplacian_filter) laplacian_image_gt = ground_truth_imfilter(test_image, laplacian_filter) #0.5 added because the output image is centered around zero otherwise and mostly black print('laplacian test') check(laplacian_image, laplacian_image_gt) ### High pass "filter" alternative ### high_pass_image = test_image - blur_image #simply subtract the low frequency content high_pass_image_gt = test_image - blur_image_gt print('high pass test') check(high_pass_image, high_pass_image_gt)
def sobel(): global image_cache sobel_h = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]]) sobel_v = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]]) horizontal = my_imfilter(image_cache, sobel_h) vertical = my_imfilter(image_cache, sobel_v) image_cache = horizontal + vertical show(image_cache)
def test_optimum_frequencies(): for image1, image2 in hybridize: i = i + 1 for cutoff_freq in freq: fig = plt.figure(cutoff_freq) filter = generate_gaussian_filter( (cutoff_freq * 4 + 1, cutoff_freq * 4 + 1), cutoff_freq ) #insert values from fspecial('Gaussian', cutoff_frequency*4+1, cutoff_frequency) here """ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % YOUR CODE BELOW. Use my_imfilter to create 'low_frequencies' and % 'high_frequencies' and then combine them to create 'hybrid_image' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Remove the high frequencies from image1 by blurring it. The amount of % blur that works best will vary with different image pairs %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% """ low_frequencies = my_imfilter(image1, filter) """ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Remove the low frequencies from image2. The easiest way to do this is to % subtract a blurred version of image2 from the original version of image2. % This will give you an image centered at zero with negative values. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% """ high_frequencies = image2 - my_imfilter(image2, filter) """ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Combine the high frequencies and low frequencies %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% """ hybrid_image = low_frequencies + high_frequencies np.clip(hybrid_image, 0, 1) #%% Visualize and save outputs # ax1 = fig.add_subplot(gs[0, 0]) # row 0, col 0 # ax1.imshow(low_frequencies) # ax2 = fig.add_subplot(gs[0, 1]) # row 0, col 1 # ax2.imshow(np.clip(high_frequencies + 0.5,0,1)) vis = vis_hybrid_image( hybrid_image) #see function script vis_hybrid_image.py np.clip(vis, 0, 1) plt.imshow(vis) plt.title(cutoff_freq) # mpimg.imsave('../Results/low_frequencies.jpg',low_frequencies) # mpimg.imsave('../Results/high_frequencies.jpg',np.clip(high_frequencies + 0.5,0,1)) # mpimg.imsave('../Results/hybrid_image.jpg',hybrid_image) mpimg.imsave('../Results/hybrid_image_scales.jpg', np.clip(vis, 0, 1)) plt.show()
def blur(): blur_filter = np.array(([1, 1, 1], [1, 1, 1], [1, 1, 1]), dtype="float32") blur_filter = blur_filter / np.sum(blur_filter) blur_image = my_imfilter(test_image, blur_filter) luo_imshow("Figure 3", blur_image) luo_imwrite(blur_image, 'blur_image.jpg') return blur_image
def gaussian(): global image_cache cutoff_frequency = 2 gaussian_filter = gauss2D(shape=(cutoff_frequency * 4 + 1, cutoff_frequency * 4 + 1), sigma=cutoff_frequency) image_cache = my_imfilter(image_cache, gaussian_filter) show(image_cache)
def laplacian(): laplacian_filter = np.array(([0, 1, 0], [1, -4, 1], [0, 1, 0]), dtype="float32") laplacian_image = my_imfilter(test_image, laplacian_filter) # 0.5 added because the output image is centered around zero otherwise and mostly black luo_imshow("Figure 6", laplacian_image + 0.5) luo_imwrite(laplacian_image + 0.5, "laplacian_image.jpg") return laplacian_image
def sobel(): sobel_filter = np.array(([-1, 0, 1], [-2, 0, 2], [-1, 0, 1]), dtype="float32") sobel_image = my_imfilter(test_image, sobel_filter) # 0.5 added because the output image is centered around zero otherwise and mostly black luo_imshow("Figure 5", sobel_image + 0.5) luo_imwrite(sobel_image + 0.5, "sobel_image.jpg") return sobel_image
def identity(): identity_filter = np.array( ([0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 1, 0], [0, 0, 0], [0, 0, 0 ], [0, 0, 0]), dtype="float32") identity_image = my_imfilter(test_image, identity_filter) luo_imshow("Figure 2", identity_image) luo_imwrite(identity_image, 'identity_image.jpg') return identity_image
def Sobel(): global im_choose, imtk, lb2, lb7, choice, save_str Reset() sobel_filterx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) sobel_filtery = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) im_sobelx = np.squeeze(my_imfilter(im_choose, sobel_filterx), axis = 2) im_sobely = np.squeeze(my_imfilter(im_choose, sobel_filtery), axis = 2) im_choose = np.sqrt((im_sobelx * im_sobelx) + (im_sobely * im_sobely)) if choice == 1: save_str = 'lena_sobel.bmp' else: save_str = 'babo_sobel.bmp' lb7 = tk.Label(window, text='- Contain x, y dir -', width=20, height=1).place(x=362, y=60) Show_im()
def Denoise(): global im_choose, imtk, lb2, avg_filter, lb7, choice, save_str im_choose = np.squeeze(my_imfilter(im_choose, avg_filter), axis = 2) if choice == 1: save_str = 'lena_degauss.bmp' else: save_str = 'babo_degauss.bmp' lb7 = tk.Label(window, text='- Done denoise -', width=20, height=1).place(x=362, y=60) Show_im()
def Averaging(): global avg_filter, im_choose, imtk, lb2, lb7, choice, save_str Reset() im_choose = np.squeeze(my_imfilter(im_choose, avg_filter), axis = 2) if choice == 1: save_str = 'lena_avg.bmp' else: save_str = 'babo_avg.bmp' lb7 = tk.Label(window, text='- Done averaging -', width=20, height=1).place(x=362, y=60) Show_im()
def Gaussian(): global im_choose, imtk, lb2, lb7, choice, save_str Reset() gauss_filter = gauss2D(shape = (25, 25), sigma = 10) im_choose = np.squeeze(my_imfilter(im_choose, gauss_filter), axis = 2) if choice == 1: save_str = 'lena_gauss.bmp' else: save_str = 'babo_gauss.bmp' lb7 = tk.Label(window, text='- Done Gaussian -', width=20, height=1).place(x=362, y=60) Show_im()
def Laplacian(): global im_choose, imtk, lb2, lb7, choice, save_str Reset() laplacian_filter = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) #laplacian_filter = np.array([[0, 1, 1, 2, 2, 2, 1, 1, 0], # [1, 2, 4, 5, 5, 5, 4, 2, 1], # [1, 4, 5, 3, 0, 3, 5, 4, 1], # [2, 5, 3, -12, -24, -12, 3, 5, 2], # [2, 5, 0, -24, -40, -24, 0, 5, 2], # [2, 5, 3, -12, -24, -12, 3, 5, 2], # [1, 4, 5, 3, 0, 3, 5, 4, 1], # [1, 2, 4, 5, 5, 5, 4, 2, 1], # [0, 1, 1, 2, 2, 2, 1, 1, 0]]) im_choose = np.squeeze(my_imfilter(im_choose, laplacian_filter), axis = 2) im_choose = np.uint8(255 * (im_choose - im_choose.min()) / (im_choose.max() - im_choose.min())) if choice == 1: save_str = 'lena_lapla.bmp' else: save_str = 'babo_lapla.bmp' lb7 = tk.Label(window, text='- Done laplacian -', width=20, height=1).place(x=362, y=60) Show_im()
# remove the low frequencies from another image (by subtracting a blurred # version from the original version). You will want to tune this for every # image pair to get the best results. gaussian_filter = gauss2D(shape=(cutoff_frequency * 4 + 1, cutoff_frequency * 4 + 1), sigma=cutoff_frequency) ######################################################################### # TODO: Use my_imfilter create 'low_frequencies' and # # 'high_frequencies' and then combine them to create 'hybrid_image' # ######################################################################### ######################################################################### # Remove the high frequencies from image1 by blurring it. The amount of # # blur that works best will vary with different image pairs # ######################################################################### low_frequencies = my_imfilter(image1, gaussian_filter) ############################################################################ # Remove the low frequencies from image2. The easiest way to do this is to # # subtract a blurred version of image2 from the original version of image2.# # This will give you an image centered at zero with negative values. # ############################################################################ pulse = np.zeros_like(gaussian_filter) pulse[cutoff_frequency * 2 + 1, cutoff_frequency * 2 + 1] = 1 # high_frequencies = np.clip(my_imfilter(image2, pulse - gaussian_filter), -0.5 ,0.5) high_frequencies = my_imfilter(image2, pulse - gaussian_filter) ############################################################################ # Combine the high frequencies and low frequencies # ############################################################################ hybrid_image = normalize(high_frequencies + low_frequencies)
def main(): """ function to create hybrid images """ # read images and convert to floating point format main_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) image1 = mpimg.imread(os.path.join(main_path, 'data', 'fish.bmp')) image2 = mpimg.imread(os.path.join(main_path, 'data', 'submarine.bmp')) image1 = image1.astype(np.float32) / 255 image2 = image2.astype(np.float32) / 255 # Several additional test cases are provided for you, but feel free to make # your own (you'll need to align the images in a photo editor such as # Photoshop). The hybrid images will differ depending on which image you # assign as image1 (which will provide the low frequencies) and which image # you asign as image2 (which will provide the high frequencies) ### Filtering and Hybrid Image construction ### cutoff_frequency = 7 # This is the standard deviation, in pixels, of the # Gaussian blur that will remove the high frequencies from one image and # remove the low frequencies from another image (by subtracting a blurred # version from the original version). You will want to tune this for every # image pair to get the best results. gaussian_filter = gauss2D(shape=(cutoff_frequency * 4 + 1, cutoff_frequency * 4 + 1), sigma=cutoff_frequency) ######################################################################### # TODO: Use my_imfilter create 'low_frequencies' and # # 'high_frequencies' and then combine them to create 'hybrid_image' # ######################################################################### ######################################################################### # Remove the high frequencies from image1 by blurring it. The amount of # # blur that works best will vary with different image pairs # ######################################################################### low_frequencies = my_imfilter(image1, gaussian_filter) ############################################################################ # Remove the low frequencies from image2. The easiest way to do this is to # # subtract a blurred version of image2 from the original version of image2.# # This will give you an image centered at zero with negative values. # ############################################################################ high_frequencies = image2 - my_imfilter(image2, gaussian_filter) ############################################################################ # Combine the high frequencies and low frequencies # ############################################################################ hybrid_image = high_frequencies + low_frequencies ### Visualize and save outputs ### plt.figure(1) plt.imshow(low_frequencies) plt.figure(2) plt.imshow(high_frequencies + 0.5) vis = vis_hybrid_image(hybrid_image) plt.figure(3) plt.imshow(vis) plt.imsave(os.path.join(main_path, 'results', 'fish_frequencies.png'), low_frequencies, dpi=95) plt.imsave(os.path.join(main_path, 'results', 'submarine_high_frequencies.png'), high_frequencies + 0.5, dpi=95) plt.imsave(os.path.join(main_path, 'results', 'fish_submarine_hybrid_image.png'), hybrid_image, dpi=95) plt.imsave(os.path.join(main_path, 'results', 'fish_submarine_hybrid_image_scales.png'), vis, dpi=95) plt.show()
sigma=cutoff_frequency_4) gaussian_filter_5 = gauss2D(shape=(cutoff_frequency_5 * 4 + 1, cutoff_frequency_5 * 4 + 1), sigma=cutoff_frequency_5) gaussian_filter_6 = gauss2D(shape=(cutoff_frequency_6 * 4 + 1, cutoff_frequency_6 * 4 + 1), sigma=cutoff_frequency_6) ######################################################################### # TODO: Use my_imfilter create 'low_frequencies' and # # 'high_frequencies' and then combine them to create 'hybrid_image' # ######################################################################### ######################################################################### # Remove the high frequencies from image1 by blurring it. The amount of # # blur that works best will vary with different image pairs # ######################################################################### low_frequencies = my_imfilter(image1, gaussian_filter) low_frequencies_2 = my_imfilter(image3, gaussian_filter_2) low_frequencies_3 = my_imfilter(image5, gaussian_filter_3) low_frequencies_4 = my_imfilter(image7, gaussian_filter_4) low_frequencies_5 = my_imfilter(image9, gaussian_filter_5) low_frequencies_6 = my_imfilter(image11, gaussian_filter_6) ############################################################################ # Remove the low frequencies from image2. The easiest way to do this is to # # subtract a blurred version of image2 from the original version of image2.# # This will give you an image centered at zero with negative values. # ############################################################################ high_frequencies = image2 - my_imfilter(image2, gaussian_filter) high_frequencies_2 = image4 - my_imfilter(image4, gaussian_filter_2) high_frequencies_3 = image6 - my_imfilter(image6, gaussian_filter_3) high_frequencies_4 = image8 - my_imfilter(image8, gaussian_filter_4) high_frequencies_5 = image10 - my_imfilter(image10, gaussian_filter_5)
# remove the low frequencies from another image (by subtracting a blurred # version from the original version). You will want to tune this for every # image pair to get the best results. gaussian_filter = gauss2D(shape=(cutoff_frequency * 4 + 1, cutoff_frequency * 4 + 1), sigma=cutoff_frequency) ######################################################################### # TODO: Use my_imfilter create 'low_frequencies' and # # 'high_frequencies' and then combine them to create 'hybrid_image' # ######################################################################### ######################################################################### # Remove the high frequencies from image1 by blurring it. The amount of # # blur that works best will vary with different image pairs # ######################################################################### low_frequencies = my_imfilter(image1, gaussian_filter) ############################################################################ # Remove the low frequencies from image2. The easiest way to do this is to # # subtract a blurred version of image2 from the original version of image2.# # This will give you an image centered at zero with negative values. # ############################################################################ high_frequencies = image2 - my_imfilter(image2, gaussian_filter) ############################################################################ # Combine the high frequencies and low frequencies # ############################################################################ hybrid_image = normalize(low_frequencies + high_frequencies) ''' Visualize and save outputs ''' plt.figure(1) plt.imshow(low_frequencies)
def averag(): global image_cache kernel = np.full((3, 3), 1 / 9) image_cache = my_imfilter(image_cache, kernel) show(image_cache)
plt.imshow(test_image) def gaussian_filter(shape =(5,5), sigma=1): x, y = [ int(edge /2) for edge in shape] grid = np.array([[((i**2+j**2)/(2.0*sigma**2)) for i in range(-x, x+1)] for j in range(-y, y+1)]) g_filter = np.exp(-grid)/(2*np.pi*sigma**2) g_filter /= np.sum(g_filter) return g_filter #%% This filter should do nothing regardless of the padding method you use. """ Identity filter """ identity_filter = np.asarray([[0,0,0],[0,1,0],[0,0,0]]) identity_image = my_imfilter(test_image, identity_filter) plt.figure(2) plt.imshow(identity_image) mpimg.imsave('../Results/identity_image.jpg',identity_image) # #%% This filter should remove some high frequencies """ Small blur with a box filter """ blur_filter = np.asarray([[1,1,1],[1,1,1],[1,1,1]]) blur_filter = blur_filter / np.sum(blur_filter); # making the filter sum to 1 # blur_image = my_imfilter(test_image, blur_filter) # plt.figure(3)
filter = filter / np.sum(filter) filter_transpose = np.transpose(filter) """ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % YOUR CODE BELOW. Use my_imfilter to create 'low_frequencies' and % 'high_frequencies' and then combine them to create 'hybrid_image' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Remove the high frequencies from image1 by blurring it. The amount of % blur that works best will vary with different image pairs %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% """ #low_frequencies = low_frequencies = my_imfilter(image1, filter) low_frequencies = my_imfilter(low_frequencies, filter_transpose) """ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Remove the low frequencies from image2. The easiest way to do this is to % subtract a blurred version of image2 from the original version of image2. % This will give you an image centered at zero with negative values. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% """ #high_frequencies = low_frequencies_2 = my_imfilter(image2, filter) low_frequencies_2 = my_imfilter(low_frequencies_2, filter_transpose) high_frequencies = image2 - low_frequencies_2 """ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
import numpy as np import matplotlib.image as mpimg import matplotlib.pyplot as plt import os ''' set up ''' main_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) img_path = main_path + '/data/cat.bmp' test_image = mpimg.imread(img_path) test_image = imresize(test_image, 0.7, 'bilinear') test_image = test_image.astype(np.single) / 255 plt.figure('Image') plt.imshow(test_image) ''' Identify filter ''' # This filter should do nothing regardless of the padding method you use. identity_filter = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]], dtype=np.single) identity_image = my_imfilter(test_image, identity_filter) plt.figure('Identity') plt.imshow(identity_image) ''' Small blur with a box filter ''' # This filter should remove some high frequencies blur_filter = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) blur_filter = blur_filter.astype(np.float) / np.sum(blur_filter) # making the filter sum to 1 blur_image = my_imfilter(test_image, blur_filter) plt.figure('Box filter') plt.imshow(normalize(blur_image)) plt.imsave(main_path + '/results/blur_image.png', normalize(blur_image + 0.5), 'quality', 95) ''' Large blur ''' #This blur would be slow to do directly, so we instead use the fact that #Gaussian blurs are separable and blur sequentially in each direction.
# Gaussian blur that will remove the high frequencies from one image and # remove the low frequencies from another image (by subtracting a blurred # version from the original version). You will want to tune this for every # image pair to get the best results. gaussian_filter = gauss2D(shape=(cutoff_frequency*4+1,cutoff_frequency*4+1), sigma = cutoff_frequency) ######################################################################### # TODO: Use my_imfilter create 'low_frequencies' and # # 'high_frequencies' and then combine them to create 'hybrid_image' # ######################################################################### ######################################################################### # Remove the high frequencies from image1 by blurring it. The amount of # # blur that works best will vary with different image pairs # ######################################################################### low_frequencies = my_imfilter(image1, gaussian_filter); low_frequencies = normalize(low_frequencies) ############################################################################ # Remove the low frequencies from image2. The easiest way to do this is to # # subtract a blurred version of image2 from the original version of image2.# # This will give you an image centered at zero with negative values. # ############################################################################ #high_frequencies = ''' High pass filter (Discrete Laplacian) ''' low_frequencies_2 = my_imfilter(image2, gaussian_filter); alpha = 0.7 high_frequencies = image2 - alpha * low_frequencies_2 high_frequencies = normalize(high_frequencies) ############################################################################
def laplacian(): global image_cache kernel = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]]) image_cache = image_cache + my_imfilter(image_cache, kernel) show(image_cache)
def main(): """function to helps debug your image filtering algorithm. """ # NOTE: __file__ has some problems in jupyter notebook main_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) img_path = os.path.join(main_path, 'data', 'cat.bmp') #main_path = '/Users/jimmyyuan/JimmyYuan/Computer Vision/homework-1' #img_path = '/Users/jimmyyuan/JimmyYuan/Computer Vision/homework-1/data/cat.bmp' test_image = mpimg.imread(img_path) test_image = imresize(test_image, 0.7, interp='bilinear') test_image = test_image.astype(np.float32) / 255 plt.figure('Image') plt.title('Image') plt.imshow(test_image) ### Identify filter ### # This filter should do nothing regardless of the padding method you use. identity_filter = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]], dtype=np.float32) identity_image = my_imfilter(test_image, identity_filter) plt.figure('Identity') plt.title('Identity') plt.imshow(identity_image) ### Small blur with a box filter ### # This filter should remove some high frequencies blur_filter = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) # making the filter sum to 1 blur_filter = blur_filter.astype(np.float) / np.sum(blur_filter) blur_image = my_imfilter(test_image, blur_filter) plt.figure('Box filter') plt.title('Box filter') plt.imshow(normalize(blur_image)) plt.imsave(os.path.join(main_path, 'results', 'blur_image.png'), normalize(blur_image + 0.5), dpi=95) ### Large blur ### #This blur would be slow to do directly, so we instead use the fact that #Gaussian blurs are separable and blur sequentially in each direction. large_2d_blur_filter = gauss2D(shape=(25, 25), sigma=10) large_blur_image = my_imfilter(test_image, large_2d_blur_filter) plt.figure('Gauss filter') plt.title('Gauss filter') plt.imshow(normalize(large_blur_image)) plt.imsave(os.path.join(main_path, 'results', 'large_blur_image.png'), normalize(large_blur_image + 0.5), dpi=95) ### Oriented filter (Sobel Operator) ### sobel_filter = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) sobel_image = my_imfilter(test_image, sobel_filter) #0.5 added because the output image is centered around zero otherwise and mostly black plt.figure('Sobel filter') plt.title('Sobel filter') plt.imshow(normalize(sobel_image + 0.5)) plt.imsave(os.path.join(main_path, 'results', 'sobel_image.png'), normalize(sobel_image + 0.5), dpi=95) ### High pass filter (Discrete Laplacian) ### laplacian_filter = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) laplacian_image = my_imfilter(test_image, laplacian_filter) #0.5 added because the output image is centered around zero otherwise and mostly black plt.figure('Laplacian filter') plt.title('Laplacian filter') plt.imshow(normalize(laplacian_image + 0.5)) plt.imsave(os.path.join(main_path, 'results', 'laplacian_image.png'), normalize(laplacian_image + 0.5), dpi=95) ### High pass "filter" alternative ### high_pass_image = test_image - blur_image #simply subtract the low frequency content plt.figure('High pass filter') plt.title('High pass filter') plt.imshow(normalize(high_pass_image + 0.5)) plt.imsave(os.path.join(main_path, 'results', 'high_pass_image.png'), normalize(high_pass_image + 0.5), dpi=95) plt.show()
# remove the low frequencies from another image (by subtracting a blurred # version from the original version). You will want to tune this for every # image pair to get the best results. gaussian_filter = gauss2D(shape=(cutoff_frequency * 4 + 1, cutoff_frequency * 4 + 1), sigma=cutoff_frequency) ######################################################################### # TODO: Use my_imfilter create 'low_frequencies' and # # 'high_frequencies' and then combine them to create 'hybrid_image' # ######################################################################### ######################################################################### # Remove the high frequencies from image1 by blurring it. The amount of # # blur that works best will vary with different image pairs # ######################################################################### low_frequencies = my_imfilter(I_1, gaussian_filter) low_frequencies_1 = my_imfilter(I_3, gaussian_filter) ############################################################################ # Remove the low frequencies from image2. The easiest way to do this is to # # subtract a blurred version of image2 from the original version of image2.# # This will give you an image centered at zero with negative values. # ############################################################################ high_frequencies = I_2 - my_imfilter(I_2, gaussian_filter) high_frequencies_1 = I_4 - my_imfilter(I_4, gaussian_filter) high_frequencies = normalize(high_frequencies) high_frequencies_1 = normalize(high_frequencies_1) ############################################################################ # Combine the high frequencies and low frequencies # ############################################################################ hybrid_image = normalize(low_frequencies + high_frequencies)
#%% Setup test_image = mpimg.imread('../data/cat.bmp') test_image = resize(test_image, (test_image.shape[0] // 2, test_image.shape[1] // 2), anti_aliasing=True) #resizing to speed up testing plt.figure(1) plt.imshow(test_image) #%% This filter should do nothing regardless of the padding method you use. """ Identity filter """ identity_filter = np.asarray([[0, 0, 0], [0, 1, 0], [0, 0, 0]]) identity_image = my_imfilter(test_image, identity_filter) plt.figure(2) plt.imshow(identity_image) mpimg.imsave('../Results/identity_image.jpg', identity_image) # #%% This filter should remove some high frequencies """ Small blur with a box filter """ blur_filter = np.asarray([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) blur_filter = blur_filter / np.sum(blur_filter) # making the filter sum to 1 # blur_image = my_imfilter(test_image, blur_filter) #
# version from the original version). You will want to tune this for every # image pair to get the best results. filter = luo_fspecial(cutoff_frequency * 4 + 1, cutoff_frequency * 4 + 1, cutoff_frequency) ################################################################### # YOUR CODE BELOW. Use my_imfilter create 'low_frequencies' and # 'high_frequencies' and then combine them to create 'hybrid_image' ################################################################### ######################################################################## # Remove the high frequencies from image1 by blurring it. The amount of # blur that works best will vary with different image pairs ######################################################################## low_frequencies = my_imfilter(image1, filter) ######################################################################## # Remove the low frequencies from image2. The easiest way to do this is to # subtract a blurred version of image2 from the original version of image2. # This will give you an image centered at zero with negative values. ######################################################################## high_frequencies = image2 - my_imfilter(image2, filter) ######################################################################## # Combine the high frequencies and low frequencies ######################################################################## hybrid_image = low_frequencies + high_frequencies