3

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)

 while(1):
    ret, frame = cap.read()
#   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('fgmaskMOG',fgmaskMOG)
#   cv2.imshow('fgmaskGMG',fgmaskGMG)
#   cv2.imshow('fgmaskMOG2',fgmaskMOG2)
#   cv2.imshow('fgmaskCNT',fgmaskCNT)

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


cap.release()
cv2.destroyAllWindows()

(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

Severus Tux
  • 265
  • 3
  • 13

1 Answers1

4

1] Improving results by OpenCV Background Subtraction

  • For varying light conditions it is important to normalize your pixal values between 0 and 1. In your code I do not see that happening
  • Background subtraction will not work with a single image (In your code you are reading an image)
  • If you are applying background subtraction on sequence of frames then the first frame of background subtraction result is of no use
  • you might want to adjust the arguments of the cv2.bgsegm.createBackgroundSubtractorMOG() that you are passing to get the best results... Play around with the threshold and see what results do you get
  • You can also apply gaussian filter to the individual frames to reduce noise and get better results cv2.GaussianBlur()
  • You can try cv2.equalizeHist() on individual frame so that you improve the contrast of the frames

Anyways you say that you are trying to detect moving object. Nowadays there are many modern methods that use deep-learning for object detection

2] Use tensorflow object detection api

  • It does object detection in real-time and also gives you the bounding box co-ordinate of the detected objects

  • Here are results of Tensorflow object detection api: enter image description here

3] How about trying Opencv Optical Flow

4] Simple subtraction

  • Your environment is static
  • So take a frame of your environment and store it in a variable say environment_frame
  • Now read every frame from your video and simply subtract it from your environment frame results = environment_frame - current_frame
  • Now if the np.sum(results) is greater than a threshold value then we say there is a object
  • Now if np.sum(results) is greater then threshold then we know there is a moving object but where ???
  • The moving object is where there are clustered cluttered pixels which you can easily find by some clustering algorithm
  • Do not forget to normalize your pixel values between 0 and 1

----------------------------UPDATED----------------------------------------

  • If you want to find helmets in real time then your best bet is deep-learning
  • You can use a deep learning technique like YOLO which newer version of OpenCV has ... but I do no think they have a python binding for YOLO in OpencV
  • The other real time technique can be RCNN which the tensorflow object detection api already has .... I have mentioned it above
  • If you want to use traditional computer vision methods then you can try hog and svm for helmet data and then you can try a sliding window technique to find the helmet in your frame (This won't be in real time)
Jai
  • 3,211
  • 2
  • 17
  • 26
  • Thanks :) , I will try normalizing . BTW I had commented out the while loop , Corrected now – Severus Tux Mar 24 '18 at 11:47
  • My main intention is not to detect moving objects, I am trying to develop a "motor cycle helmet detection", for that What I have in my mind is moving object detection --> classification --> Extract ROI of each motercycle --> Detect Helmet and log . Do you think this is the right approach? Is there any modern tools which would help me ? thanks – Severus Tux Mar 24 '18 at 11:52
  • @SeverusTux .... I have updated my answer... If my solution is helpful and answers your question then do not forget to mark it as correct – Jai Mar 25 '18 at 00:54