반응형
import cv2
import numpy as np
import matplotlib.pyplot as plt
from google.colab.patches import cv2_imshow
%matplotlib inline
def plot_img(images, titles):
fig, axs = plt.subplots(nrows = 1, ncols = len(images), figsize = (15, 15))
for i, p in enumerate(images):
axs[i].imshow(p, 'gray')
axs[i].set_title(titles[i])
#axs[i].axis('off')
plt.show()
!wget https://www.shrednations.com/wp-content/uploads/corporate-espionage.jpg
img = cv2.imread('corporate-espionage.jpg', 0)
plt.imshow(img, 'gray')
plt.show()
height, width = img.shape
print(width, height)
영상 이진화
ret, binary_img = cv2.threshold(img, 128, 1, cv2.THRESH_BINARY)
# Plot the images
images = [img, binary_img]
titles = ['Original image', 'THRESH_BINARY']
plot_img(images, titles)
4연결성 기준 3X3 팽창(dilation), 침식(erosion), 열기(open), 닫기(close) 구현
- 4 연결성 필터
- 이진화 된 영상의 픽셀 값이 0인 부분에 대하여 팽창, 침식, 열기 과정 수행
#3x3 팽창(dilatioin) 함수 작성
def dilation_3x3(img):
# 함수 작성
r, c = img.shape
pad_img = img.copy()
pad_img = cv2.copyMakeBorder(img, 1, 1, 1, 1, cv2.BORDER_CONSTANT, value=0)
kernel = np.array([[0,1,0],[1,1,1],[0,1,0]])
output_img = img.copy()
for i in range(0,r):
for j in range(0,c):
boundary = pad_img[i:i+3, j:j+3] - kernel
if(np.min(boundary) == -1):
output_img[i,j] = 0
else:
output_img[i,j] = 1
return output_img
#3x3 침식(erosion) 함수 작성
def erosion_3x3(img):
# 함수 작성
r, c = img.shape
pad_img = img.copy()
pad_img = cv2.copyMakeBorder(img, 1, 1, 1, 1, cv2.BORDER_CONSTANT, value=0)
kernel = np.array([[0,1,0],[1,1,1],[0,1,0]])
output_img = img.copy()
for i in range(0,r):
for j in range(0,c):
boundary = pad_img[i:i+3, j:j+3] - kernel
if(boundary[0,1]+boundary[1,0]+boundary[1,1]+boundary[1,2]+boundary[2,1] == -5):
output_img[i,j] = 0
else:
output_img[i,j] = 1
return output_img
#3x3 열기(open) 함수 작성
def open_3x3(img):
# 함수 작성
img = erosion_3x3(img)
output_img = dilation_3x3(img)
return output_img
#3x3 닫기(close) 함수 작성
def Close_3x3(img):
# 함수 작성
img = dilation_3x3(img)
output_img = erosion_3x3(img)
return output_img
팽창 결과
# 3x3 팽창(dilation) 결과 출력
# cv2.dilate(src, kernel, dst, anchor, iterations, bordeType, borderValue)
dilated_img = dilation_3x3(binary_img)
images = [img, dilated_img]
titles = ['Original image', 'Dilated_image']
plot_img(images, titles)
침식 결과
# 3x3 침식(erosion) 결과 출력
# cv2.erode(src, kernel, anchor, iterations, borderType, borderValue)
erosion_img = erosion_3x3(binary_img)
images = [img, erosion_img]
titles = ['Original image', 'Erosion_image']
plot_img(images, titles)
열기 결과
# 3x3 열기(openning) 결과 출력
# cv2.morphologyEx(src, op, kernel, dst, anchor, iteration, borderType, borderValue)
# cv2.MORPH_OPEN: 열림 연산
open_img = open_3x3(binary_img)
images = [img, open_img]
titles = ['Original image', 'Open_image']
plot_img(images, titles)
닫기 결과
# 3x3 닫기(closing) 결과 출력
# cv2.morphologyEx(src, op, kernel, dst, anchor, iteration, borderType, borderValue)
# cv2.MORPH_COLSE: 닫힘 연산
close_img = Close_3x3(binary_img)
images = [img, close_img]
titles = ['Original image', 'Close_image']
plot_img(images, titles)
반응형
'Computer vision' 카테고리의 다른 글
[Computer vision] 이진화 알고리즘 / 오츄 알고리즘 (0) | 2021.10.29 |
---|---|
[Computer vision] 이진영상 라벨링하기 / 4연결성(4-neighbors), 8연결성(8-neighbors) (0) | 2021.10.27 |
[Computer vision] 라플라시안 연산자 (Laplacian Operator) (0) | 2021.09.30 |
[Computer vision] 선형 필터(Linear Filter) / 박스 필터(Box Filter), 가우시안 필터(Gaussian Filter) (0) | 2021.09.23 |
[Computer vision] 컨볼루션 Convolution이란? (0) | 2021.09.23 |