즐거운프로그래밍

[딥러닝] 필터 역할 살펴보기(다양한 이미지 출력)

수수께끼 고양이 2023. 11. 1. 11:23
728x90
반응형

 

선명한 이미지 추출하기

import numpy as np
import cv2
import matplotlib.pyplot as plt

image_color=cv2.imread('../images/cat.jpg')
print('image_color.shape =',image_color.shape)
image=cv2.cvtColor(image_color,cv2.COLOR_BGR2GRAY)
print('image.shape =',image.shape)

filter=np.array([
    [-1,-1,-1],
    [-1,9,-1],
    [-1,-1,-1]
])

image_pad=np.pad(image,((5,5),(5,5)))
print('image_pad.shape =', image_pad.shape)

convolution=np.zeros_like(image)

for row in range(image.shape[0]):
    for col in range(image.shape[1]):
        window=image_pad[row:row+3, col:col+3]
        convolution[row,col]=np.clip(np.sum(window*filter),0,255)
        
image=[image, convolution]
label=['gray','convolution']

plt.figure(figsize=(10,5))
for i in range(len(image)):
    plt.subplot(1,2,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.imshow(image[i],cmap=plt.cm.gray)
    plt.xlabel(label[i])
plt.show()

 

 

 


경계선 추출하기

import numpy as np
import cv2
import matplotlib.pyplot as plt

image_color=cv2.imread('../images/cat.jpg')
print('image_color.shape =',image_color.shape)
image=cv2.cvtColor(image_color,cv2.COLOR_BGR2GRAY)
print('image.shape =',image.shape)

filter=np.array([
    [-1,-1,-1],
    [-1,8,-1],
    [-1,-1,-1]
])

image_pad=np.pad(image,((5,5),(5,5)))
print('image_pad.shape =', image_pad.shape)

convolution=np.zeros_like(image)

for row in range(image.shape[0]):
    for col in range(image.shape[1]):
        window=image_pad[row:row+3, col:col+3]
        convolution[row,col]=np.clip(np.sum(window*filter),0,255)
        
image=[image, convolution]
label=['gray','convolution']

plt.figure(figsize=(10,5))
for i in range(len(image)):
    plt.subplot(1,2,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.imshow(image[i],cmap=plt.cm.gray)
    plt.xlabel(label[i])
plt.show()

 

 

 


이미지 단순화하기

import numpy as np
import cv2
import matplotlib.pyplot as plt

image_color=cv2.imread('../images/cat.jpg')
print('image_color.shape =',image_color.shape)
image=cv2.cvtColor(image_color,cv2.COLOR_BGR2GRAY)
print('image.shape =',image.shape)

filter=np.array([
    [-1,-1,-1],
    [-1,8,-1],
    [-1,-1,-1]
])

image_pad=np.pad(image,((1,1),(1,1)))
print('image_pad.shape =', image_pad.shape)

convolution=np.zeros_like(image)

for row in range(image.shape[0]):
    for col in range(image.shape[1]):
        window=image_pad[row:row+3, col:col+3]
        convolution[row,col]=np.clip(np.sum(window*filter),0,255)
        
max_pooled=np.zeros((int(image.shape[0]/2),int(image.shape[1]/2)))

for row in range(0,int(image.shape[0]/2)):
    for col in range(0, int(image.shape[1]/2)):
        window=convolution[2*row:2*row+2, 2*col:2*col+2]
        max_pooled[row,col]=np.max(window)
        
image=[image, convolution, max_pooled]
label=['gray','convolution', 'max_pooled']

plt.figure(figsize=(15,5))
for i in range(len(image)):
    plt.subplot(1,3,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.imshow(image[i],cmap=plt.cm.gray)
    plt.xlabel(label[i])
plt.show()

 

 

 

 

 

 

728x90
반응형