1

Say I have a background image bg and the current frame I, how can I get the foreground mask fgmask?

Note: using the stander way fgmask = abs(bg-I) > th not giving an accurate result, lot of noise found.

I'm aware of MOG2 function in opencv but the problem with this function is that AFAIK it gives the foreground mask depending on the adaptive background which the model generates. Is there away to set this background?

UPDATE:

I have reached to a way to get the foreground mask but I think it is sensitive to the light and shadows.

def getForegroundMask(frame, background, th):
    # reduce the nois in the farme
    frame = cv2.blur(frame, (5,5))
    # get the absolute difference between the foreground and the background
    fgmask= cv2.absdiff(frame, background)
    # convert foreground mask to gray
    fgmask = cv2.cvtColor(fgmask, cv2.COLOR_BGR2GRAY)
    # apply threshold (th) on the foreground mask
    _, fgmask = cv2.threshold(fgmask, th, 255, cv2.THRESH_BINARY)
    # setting up a kernal for morphology
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
    # apply morpholoygy on the foreground mask to get a better result
    fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_CLOSE, kernel)
    return fgmask
Ehab AlBadawy
  • 3,065
  • 4
  • 19
  • 31
  • show some example so we can understand what do you mean by "noise" – Humam Helfawi Dec 19 '15 at 17:33
  • I have reached to a way that is getting a better result but I think it will be very sensitive to the light ,, [code](https://github.com/Grad-CSE2016/Abandoned-Object-Detection/commit/51b34fab4a1b0d7cde9fd886ead79afcf6925ffd) – Ehab AlBadawy Dec 19 '15 at 23:17

0 Answers0