Commit 2f8fb846 authored by 袁通's avatar 袁通
Browse files

update LAB6

parent 75872394
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -5,4 +5,3 @@ from scipy import interpolate
import matplotlib.pyplot as plt

if __name__ == '__main__':
 No newline at end of file
+2 −2
Original line number Diff line number Diff line
@@ -21,8 +21,8 @@ def butterworth_notch_filters_11810818(input_image):
                  show_image,
                  cmap=cm.gray)

    for sigma in [10, 30, 60, 90, 120, 160]:
        for n in [1, 2, 3]:
    for sigma in [10]:
        for n in [1]:
            centers = [
                [109, 87],
                [109, 170],
+186 −0
Original line number Diff line number Diff line
'''
Library USE for EE326 2021
'''

import numpy as np
from skimage import io, data
import math
from scipy import interpolate
import matplotlib.pyplot as plt


# General
def format_image(input_image):
    output_image = input_image
    output_image -= np.min(output_image)
    output_image = (output_image/np.max(output_image))*255
    return output_image

# LAB 4

def convolution_3x3(input_image, operator_3x3):
    col, row = input_image.shape
    output_image = np.zeros([col, row])
    input_image = np.pad(input_image, 1)
    for i in range(0, col):
        for j in range(0, row):
            for i2 in range(3):
                for j2 in range(3):
                    output_image[i, j] += input_image[i+i2, j+j2] * operator_3x3[i2, j2]

    return output_image


def sobel_filter(input_image):

    operator1 = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])
    operator2 = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])

    output_image1 = convolution_3x3(input_image, operator1)
    output_image2 = convolution_3x3(input_image, operator2)
    #
    # output_image1 = np.clip(output_image1, 0, 255)
    # output_image2 = np.clip(output_image2, 0, 255)

    output_image = output_image1 + output_image2 # + input_image
    # output_image = np.clip(output_image, 0, 255)

    output_image = output_image.astype(np.uint8)

    return output_image


def zero_padding(input_image, P, Q):
    output_image = np.zeros([P, Q])

    return output_image


def denoise_filter(input_image, n_size, mode):
    output_image = np.zeros(input_image.shape, dtype=np.uint8)

    m, n = input_image.shape

    for i in range(m):
        for j in range(n):
            step = (int)((n_size - 1) / 2)
            pixels = np.zeros(n_size * n_size)

            for i2 in range(n_size):
                for j2 in range(n_size):
                    if i - step + i2 >= 0 \
                            and i - step + i2 < input_image.shape[0] \
                            and j - step + j2 >= 0 \
                            and j - step + j2 < input_image.shape[0]:
                        pixels[j2 * n_size + i2] = input_image[i - step + i2, j - step + j2]

            pixels = np.sort(pixels)

            if(mode == "max"):
                output_image[i, j] = pixels[pixels.shape[0]-1]
            elif(mode == "medium"):
                output_image[i, j] = pixels[(int)((n_size * n_size + 1) / 2)]
            elif(mode == "min"):
                output_image[i, j] = pixels[0]
            elif(mode == "average"):
                output_image[i, j] = np.average(pixels)
            elif(mode == "smart"):
                have_normal_pixel = 0
                for pixel in pixels:
                    if(pixel < 250 and pixel > 5):
                        have_normal_pixel = 1
                if(have_normal_pixel):
                    selected = (int)(pixels.shape[0]/2)
                    while(pixels[selected] < 5):
                        selected = selected + 1
                    while(pixels[selected] > 250):
                        selected = selected - 1
                    output_image[i, j] = pixels[selected]
                else:
                    output_image[i, j] = np.average(pixels)



    return output_image

# LAB 5


def extract_result_eastsouth(input_image):
    x, y = input_image.shape
    output_image = input_image[int(x/2):x, int(y/2):y]

    return output_image


def extract_result_westnorth(input_image):
    x, y = input_image.shape
    output_image = input_image[0:int(x/2), 0:int(y/2)]

    return output_image


def zero_padding_DFT(input_image, P, Q):
    m, n = input_image.shape

    output_image = np.zeros([P, Q])
    output_image[0:m, 0:n] = input_image

    return output_image


def zero_padding_DFT(input_image):
    m,n = input_image.shape

    P = 2*m
    Q = 2*n

    output_image = np.zeros([P, Q])
    output_image[0:m, 0:n] = input_image

    return output_image


def centering(size):
    m, n = size
    centering_matrix = np.ones(size)
    mul1 = 1
    for i in range(m):
        mul2 = mul1
        for j in range(n):
            centering_matrix[i, j] = centering_matrix[i, j] * mul2
            mul2 *= -1
        mul1 *= -1
    return centering_matrix


def generating_from_spatial_filter(input_filter, P, Q):
    output_filter = np.zeros(P, Q)

    return output_filter


def gaussian_filter(a, b, sigma):
    x, y = np.meshgrid(np.linspace(0, a-1, a), np.linspace(0, b-1, b))
    x = x - a/2
    y = y - b/2
    d = x * x + y * y
    g = np.exp(-(d / (2.0 * sigma ** 2)))
    # g = g/np.sum(g)
    return g


def butterworth_filter(b, a, center, n, sigma):
    cx, cy = center
    x, y = np.meshgrid(np.linspace(0, a - 1, a), np.linspace(0, b - 1, b))
    x = x - cx
    y = y - cy
    d = np.sqrt(x * x + y * y)
    h = 1/((1+(d/sigma))**(2*n))
    return h


# LAB 6


+103 KiB

File added.

No diff preview for this file type.

+203 KiB
Loading image diff...
Loading