How to implement the code for shadow removal from mask in opencv?

1.9k Views Asked by At

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()

1

There are 1 best solutions below

2
On

If you are using BackgroundSubtractorMOG2 or BackgroundSubtractorKNN background subtractor, then you can easily set the ShadowValue to false or 0:

mog2->setShadowValue(0);
// Or
knn->setShadowValue(0);

And it will remove the shadow from the mask.

With shadow value = true

enter image description here

With shadow value = false

enter image description here