I am using MoG method in opencv to detect the moving object in a static background frame,but it also detects shadows too. I want to remove the shadows from the mask. I tried using using threshold for grey color(as shaodws are marked in grey in mask) ,but threshold also removes the grey part of an object too. I am trying to implement https://pdfs.semanticscholar.org/53e0/7f60d03461def8ed4f765f2a6b7dfc4bfbd0.pdf this paper's algorithm. Can anyone tell me how to implement this in python?
import cv2
import numpy as np
cap = cv2.VideoCapture('TownCentreXVID.avi')
fgbg = cv2.createBackgroundSubtractorMOG2()
while(1):
_, frame = cap.read()
mask = fgbg.apply(frame)
kernel = np.ones((5,5),np.uint8)
opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
closing = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
window = cv2.namedWindow('Original', cv2.WINDOW_NORMAL| cv2.WINDOW_KEEPRATIO )
window = cv2.namedWindow('Mask', cv2.WINDOW_NORMAL| cv2.WINDOW_KEEPRATIO)
window = cv2.namedWindow('Opening', cv2.WINDOW_NORMAL| cv2.WINDOW_KEEPRATIO )
#window = cv2.namedWindow('Closing', cv2.WINDOW_NORMAL| cv2.WINDOW_KEEPRATIO)
cv2.imshow('Original',frame)
cv2.imshow('Mask',thresh)
cv2.imshow('Opening',opening)
#cv2.imshow('Closing',closing)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
cap.release()
If you are using
BackgroundSubtractorMOG2
orBackgroundSubtractorKNN
background subtractor, then you can easily set theShadowValue
tofalse
or0
:And it will remove the shadow from the mask.
With shadow value = true
With shadow value = false