Deep Learning/Computer Vision
OpenCV (16) - convexHull
알 수 없는 사용자
2022. 6. 10. 01:00
728x90
대략적인 형태 Contour 외곽 빠르게 구하기
cv2.convexHull(contour)
Convex Hull 알고리즘으로 외곽을 구하는 함수
단일 contour 반환
import cv2
import matplotlib.pyplot as plt
image = cv2.imread('digit_image.png')
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(image_gray, 230, 255, 0) # 픽셀 값이 230 보다 큰 경우 255, 원본은 배경이 하얀색
thresh = cv2.bitwise_not(thresh) # 검정과 하얀색 반전
contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0]
image = cv2.drawContours(image, contours, -1, (0,255,0), 4)
plt.imshow(cv2.cvtColor(image,cv2.COLOR_BGR2RGB))
plt.show()
contour = contours[0]
hull = cv2.convexHull(contour)
image = cv2.drawContours(image, [hull], -1, (0,255,0), 4)
plt.imshow(cv2.cvtColor(image,cv2.COLOR_BGR2RGB))
plt.show()