Motion Detector by frame substraction

6 views (last 30 days)
Flora Blackrose
Flora Blackrose on 14 Jun 2020
Answered: Aakash Mehta on 24 Jun 2020
Hello,
I have a question regarding MATLAB. I have a final project of a motion detector and we have not studied anything related to it. I am googling and found few things. Our teacher has suggested us to store a frame (of without any object) in a variable 'a' and then store every new frame in variable 'b' (I guess we do it by a for loop). Then we do;
c=a-b
Now, I am confused how I am going to achieve the 'c' variable one. This is what I have done so far
b
cam=webcam
preview(cam)
a=snapshot(cam);
image(a)
Now, I am not sure how exactly store the new frames (fps) in a variable 'b' and subtract it. If possible do guide me.
QUESTION: You have to write a matlab code to get live video stream from the webcam or any camera connected to matlab then you have to do motion detection by using background subtraction from video frame. After motion is detected the coordinate of centroid of the moving object must be calculated and display on MATLAB output screen.
Thanks again!!!

Answers (2)

Pranjal Kaura
Pranjal Kaura on 18 Jun 2020
Hey,
It seems, you need help with the Motion detection model in which detection is done using frame by frame subtraction.
The basic theory behind the model is that if there’s some motion in the current image, it’s pixel values will differ from the reference Image, which we store at the very start of the model. This difference can be captured and thus motion can be detected.
Here’s a code snippet to generate a matrix that captures 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; % this is your 'c' matrix/variable
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);
end
For further analysis of motion detection from this matrix, you can refer to the documentation of Contour function.

Aakash Mehta
Aakash Mehta on 24 Jun 2020
As per my understanding, you can get the frames from the webcam. Now, you want to detect the motion from the frame. To detect the motion and subtract the background you can use the vision.ForegroundDetector. Which internally uses a Mixture of Gaussians model to detect the foreground/Motion. By this detector, you will able to get the foreground mask which is a binary image. So, we can consider it as a 2D matrix which contains 0 or 1. To use, the Foreground detector you have to install the Computer Vision toolbox.
Mostly, this method is applicable for video and you also want to apply it on video. So, there is no need for background beforehand(A). This model learns from initial frames and then gives the foreground mask. So, if you are using this technique then you don't need to subtract A(background frame) - B(current frame)
Refer to the following documentation for more information on how to detect the foreground mask.
https://www.mathworks.com/help/vision/ref/vision.foregrounddetector-system-object.html

Categories

Find more on Computer Vision Toolbox in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!