Skip to content

Instantly share code, notes, and snippets.

@zhunhung
Created October 17, 2019 15:13
Show Gist options
  • Save zhunhung/6a321d20a39fb6c6d5db63d93c8be980 to your computer and use it in GitHub Desktop.
Save zhunhung/6a321d20a39fb6c6d5db63d93c8be980 to your computer and use it in GitHub Desktop.
Find contours in an image
# convert image to grayscale
imgray = cv2.cvtColor(img, cv.COLOR_BGR2GRAY)
# apply threshold
thresh = cv2.threshold(imgray, 127, 255, 0)[1]
# find contours
contours, hierarchy = cv.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# draw contours on image
cv2.drawContours(img, contours, -1, (0, 255, 0), 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment