같은 색과 강도로 계속되는 점을 이은 곡선
# Syntex
contours, hierarchy = cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]])
# Example
contours, hierarchy = cv2.findContours(img, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
# ---------- 외부 윤곽 출력 ------------ #
# Set up empty array
external_contours = np.zeros(image.shape)
# For every entry in contours
for i in range(len(contours)):
# last column in the array is -1 if an external contour (no contours inside of it)
if hierarchy[0][i][3] == -1:
# We can now draw the external contours from the list of contours
cv2.drawContours(external_contours, contours, i, 255, -1)
# hierarchy
array([[[ 4, -1, 1, -1],
[ 2, -1, -1, 0],
[ 3, 1, -1, 0],
[-1, 2, -1, 0],
[21, 0, 5, -1],
[ 6, -1, -1, 4],
[ 7, 5, -1, 4],
[ 8, 6, -1, 4],
[ 9, 7, -1, 4],
[10, 8, -1, 4],
[11, 9, -1, 4],
[12, 10, -1, 4],
[13, 11, -1, 4],
[14, 12, -1, 4],
[15, 13, -1, 4],
[16, 14, -1, 4],
[17, 15, -1, 4],
[18, 16, -1, 4],
[19, 17, -1, 4],
[20, 18, -1, 4],
[-1, 19, -1, 4],
[-1, 4, -1, -1]]], dtype=int32)
mode:
cv2.RETR_EXTERNAL: 외부 윤곽선만 찾습니다.cv2.RETR_LIST: 모든 윤곽선을 찾지만, 계층 구조는 무시합니다.cv2.RETR_CCOMP: 2단계 계층 구조를 구성합니다.cv2.RETR_TREE: 모든 윤곽선과 계층 구조를 찾습니다.method:
cv2.CHAIN_APPROX_NONE: 윤곽선의 모든 점을 저장합니다.cv2.CHAIN_APPROX_SIMPLE: 윤곽선의 꼭짓점을 저장하여 메모리를 절약합니다.cv2.CHAIN_APPROX_TC89_L1, cv2.CHAIN_APPROX_TC89_KCOS: Teh-Chin 알고리즘에 기반한 근사화 메서드입니다.contours (Optional):
hierarchy (Optional):
offset (Optional):
next: 같은 계층에서 다음 윤곽선의 인덱스. 더 이상 없다면 -1.previous: 같은 계층에서 이전 윤곽선의 인덱스. 더 이상 없다면 -1.child: 첫 번째 자식 윤곽선의 인덱스. 자식이 없다면 -1.parent: 부모 윤곽선의 인덱스. 부모가 없다면 -1.