Skip to content

Instantly share code, notes, and snippets.

@daniellopez0708
Created April 6, 2020 12:36
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 daniellopez0708/4d13c0fcd0e055fa616daa3a37b3e2dc to your computer and use it in GitHub Desktop.
Save daniellopez0708/4d13c0fcd0e055fa616daa3a37b3e2dc to your computer and use it in GitHub Desktop.
def filter_mask(img):
'''
This filters are hand-picked just based on visual tests
'''
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2, 2))
# Fill any small holes
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
# Remove noise
opening = cv2.morphologyEx(closing, cv2.MORPH_OPEN, kernel)
# Dilate to merge adjacent blobs
dilation = cv2.dilate(opening, kernel, iterations=2)
return dilation
bg_subtractor = cv2.createBackgroundSubtractorMOG2(
history=500, detectShadows=True)
# Set up image source
cap = skvideo.io.vreader(VIDEO_SOURCE)
# skipping 500 frames to train bg subtractor
train_bg_subtractor(bg_subtractor, cap, num=500)
frame = next(cap)
fg_mask = bg_subtractor.apply(frame, None, 0.001)
fg_mask[fg_mask < 240] = 0
fg_mask = filter_mask(fg_mask)
plt.figure(figsize=(12,12))
plt.imshow(fg_mask)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment