3-1/Python 과제&실습

13주차-실습&과제

Donghun Kang 2024. 6. 13. 11:51

 

import matplotlib.pyplot as plt
import numpy as np

def meanFilter(img, filter_size):
    if filter_size % 2 == 0:
        raise ValueError("Filter size must be an odd number.")
    
    pad_size = filter_size // 2
    
    padded_img = np.pad(img, ((pad_size, pad_size), (pad_size, pad_size)), mode='constant', constant_values=0)
    
    filtered_img = np.zeros_like(img)
    
    for i in range(img.shape[0]):
        for j in range(img.shape[1]):
            filtered_img[i, j] = np.mean(padded_img[i:i+filter_size, j:j+filter_size])
    
    return filtered_img

img = plt.imread('cameraman.jpg')

if img.ndim == 3:
    img = np.mean(img, axis=2).astype(np.uint8)

filtered_img_7 = meanFilter(img, 7)
filtered_img_15 = meanFilter(img, 15)

plt.figure(figsize=(15, 5))

plt.subplot(1, 3, 1)
plt.imshow(img, cmap='gray')
plt.axis('off')

plt.subplot(1, 3, 2)
plt.imshow(filtered_img_7, cmap='gray')
plt.axis('off')

plt.subplot(1, 3, 3)
plt.imshow(filtered_img_15, cmap='gray')
plt.axis('off')

plt.show()

 

cameraman.jpg
0.05MB

'3-1 > Python 과제&실습' 카테고리의 다른 글

14주차-실습&과제  (0) 2024.06.13
12주차-실습&과제  (0) 2024.05.31
11주차-실습&과제  (0) 2024.05.21
10주차-실습&과제  (0) 2024.05.21
9주차-실습&과제  (0) 2024.05.21