Commit c8f88061 authored by Sparkf's avatar Sparkf 🏙️
Browse files

lab5 submit

parent 11c932ff
Loading
Loading
Loading
Loading
+28 −13
Changes for lab5/butterworse_notch.py: 28 added lines, 13 removed lines.
Original line number Diff line number Diff line
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import time
import lab5_lib


def butterworse_pass_11812418(input_img, sigma):
def butterworse_pass_11812418(input_img, sigma, n):
    # Reading of the image into numpy array:
    img = Image.open(input_img)
    # FFT transform for img arr
@@ -19,15 +18,25 @@ def butterworse_pass_11812418(input_img, sigma):
    # 492,336

    # define butterworth
    butterworth_filter = 1-lab5_lib.generate_butterworth(40, 40, 2, sigma)
    bf_x = [70,150,310,395]
    bf_y = [90,210]
    butterworth_filter = np.ones([row, col])
    # = 1-lab5_lib.generate_butterworth(40, 40, 2, sigma)
    centers = [
        [109, 87],
        [109, 170],
        [115, 330],
        [115, 412],
        [227, 405],
        [227, 325],
        [223, 162],
        [223, 79]
    ]
    for point in centers:
        butterworth_filter = butterworth_filter - lab5_lib.generate_butterworth(row, col, n, sigma, point[0], point[1])

    for x in bf_x:
        for y in bf_y:
            input_image[x:x+40,y:y+40] = np.multiply(input_image[x:x+40,y:y+40],butterworth_filter)
    plt.imshow(np.log(np.abs(input_image)))
    plt.show()
    input_image = np.multiply(input_image, butterworth_filter)

    # plt.imshow(np.log(np.abs(input_image)))
    # plt.show()

    # plt.imshow(input_image)
    # plt.show()
@@ -39,12 +48,18 @@ def butterworse_pass_11812418(input_img, sigma):
    # plt.imshow(np.real(output_img))
    # plt.show()

    return np.abs(output_img)
    return np.abs(output_img), np.log(np.abs(input_image))


if __name__ == '__main__':
    start_time = time.time()
    A = butterworse_pass_11812418("Q5_3.tif", 100)
    for sigma in [10, 40, 80, 120]:
        for n in [1, 2, 3, 4]:
            A, A_S = butterworse_pass_11812418("Q5_3.tif", sigma, n)
            op_image = Image.fromarray(A.astype(np.uint8))
            op_image.save("output/Q5_3_M_S" + str(sigma) + "_N" + str(n) + ".png")
            # plt.imshow(A_S)
            # plt.axis('off')
            # plt.savefig("output/Q5_3_M_F" + str(sigma) + "_N" + str(n) + ".png", bbox_inches='tight', pad_inches=0,
            #             dpi=150)
    print("--- %s seconds ---" % (time.time() - start_time))
    op_image.save("output/Q5_3_M.tif")
+31 −26
Changes for lab5/gaussian.py: 31 added lines, 26 removed lines.
Original line number Diff line number Diff line
import matplotlib.pyplot as plt
import time
import numpy as np
from PIL import Image
from joblib import Parallel, delayed
import time
import lab5_lib


@@ -19,39 +17,46 @@ def gaussian_pass_11812418(input_img, sigma):
    input_image = np.fft.fft2(input_image)
    input_image = np.fft.fftshift(input_image)
    # define gaussian
    gaussian_filter = lab5_lib.generate_gaussian(row, col, sigma)
    gaussian_filter_HP = lab5_lib.generate_gaussian(row, col, sigma)
    gaussian_filter_LP = np.ones([row, col]) - lab5_lib.generate_gaussian(row, col, sigma)
    # print(gaussian_filter)
    # plt.imshow(gaussian_filter)
    # plt.show()
    filtered_img = np.multiply(input_image, gaussian_filter)
    output_img = np.fft.ifft2(np.fft.ifftshift(filtered_img))
    output_img = lab5_lib.transform_centering(output_img)
    # print(np.min(np.abs(output_img)))
    # output_img = lab5_lib.normalize(output_img)
    # plt.imshow(np.real(output_img))
    filtered_img_HP = np.multiply(input_image, gaussian_filter_HP)
    output_img_HP = np.fft.ifft2(np.fft.ifftshift(filtered_img_HP))
    output_img_HP = lab5_lib.transform_centering(output_img_HP)
    filtered_img_LP = np.multiply(input_image, gaussian_filter_LP)
    output_img_LP = np.fft.ifft2(np.fft.ifftshift(filtered_img_LP))
    output_img_LP = lab5_lib.transform_centering(output_img_LP)
    # print(np.min(np.abs(output_img_HP)))
    # output_img_HP = lab5_lib.normalize(output_img_HP)
    # plt.imshow(np.real(output_img_HP))
    # plt.show()

    return np.abs(output_img), np.abs(input_image_origin-output_img)
    return np.abs(output_img_HP), np.abs(output_img_LP), lab5_lib.normalize(
        gaussian_filter_LP) * 255, lab5_lib.normalize(gaussian_filter_HP) * 255


if __name__ == '__main__':
    start_time = time.time()
    A_LP,A_HP = gaussian_pass_11812418("Q5_2.tif", 30)
    for i in [30, 60, 160]:
        A_LP, A_HP, G_HP, G_LP = gaussian_pass_11812418("Q5_2.tif", i)
        op_image_LP = Image.fromarray(A_LP.astype(np.uint8))
    op_image_LP.save("output/Q5_2_LP30.tif")
        op_image_LP.save("output/Q5_2_LP" + str(i) + ".tif")
        op_image_HP = Image.fromarray(A_HP.astype(np.uint8))
    op_image_HP.save("output/Q5_2_HP30.tif")
    A_LP,A_HP = gaussian_pass_11812418("Q5_2.tif", 60)
    op_image_LP = Image.fromarray(A_LP.astype(np.uint8))
    op_image_LP.save("output/Q5_2_LP60.tif")
    op_image_HP = Image.fromarray(A_HP.astype(np.uint8))
    op_image_HP.save("output/Q5_2_HP60.tif")
    A_LP,A_HP = gaussian_pass_11812418("Q5_2.tif", 160)
        op_image_HP.save("output/Q5_2_HP" + str(i) + ".tif")
        op_image_GL = Image.fromarray(G_LP.astype(np.uint8))
        op_image_GL.save("output/Q5_2_LP" + str(i) + "_F.tif")
        op_image_GH = Image.fromarray(G_HP.astype(np.uint8))
        op_image_GH.save("output/Q5_2_HP" + str(i) + "_F.tif")
    print("--- %s seconds ---" % (time.time() - start_time))
    for i in [30, 60, 160]:
        A_LP, A_HP, G_HP, G_LP = gaussian_pass_11812418("Q5_2.tif", i)
        op_image_LP = Image.fromarray(A_LP.astype(np.uint8))
    op_image_LP.save("output/Q5_2_LP160.tif")
        op_image_LP.save("output/Q5_2_LP" + str(i) + ".png")
        op_image_HP = Image.fromarray(A_HP.astype(np.uint8))
    op_image_HP.save("output/Q5_2_HP160.tif")


    print("--- %s seconds ---" % (time.time() - start_time))
        op_image_HP.save("output/Q5_2_HP" + str(i) + ".png")
        op_image_GL = Image.fromarray(G_LP.astype(np.uint8))
        op_image_GL.save("output/Q5_2_LP" + str(i) + "_F.png")
        op_image_GH = Image.fromarray(G_HP.astype(np.uint8))
        op_image_GH.save("output/Q5_2_HP" + str(i) + "_F.png")
+4 −3
Changes for lab5/lab5_lib.py: 4 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ def transform_centering(input_img):
def DFT_kernel(input_image_padded, kernel):
    sz = (input_image_padded.shape[0] - kernel.shape[0], input_image_padded.shape[1] - kernel.shape[1])
    DFT_kernel_1 = np.pad(kernel, (((sz[0] + 1) // 2, sz[0] // 2), ((sz[1] + 1) // 2, sz[1] // 2)), 'constant')
    DFT_kernel_1 = multiply_center(DFT_kernel_1)
    DFT_kernel_1_fft = np.fft.fft2(DFT_kernel_1)
    return DFT_kernel_1_fft

@@ -49,10 +50,10 @@ def generate_gaussian(a, b, sigma):



def generate_butterworth(row, col, n, sigma):
def generate_butterworth(row, col, n, sigma, cr, cc):
    x, y = np.meshgrid(np.linspace(0, col - 1, col), np.linspace(0, row - 1, row))
    x = x - col / 2
    y = y - row / 2
    x = x - cr
    y = y - cc
    d = np.sqrt(x * x + y * y)
    h = 1 / ((1 + (d / sigma)) ** (2 * n))
    return h
+19 −9
Changes for lab5/sobel.py: 19 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -47,15 +47,26 @@ def sobel_11812418(input_img):
    img_arr = lab5_lib.multiply_center(img_arr)

    img_arr_fft = np.fft.fft2(img_arr)
    # plt.imshow(20*np.log(np.abs((np.fft.fft2(img_arr)))))
    # plt.show()

    plt.imshow(20 * np.log(np.abs((np.fft.fft2(img_arr)))))
    heatmap = plt.pcolor(20 * np.log(np.abs((np.fft.fft2(img_arr)))))
    plt.colorbar(heatmap)
    plt.savefig('output/Q5_1_F.png', dpi=600)

    kernel1 = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
    kernel2 = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])
    kernel1 = kernel1 + kernel2

    DFT_kernel_1_fft = lab5_lib.DFT_kernel(img_arr, kernel1)
    DFT_kernel_fft = DFT_kernel_1_fft

    # plt.imshow(np.abs(DFT_kernel_1_fft))
    # heatmap = plt.pcolor(np.abs(DFT_kernel_1_fft))
    # plt.colorbar(heatmap)
    # plt.savefig('output/sobel_kernel.png', dpi=600)

    # K1
    filtered_1 = np.real(np.fft.ifft2(img_arr_fft * DFT_kernel_1_fft))
    filtered_1 = np.real(np.fft.ifft2(img_arr_fft * DFT_kernel_fft))
    filtered_1 = lab5_lib.FFT_extract_padding(filtered_1)
    filtered_1 = lab5_lib.multiply_center(filtered_1)
    # plt.imshow(filtered_1)
@@ -65,14 +76,11 @@ def sobel_11812418(input_img):
    # op_img = np.fft.ifft2(conv(img_arr, kernel_flat))
    # print(filtered_1)
    op_img = lab5_lib.normalize(filtered_1) * 255
    op_img = np.clip(op_img, 115, 145)
    op_img = lab5_lib.normalize(op_img)*255
    print(op_img)

    plt.imshow(op_img)
    plt.show()

    op_img_spacial = conv(input_image_origin,kernel1.flatten())
    op_img_spacial = conv(input_image_origin, np.flip(kernel1.flatten()))
    op_img_spacial = lab5_lib.normalize(op_img_spacial) * 255

    return op_img, op_img_spacial
@@ -83,5 +91,7 @@ A, A_S = sobel_11812418("Q5_1.tif")
op_image = Image.fromarray(A.astype(np.uint8))
op_image_S = Image.fromarray(A_S.astype(np.uint8))
print("--- %s seconds ---" % (time.time() - start_time))
op_image.save("output/Q5_1_M.tif")
op_image_S.save("output/Q5_1_M_S.tif")
op_image.save("output/Q5_1_M_Frequency.tif")
op_image_S.save("output/Q5_1_M_Spatial.tif")
op_image.save("output/Q5_1_M_Frequency.png")
op_image_S.save("output/Q5_1_M_Spatial.png")
+8.2 MiB

File added.

No diff preview for this file type.

Loading