경기도미래기술학교 AI개발자 부트캠프 49일차 TIL- 소벨필터링 계속 배우기.
2023. 7. 16. 21:41ㆍ카테고리 없음
반응형
오늘은 이런걸 그렸다. 소벨 필터링의 엣지부분에 색칠하는 것.
import numpy as np
from scipy.signal import correlate2d
import matplotlib.pyplot as plt
white_patch = 255 * np.ones(shape=(10, 10))
black_patch = 0 * np.ones(shape=(10, 10))
img1 = np.hstack([white_patch, black_patch])
img2 = np.hstack([black_patch, white_patch])
img3 = np.vstack([img1, img2])
img = np.tile(img3, reps=[2, 2])
fig, ax = plt.subplots(ncols=3, figsize=(10 * 3, 10))
ax[0].imshow(img, cmap='gray')
ax[0].tick_params(left=False, labelleft=False, bottom=False, labelbottom=False)
filter_x = np.array([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])
filter_y = np.array([[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])
F = filter_x.shape[0]
L = img.shape[0]
L_ = L - F + 1
W_ = img.shape[1] - F + 1
filtered_x = []
filtered_y = []
for i in range(L_):
for j in range(W_):
window = img[i : i + F, j : j + F]
filtered_x.append(np.sum(window * filter_x))
filtered_y.append(np.sum(window * filter_y))
filtered_x = np.array(filtered_x).reshape((L_, W_))
filtered_y = np.array(filtered_y).reshape((L_, W_))
ax[1].imshow(filtered_x, cmap='gray')
ax[1].tick_params(left=False, labelleft=False, bottom=False, labelbottom=False)
ax[2].imshow(filtered_y, cmap='gray')
ax[2].tick_params(left=False, labelleft=False, bottom=False, labelbottom=False)
plt.show()
나만의 소스.
import matplotlib.image as mpimg
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
test_img = mpimg.imread('./신짱구.png')
print(test_img.shape)
test_img = np.dot(test_img[...,:3], [0.2989, 0.5870, 0.1140])
print(test_img.shape)
fig, ax = plt.subplots(ncols=3, figsize=(10 * 3, 10))
ax[0].imshow(test_img, cmap='gray')
ax[0].tick_params(left=False, labelleft=False, bottom=False, labelbottom=False)
filter_x = np.array([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])
filter_y = np.array([[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])
F = filter_x.shape[0]
L = test_img.shape[0]
L_ = L - F + 1
W_ = test_img.shape[1] - F + 1
filtered_x = []
filtered_y = []
for i in range(L_):
for j in range(W_):
window = test_img[i : i + F, j : j + F]
filtered_x.append(np.sum(window * filter_x))
filtered_y.append(np.sum(window * filter_y))
filtered_x = np.array(filtered_x).reshape((L_, W_))
filtered_y = np.array(filtered_y).reshape((L_, W_))
ax[1].imshow(filtered_x, cmap='gray')
ax[1].tick_params(left=False, labelleft=False, bottom=False, labelbottom=False)
ax[2].imshow(filtered_y, cmap='gray')
ax[2].tick_params(left=False, labelleft=False, bottom=False, labelbottom=False)
plt.show()
간단한 이미지 해보라고 하셔서 짱구를 조졌다. [0.2989, 0.5870, 0.1140]는 RGB값이다. 이게 3차원 값으로 되어 있어서 2차원으로 줄이는 작업이 필요했다.
728x90