■ findContours 함수를 사용해 외곽선을 검출하는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import cv2 imageNDArray = cv2.imread("source.jpg", cv2.IMREAD_COLOR) grayscaleImageNDArray = cv2.cvtColor(imageNDArray, cv2.COLOR_RGB2GRAY) ret, binary = cv2.threshold(grayscaleImageNDArray, 127, 255, cv2.THRESH_BINARY) binary = cv2.bitwise_not(binary) contourList, hierarchyNDArray = cv2.findContours(binary, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE) for i in range(len(contourList)): cv2.drawContours(imageNDArray, [contourList[i]], 0, (255, 0, 0), 2) #cv2.putText(imageNDArray, str(i), tuple(contourList[i][0][0]), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 0), 1) #print(i, hierarchyNDArray[0][i]) cv2.imshow("src", imageNDArray) cv2.waitKey(0) cv2.destroyAllWindows() |