UOMOP

Low Pass Filtering using cv2 본문

Wireless Comm./CISL

Low Pass Filtering using cv2

Happy PinGu 2023. 7. 11. 13:34
def spectrum(img) :

    f = np.fft.fft2(img)
    fshift = np.fft.fftshift(f)
    magnitude_fshift = np.log(np.abs(fshift) + 1)

    return magnitude_fshift


def average_filter(img, kernel_size) :
    
    return cv2.blur(img, (kernel_size, kernel_size))

def gaussian_filter(img, kernel_size) :

    # cv2.GaussianBlur의 parameter에는 sigma가 존재하는데, 이것은 가우시안 커널의 X, Y 방향의 표준편차이다.
    return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)

def median_filter(img, kernel_size) :

    return cv2.medianBlur(img, kernel_size)

def bilateral_filter(img, kernel_size) :

    return cv2.bilateralFilter(img, kernel_size, 75, 75)
    

def compare_img(img, filter_type, filtering_list) :


    plt.figure(figsize = (20, 30))

    plt.subplot(1, len(filtering_list)+1, 1)
    plt.imshow(img)
    plt.title("Original", fontsize = 10)
    plt.xlabel(img.sum())
    plt.xticks([])
    plt.gca().axes.yaxis.set_visible(False)
    

    for i in range(len(filtering_list)) :
        if filter_type == 'average' :
            img_filtered = average_filter(img, filtering_list[i])
        elif filter_type == 'gaussian' :
            img_filtered = gaussian_filter(img, filtering_list[i])
        elif filter_type == 'median' :
            img_filtered = median_filter(img, filtering_list[i])
        elif filter_type == 'bilateral' :
            img_filtered = bilateral_filter(img, filtering_list[i])
        else :
            print('Type Error!')
        plt.subplot(1, len(filtering_list)+1, i+2)
        plt.imshow(img_filtered)
        plt.title('Average : {}'.format(filtering_list[i]), fontsize = 10)
        plt.xlabel(img_filtered.sum())
        plt.xticks([])
        plt.gca().axes.yaxis.set_visible(False)

    plt.show()


def compare_spec(img, filter_type, filtering_list) :

    plt.figure(figsize = (20, 30))

    plt.subplot(1, len(filtering_list)+1, 1)
    plt.imshow(spectrum(img), cmap = 'gray')
    plt.title('Original', fontsize = 10)
    plt.xlabel(img.sum())
    plt.xticks([])
    plt.gca().axes.yaxis.set_visible(False)

    for i in range(len(filtering_list)) :
        if filter_type == 'average' :
            img_filtered = average_filter(img, filtering_list[i])
        elif filter_type == 'gaussian' :
            img_filtered = gaussian_filter(img, filtering_list[i])
        elif filter_type == 'median' :
            img_filtered = median_filter(img, filtering_list[i])
        elif filter_type == 'bilateral' :
            img_filtered = bilateral_filter(img, filtering_list[i])
        else :
            print('Type Error!')
        freq = spectrum(img_filtered)

        plt.subplot(1, len(filtering_list)+1, i+2)
        plt.imshow(freq, cmap = 'gray')
        plt.title('Average : {}'.format(filtering_list[i]), fontsize = 10)
        plt.xlabel(img_filtered.sum())
        plt.xticks([])
        plt.gca().axes.yaxis.set_visible(False)

    plt.show()​
import cv2
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
import cv2
import matplotlib.pyplot as plt

img = cv2.imread("lena.png", cv2.IMREAD_COLOR)
b, g, r = cv2.split(img)   # img파일을 b,g,r로 분리
img_color = cv2.merge([r,g,b])   # b, r을 바꿔서 Merge

img_gray = cv2.imread("lena.png", cv2.IMREAD_GRAYSCALE)

plt.figure(figsize = (8, 8))

plt.subplot(1, 2, 1)
plt.imshow(img_gray, cmap = 'gray')
plt.title("img_gray")

plt.subplot(1, 2, 2)
plt.imshow(img_color)
plt.title("img_color")

plt.show()

 

filtering_list = list(range(7, 90, 10))
print('filtering_list : {}'.format(filtering_list))

Averaging Filter

Gaussian Filter

Median Filter

Bilateral Filter

'Wireless Comm. > CISL' 카테고리의 다른 글

Cifar10 AWGN [1dB, 10dB, 20dB]  (0) 2023.07.18
Cifar10 Rayleigh [1dB 10dB 20dB]  (0) 2023.07.11
Cifar10 Rayleigh with SSIM  (0) 2023.07.10
Cifar10 Autoencoder 20230710 Rayleigh  (0) 2023.07.10
Basic AutoEncoder using Cifar10  (0) 2023.06.10
Comments