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
반응형
'즐거운프로그래밍' 카테고리의 다른 글
[딥러닝] 3x3x2 입력 이미지의 합성곱 (0) | 2023.11.01 |
---|---|
[딥러닝] 필터 역할 살펴보기 - 연습문제 (0) | 2023.11.01 |
[딥러닝] 필터 역할 살펴보기(컬러이미지로 적용하기) (0) | 2023.11.01 |
[딥러닝] 필터 역할 살펴보기(부드러운 이미지 추출하기) (0) | 2023.10.31 |
[딥러닝] 알고리즘 CNN신경망 입력 필터 늘리기 (0) | 2023.10.31 |