MATLAB: How to detect vehicles passing a virtual line in a video

boundary boxComputer Vision Toolboximage processingImage Processing Toolboxvehicle detection

I am currently working on a Smart Traffic System project. I am trying to detect the number of cars in a .mp4 video. I was thinking of adding a virtual line on the video and seeing if the vehicle passes it and keep count. I was looking up some methods online and seem to be stuck. I am using the Computer Vision Toolbox to generate blobs and also to work on the foreground. The blobs seem to be detecting parts of the road signs or the road itself as an object so I am not able to rely upon it for an accurate count.
I thought of using the blob co-ordinates and seeing if it passed through the line and then keep track of it but I cannot determine their co-ordinates.
Basically I wanted to know if there was a solid technique to use a virtual line and see how many cars have passed it.

Best Answer

  • Hey,
    It is my understanding that you want to build a vehicle detection model, which can count the number of vehicles crossing a virtual line. Interest-point detection methods like blob detection are not working due to noise in the data.
    If the sole purpose of the model is to detect vehicles from a video, you can use motion detection techniques to detect the presence/position of a car. If you have a stationary recording device, one method could be to extract a frame out of the video wherein no cars are present. Using that as a reference frame, motion in other frames can be detected using frame subtraction and thresholding. Further using contour detection, position of the car can also be recorded and compared with the virtual line. This method would also be robust against the noise provided by road, signs etc.
    Here’s a code snippet for motion detection:
    RefImage = rgb2gray(imread('pathToFile'));
    totalNumFrames = 1000;
    thresholdParam = 0.1;%how sensitive you want the model to be to changes in pixel values/luminosity.
    for i = (1:totalNumFrames)
    curFrame %get the currentFrame
    mask = curFrame - refFrame;
    binarisedMask = imbinarize(img2-img1, thresholdParam);%This matrix has the value 1 wherever the curFrame
    %differs from refFrame and staisfies thresholdParam
    %constraint. Thus motion detection is captured by this
    %matrix
    imshow(binarisedMask);
    %add contour detection for position of car.
    end
    Other methods could be to use Deep learning for vehicle detection, linked here and here