# 1) : 실습 3을 2x2 서브 화면으로 나누고, 4번째 화면에는 양방향(가로/세로)으로 평균필터링한 영상을 출력하시오.
Path = '../data/' # 현재 상위 폴더의 상위 폴더 아래에 있는 Images 폴더.
Name = 'lenna.tif'
FullName = Path + Name
monarch = cv.imread(FullName)
monarch = monarch[..., ::-1]
fig = plt.figure(num = 'mission filter') #,figsize=(8,8)
plt.subplots_adjust(left=0.02, right=0.9, bottom=0.03, top=0.9)
plt.subplots(constrained_layout=True) # 최적의 사이즈로 변환, figsize 옵션이 없어도 창이 자동으로 크기 조절됨
fig.patch.set_facecolor('pink')
fig.patch.set_alpha(0.2)
plt.subplot(2,2,1)
plt.imshow(monarch)
plt.axis('off')
plt.title('Original')
N = 50
kernel_list = [] # 다수의 커널로 list 자료로 기록한다.
# 1) 1xN 커널 정의 - 가로로 averaging 한다.
kernel = np.ones((1, N), np.float32) / (N) # 주의!!! N으로 나누어 커널의 합이 1이 되도록 정규화한다.
print('np.sum(kernel)=', np.sum(kernel)) # 커널의 총합을 확인.
kernel_list.append(kernel)
print(kernel.shape)
# 2) Nx1 커널 정의 - 세로로 averaging 한다.
kernel = np.ones((N, 1), np.float32)
kernel /= np.sum(kernel) # 커널의 합으로 나누어 커널의 합이 1이 되도록 정규화한다.
print('np.sum(kernel)=', np.sum(kernel)) # 커널의 총합을 확인.
kernel_list.append(kernel)
# NXN 커널 정의 - 가로,세로 averaging
kernel=np.ones((N,N),np.float32)
kernel /= np.sum(kernel)
print('np.sum(kernel)= ',np.sum(kernel))
kernel_list.append(kernel)
print(len(kernel_list))
title_list = ["garo",'saero','garo+saero']
for i,knl in enumerate(kernel_list):
dst = cv.filter2D(monarch,-1,knl,borderType=cv.BORDER_DEFAULT)
plt.subplot(222+i)
plt.imshow(dst)
plt.axis('off')
plt.title(title_list[i])
i+=1
plt.waitforbuttonpress()
exit(0)
# ===========================================================================
# 2) : 커널의 크기가 5x5, 15x15, 31x31의 average filtering 결과를 원본 영상과 함께 2x2 화면에 출력하시오
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
kernel_list=[]
Path = '../data/' # 현재 상위 폴더의 상위 폴더 아래에 있는 Images 폴더.
Name = 'lenna.tif'
FullName = Path + Name
monarch = cv.imread(FullName)
monarch = monarch[..., ::-1]
fig = plt.figure(num = 'mission filter') #,figsize=(8,8)
plt.subplots_adjust(left=0.02, right=0.9, bottom=0.03, top=0.9)
plt.subplots(constrained_layout=True) # 최적의 사이즈로 변환, figsize 옵션이 없어도 창이 자동으로 크기 조절됨
fig.patch.set_facecolor('green')
fig.patch.set_alpha(0.1)
plt.subplot(2,2,1)
plt.imshow(monarch)
plt.axis('off')
plt.title('original')
# 1) 5x5
N=5
kernel = np.ones((N,N), np.float32) # 주의!!! N으로 나누어 커널의 합이 1이 되도록 정규화한다.
kernel /= np.sum(kernel)
print('np.sum(kernel)=', np.sum(kernel)) # 커널의 총합을 확인.
kernel_list.append(kernel)
print(kernel.shape)
#2)15x15
N=15
kernel = np.ones((N,N), np.float32) # 주의!!! N으로 나누어 커널의 합이 1이 되도록 정규화한다.
kernel /= np.sum(kernel)
print('np.sum(kernel)=', np.sum(kernel)) # 커널의 총합을 확인.
kernel_list.append(kernel)
print(kernel.shape)
#3)31x31
N=31
kernel = np.ones((N,N), np.float32) # 주의!!! N으로 나누어 커널의 합이 1이 되도록 정규화한다.
kernel /= np.sum(kernel)
print('np.sum(kernel)=', np.sum(kernel)) # 커널의 총합을 확인.
kernel_list.append(kernel)
print(kernel.shape)
title_list =["5X5","15X15","31X31"]
for i,knl in enumerate(kernel_list):
dst = cv.filter2D(monarch,-1,knl,borderType=cv.BORDER_DEFAULT)
plt.subplot(222+i)
plt.imshow(dst)
plt.axis('off')
plt.title(title_list[i])
i+=1
plt.waitforbuttonpress()
exit(0)
'전공 과목 이수2👨💻 > 디지털영상처리' 카테고리의 다른 글
밝기를 트랙바로 조절해보기 (0) | 2021.10.27 |
---|---|
화질 개선 기법(감마, 시그모이드, 히스토그램, 샤프닝, 메디언) (0) | 2021.10.18 |
엣지검출 pyplot.imshow vs cv.imshow (0) | 2021.10.10 |
matplotlib 출력위한 코드 (0) | 2021.10.03 |
커널 / 필터링 (0) | 2021.10.03 |
이미지 위에 마우스로 사각형 그리기 (0) | 2021.09.28 |