[참고]

 

1. Contours

  • 정확도를 높이기 위해 Binary Image를 사용
  • threshold나 canny edge를 선처리로 수행함
  • cv2.findContours() 함수는 원본 이미지를 직접 수정하기 때문에 원본이미지를 보존하려면 copy해서 사용
  • openCV에서 contours를 찾는 것은 검은색 배경에서 흰 대상을 찾는 것과 비슷함. 그래서 대상은 흰색, 배경은 검은색으로 해야 함

 

import cv2
import numpy as np 

img = cv2.imread('circle1.png')
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret, thresh = cv2.threshold(imgray, 127, 255, 0)

contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
image = cv2.drawContours(img, contours, -1, (0,0,255), 1)

cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

[결과]

cv2.findContours() 확인

+ Recent posts