Skip to content

Instantly share code, notes, and snippets.

@garybradski
Created December 9, 2017 00:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garybradski/86f42bd62b54032187ee8e38681e5b0d to your computer and use it in GitHub Desktop.
Save garybradski/86f42bd62b54032187ee8e38681e5b0d to your computer and use it in GitHub Desktop.
Connected Components in Python
import cv2
import numpy as np
def connected_components(thresh_img):
thresh_img = mog_mask.copy()
contours, hierarchy = cv2.findContours(thresh_img, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
fg_mask = np.zeros(frame.shape[:2], np.uint8)
i = 0
cs = []
for c in contours:
cs.append(len(c))
cmax = max(cs)
cavg = np.mean(cs)
cstd = np.std(cs)
i = 0
for c in contours:
if len(c) > cmax - 3 * cstd:
cv2.drawContours(fg_mask, [c], 0, (1, 1, 1), -1)
i += 1
return fg_mask
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment