Skip to content

Instantly share code, notes, and snippets.

@pravj
Created December 24, 2018 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pravj/2be7a1c308b969735c1387839503a600 to your computer and use it in GitHub Desktop.
Save pravj/2be7a1c308b969735c1387839503a600 to your computer and use it in GitHub Desktop.
import cv2
# grayscale version of the single color image
image_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# bilateral filter is effective when you want to
# keep the edges sharp while removing noise
image_gray = cv2.bilateralFilter(image_gray, 10, 50, 50)
# find contour in gray scale image after applying erosion and dilation
_, thresh = cv2.threshold(image_gray, 75, 255, 0)
thresh = cv2.erode(thresh, None, iterations=2)
thresh = cv2.dilate(thresh, None, iterations=2)
_, contours, _ = cv2.findContours(
thresh,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment