'''열기
침식 + 팽창
-> 생성 이미지 침식 -> 팽창 후 결과 확인
'''
img = np.zeros(shape=(12,12), dtype=np.uint8)
img[3:-3, 2:10] = 255
img[3, 2:5] = 0
img[3, 7:10] = 0
img[-4,5:7] = 0
erode1 = cv2.erode(img,kernel,iterations=1)
open1 = cv2.dilate(erode1,kernel,iterations=1)
ax1 = plt.subplot(131)
ax2 = plt.subplot(132)
ax3 = plt.subplot(133)
ax1.axis('off')
ax2.axis('off')
ax3.axis('off')
ax1.imshow(img)
ax2.imshow(erode1)
ax3.imshow(open1)
plt.show()
'''닫기
팽창 + 침식
생성이미지 구현 및 팽창 -> 침식 후 결과 확인
'''
img = np.zeros(shape=(12,12), dtype=np.uint8)
img[3:-3, 2:10] = 255
img[3, 2:5] = 0
img[3, 7:10] = 0
img[-4,5:7] = 0
dilate1 = cv2.dilate(img,kernel,iterations=1)
close1 = cv2.erode(dilate1,kernel,iterations=1)
ax1 = plt.subplot(131)
ax2 = plt.subplot(132)
ax3 = plt.subplot(133)
ax1.axis('off')
ax2.axis('off')
ax3.axis('off')
ax1.imshow(img)
ax2.imshow(dilate1)
ax3.imshow(close1)
plt.show()
'''
기타 모폴로지 연산
-빼기 연산
-MORPH_GRADIENT : 팽창 - 침식
-MORPH_TOPHAT : 원본 - 열기
-MORPH_BLACKHAT: 닫기 - 원본
'''
# 이미지 읽어오기
img = np.zeros(shape=(12,12), dtype=np.uint8)
img[3:-3, 2:10] = 255
img[3, 2:5] = 0
img[3, 7:10] = 0
img[-4,5:7] = 0
# 이미지 크기 변환
# 커널 생성
kernel = np.ones((11,11),np.uint8)
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel, iterations=1)
blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel, iterations=1)
cv2.imshow("tophat", tophat)
cv2.imshow("blackhat", blackhat)
cv2.waitKey(0)
'OpenCV' 카테고리의 다른 글
OpenCV - 레이블링 기법 (0) | 2022.11.14 |
---|---|
OpenCV - 모폴로지 종합 실습 (0) | 2022.11.12 |
OpenCV - 침식 (ERODE), 팽창 (DILATE) (0) | 2022.11.12 |
OpenCV - 모폴로지 연산 (0) | 2022.11.12 |
OpenCV - 소벨 필터 적용 (0) | 2022.11.12 |