Exemple #1
0
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)
Exemple #2
0
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)
Exemple #3
0
def compute_LoG(image, LOG_type):

    imOut = np.zeros(shape=image.shape)

    if LOG_type == 1:
        gaussian = signal.convolve2d(image,gauss2D(0.5,5), mode="same")
        imOut = cv2.Laplacian(gaussian, ddepth=-1)
        plt.imshow(imOut,cmap='gray')
        plt.title('Method 1: Smoothing by Gaussian followed by Laplacian')
        plt.axis('off')
        #plt.show()
        plt.savefig('./log_results/method_1.pdf')

    elif LOG_type == 2:
        # computing the LoG kernel from scratch
        LoG_exact = compute_LoG_kernel(5,0.5)
        
        # take LoG kernel from internet
        #Log_internet = $$$
        
        # deprecated, not correct
        #LoG = signal.convolve2d(gauss2D(0.5,5),np.array([[0,1,0],[1,-4,1],[0,1,0]]),mode="same")
        
        # compute for exact kernel
        imOut = signal.convolve2d(image,LoG_exact, mode="same")
        # compute for internet kernel
        #imOut = signal.convolve2d(image,LoG_internet, mode="same")
        
        plt.imshow(imOut,cmap='gray')
        plt.title('Method 2: Directly with LoG kernel')
        plt.axis('off')
        plt.savefig('./log_results/method_2.pdf')
        #plt.show()

    elif LOG_type == 3:
        DoG = gauss2D(0.5*np.sqrt(2),5)-gauss2D(0.5/np.sqrt(2),5)
        imOut = signal.convolve2d(image,DoG,mode="same")
        plt.imshow(imOut,cmap='gray')
        plt.title('Method 3: Difference of two Gaussians (DoG)')
        plt.axis('off')
        plt.savefig('./log_results/method_3.pdf')
        #plt.show()


    return imOut
Exemple #4
0
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()
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.
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.imshow(normalize(large_blur_image))
plt.imsave(main_path + '/results/large_blur_image.png',
           normalize(large_blur_image + 0.5), 'quality', 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.imshow(normalize(sobel_image + 0.5))
plt.imsave(main_path + '/results/sobel_image.png',
           normalize(sobel_image + 0.5), 'quality', 95)
''' High pass filter (Discrete Laplacian) '''
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()
Exemple #7
0
image1 = image1.astype(np.single) / 255
image2 = image2.astype(np.single) / 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 = 4  # 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.#
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()
# 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
cutoff_frequency_2 = 4
cutoff_frequency_3 = 6
cutoff_frequency_4 = 5.5
cutoff_frequency_5 = 5.5
cutoff_frequency_6 = 4.5
# 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)
gaussian_filter_2 = gauss2D(shape=(cutoff_frequency_2 * 4 + 1,
                                   cutoff_frequency_2 * 4 + 1),
                            sigma=cutoff_frequency_2)
gaussian_filter_3 = gauss2D(shape=(cutoff_frequency_3 * 4 + 1,
                                   cutoff_frequency_3 * 4 + 1),
                            sigma=cutoff_frequency_3)
gaussian_filter_4 = gauss2D(shape=(cutoff_frequency_4 * 4 + 1,
                                   cutoff_frequency_4 * 4 + 1),
                            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),