How to detect moving object in varying light(illumination) conditions due to clouds - openCV

asked 2018-03-24 02:59:39 -0500

vishwaprakash gravatar image

I have been trying to detect moving vehicles. But due to varying light conditions because of clouds, (not shadows of clouds, just illuminations) the background subtraction fails.

I have uploaded my input video here --> Youtube (30secs)

Here is what I got using various available background subtraction methods available in opencv

import numpy as np
import cv2

cap = cv2.VideoCapture('traffic_finalns.mp4')
#fgbgKNN = cv2.createBackgroundSubtractorKNN()
fgbgMOG = cv2.bgsegm.createBackgroundSubtractorMOG(100,5,0.7,0)
#fgbgGMG = cv2.bgsegm.createBackgroundSubtractorGMG()
#fgbgMOG2 = cv2.createBackgroundSubtractorMOG2()
#fgbgCNT = cv2.bgsegm.createBackgroundSubtractorCNT(15,True,15*60,True)

#   ret, frame =
#   fgmaskKNN = fgbgKNN.apply(frame)
    fgmaskMOG = fgbgMOG.apply(frame)
#   fgmaskGMG = fgbgGMG.apply(frame)
#   fgmaskMOG2 = fgbgMOG2.apply(frame)
#   fgmaskCNT = fgbgCNT.apply(frame)
#   cv2.imshow('frame',frame)
#   cv2.imshow('fgmaskKNN',fgmaskKNN)
#   cv2.imshow('fgmaskGMG',fgmaskGMG)
#   cv2.imshow('fgmaskMOG2',fgmaskMOG2)
#   cv2.imshow('fgmaskCNT',fgmaskCNT)

    k = cv2.waitKey(20) & 0xff
    if k == 27:


(Below images -> Frame number - 977)

  • BackgroundSubtractorMOG : By varying the input parameter history some illumination could be reduced, but not all, as the duration of illumination is variable enter image description here

  • BackgroundSubtractorMOG2 : enter image description here

  • BackgroundSubtractorGMG : enter image description here

  • *BackgroundSubtractorKNN : * enter image description here

  • BackgroundSubtractorCNT enter image description here

edit retag flag offensive close merge delete


Try also BackgroundSubtractorGSOC and BackgroundSubtractorLSBP

mshabunin gravatar imagemshabunin ( 2018-03-24 03:31:47 -0500 )edit

@mshabunin , I tried both of them, In GSOC it has the same problem, it is even more susceptible to light changes. LSBP also has the same problem and it is lagging

vishwaprakash gravatar imagevishwaprakash ( 2018-03-24 05:50:12 -0500 )edit

How about going a step further and not using BG/FG subtraction but actual object detectors, specificly for cars? You can even combine them with a detection+segmentation approach that will then yield you the outer contour!

StevenPuttemans gravatar imageStevenPuttemans ( 2018-03-26 07:05:47 -0500 )edit

@StevenPuttemans But doesn't it slow down the process ? I assume Object Detection (in the entire frame, without first detecting moving object) is too slow to be used in real time. Am I wrong ?

vishwaprakash gravatar imagevishwaprakash ( 2018-03-30 23:31:09 -0500 )edit
mshabunin gravatar imagemshabunin ( 2018-04-02 08:43:16 -0500 )edit