Created
December 9, 2017 00:17
-
-
Save garybradski/86f42bd62b54032187ee8e38681e5b0d to your computer and use it in GitHub Desktop.
Connected Components in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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