cv2.dilate(img, kernel, iterations=1)
import cv2
import numpy as np
kernel = np.ones((3,3), dtype=np.uint8)
#print(kernel)
img = cv2.imread('../OpenCV/dilate.png', cv2.IMREAD_GRAYSCALE)
dilate1 = cv2.dilate(img, kernel, iterations=1) # 반복 횟수
dilate2 = cv2.dilate(img, kernel, iterations=2) # 반복 횟수
dilate3 = cv2.dilate(img, kernel, iterations=3) # 반복 횟수
cv2.imshow('gray', img)
cv2.imshow('dilate1', dilate1)
cv2.imshow('dilate2', dilate2)
cv2.imshow('dilate3', dilate3)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.erode(img, kernel, iterations=1)
import cv2
import numpy as np
kernel = np.ones((3,3), dtype=np.uint8)
#print(kernel)
img = cv2.imread('../OpenCV/erode.png', cv2.IMREAD_GRAYSCALE)
erode1 = cv2.erode(img, kernel, iterations=1) # 1회 반복
erode2 = cv2.erode(img, kernel, iterations=2) # 반복 횟수
erode3 = cv2.erode(img, kernel, iterations=3) # 반복 횟수
cv2.imshow('gray', img)
cv2.imshow('erode1', erode1)
cv2.imshow('erode2', erode2)
cv2.imshow('erode3', erode3)
cv2.waitKey(0)
cv2.destroyAllWindows()
침식 후 팽창, 깎아서 노이즈 제거 후 팽창
import cv2
import numpy as np
kernel = np.ones((3,3), dtype=np.uint8)
img = cv2.imread('../OpenCV/erode.png', cv2.IMREAD_GRAYSCALE)
erode = cv2.erode(img, kernel, iterations=4)
dilate = cv2.dilate(erode, kernel, iterations=5)
cv2.imshow('img', img)
cv2.imshow('erode', erode)
cv2.imshow('dilate', dilate)
cv2.waitKey(0)
cv2.destroyAllWindows()
팽창 후 침식, 구멍을 메운 후 다시 침식
import cv2
import numpy as np
kernel = np.ones((3,3), dtype=np.uint8)
img = cv2.imread('../OpenCV/dilate.png', cv2.IMREAD_GRAYSCALE)
# 팽창(흰부분 팽창)
dilate = cv2.dilate(img, kernel, iterations=3)
# 침식(검은부분 침식)
erode = cv2.erode(dilate, kernel, iterations=3)
cv2.imshow('img', img)
cv2.imshow('erode', erode)
cv2.imshow('dilate', dilate)
cv2.waitKey(0)
cv2.destroyAllWindows()