Video Surveillance using MATLAB 

 August 20, 2021

By  Abhishek Tyagi


In this blog, you will learn how to build Video Surveillance using MATLAB. This system can display live video from any source such as webcam, CCTV, etc., detecting motion and sending an alarm when the camera is triggered. It is a fun project for understanding or implementing computer vision in MATLAB and serves to be a template in designing complex systems using computer vision functionalities.

Ever wanted a program that allows you to leave your computer unattended and come back without any worry of people fidgeting with it. Or wanted a system capable of guarding or monitoring a valued object at home during an event? Or wanted your home Video Surveillance system? If yes, keep reading this blog and learn how to build Video Surveillance using MATLAB and Computer Vision.

The first part of the blog will briefly review building a Video Surveillance camera in MATLAB. Next, we'll install the libraries/packages needed to build the system and implement the developed code successfully.

And finally, we'll put our Video Surveillance using MATLAB into action!


Building this system consists of three parts. The first part is capturing framewise a live video stream by a webcam. The next step involves preprocessing the difference between the two consecutive frames. The final step is to warn if there is a movement using an alarm sound. Let us discuss each step in detail in the coming sections.

Fig 1. Video Surveillance using MATLAB

Fig 1. Video Surveillance using MATLAB


To acquire live video frames or images, we will be using the webcam function. Suppose you have multiple cameras connected to your system. You can specify the number or the name of the camera you want to use for the task. 

The algorithm requires two consecutive frames implementing the snapshot function on the webcam object. We acquire these frames as an image.

Image Processing

Once we have the frames in hand, the next step is to find the absolute difference between the consecutive frames. For this, we will be making use of the imabsdiff function in MATLAB. The image obtained is still in RGB form, but it is better to carry out image transformations in Grayscale. It is simpler and more convenient. To transform a color image to a grayscale image, we make use of the rgb2gray function.

The next step is to use the Gaussian blurring filter of size 5x5 to reduce image noise and reduce details, and for this, we use the imgaussfilt function.

After image enhancement, we move on to Computer Vision Toolbox objects and morphological processing to satisfy our requirements. We will be using two computer vision toolbox objects; one is the vision.ForegroundDetector and the other one is the vision.BlobAnalysis.

The ForegroundDetector segment moving objects in a video frame from the background by comparing to a background model whether individual pixels are part of the background or the foreground. It outputs a binary mask, where the pixel value 1 corresponds to the foreground, and the value 0 corresponds to the background. By using background subtraction, you can detect foreground objects in an image taken from a stationary camera. 

Three morphological functions will be implemented to remove the noise and fill in the holes:

  • imopen
  • imclose
  • imfill

Connected groups of foreground pixels from morphological processing likely correspond to moving objects. To find such groups, we use the blob analysis System object called 'blobs' or 'connected components' and compute their characteristics, such as area, centroid, and the bounding box.


The blob or connected component area in the above step is a characteristic or criteria for the alarm to ring. We use an if loop for this; when the area of any connected component or moving object is greater than a fixed constant value, we want the alarm to play. We have downloaded an audio file from YouTube audio library for our Video Surveillance and used it in our code. You can use your own sound or download the one used in our blog.


To display the frames obtained from the webcam, we will be using the vision.VideoPlayer object from the Computer Vision Toolbox. We make use of a while loop in this program, controlled by the video player object. During each iteration of the while loop, it shows the first frame. The second frame gets assigned to the first frame in the next while loop, and a new frame is assigned to the second frame, and it goes on. Resulting in a live video display on the screen. A bounding box is inserted onto it using the insertObjectAnnotation function for each blob identified in a frame. 

We have also created a loop or a section that can close the Video Surveillance when you don't want to use it. Whenever a user presses or inputs the g key from the keyboard, the Video Surveillance is aborted.


This section gives the complete code on how to implement Video Surveillance using MATLAB. Some of the requirements to run this code is listed below:

  • Computer Vision Toolbox
  • Support package for using webcams in MATLAB
  • Download the audio file provided



close all;

 % Create objects for foreground detection and blob analysis

Detector = vision.ForegroundDetector('NumGaussians', 3, ...

            'NumTrainingFrames', 40, 'MinimumBackgroundRatio', 0.7);

blobAnalyser = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...

            'AreaOutputPort', true, 'CentroidOutputPort', true, ...

            'MinimumBlobArea', 400);

% Create the webcam object.

reader =webcam ();

% Create the video player object.


frameSize = size(frame2);

videoPlayer = vision.VideoPlayer('Position', [100 100 [frameSize(2), frameSize(1)] +30]);

while ~isOpen(videoPlayer)

    frame1 =frame2; %first frame

    frame2 = snapshot(reader); %next frame

    diff=imabsdiff(frame1, frame2); %finding the difference between two frames

    gray=rgb2gray(diff); %RGB to gray image

    blur =  imgaussfilt(gray,'FilterSize',5); %gaussian smoothing filter

    % Detect foreground.

    mask = Detector.step(blur);

    % Apply morphological operations to remove noise and fill in holes.

    mask = imopen(mask, strel('rectangle', [3,3]));

    mask = imclose(mask, strel('rectangle', [15, 15]));

    mask = imfill(mask, 'holes');

   % Perform blob analysis to find connected components.

   [a, centroids, bboxes] = blobAnalyser.step(mask);

   %stats = regionprops(mask, {'Area','BoundingBox'})

   %this should take the contour area and if it is greater than a

   % certain value it has to play the alarm

    if a>5000




    % Draw the objects on the frame.

     frame1 = insertObjectAnnotation(frame1, 'rectangle', ...

                    bboxes, 1);

    %when g key is pressed it stops analysing

    g = figure (1);

    isKeyPressed = ~isempty(get(g,'CurrentCharacter'));

    if isKeyPressed

        clear cam;






% Clean up.

clear reader;

Release (videoPlayer);

release (Detector);


In this blog, you learned how to build a Video Surveillance using MATLAB from scratch using a computer vision Toolbox.

You can easily extend this method to work with other detection forms, including Face Detection, Motion-based multiple object tracking, and more. You can take it as an exercise to implement. In that case, you can use this project as a base for implementing any additional computer vision functionality.

I hope you enjoyed this blog!

Did you find some helpful content from our video or article and now looking for its code, model, or application? You can purchase the specific Title, if available, and instantly get the download link.

Thank you for reading this blog. Do share this blog if you found it helpful. If you have any queries, post them in the comments or contact us by emailing your questions to [email protected]. Follow us on LinkedIn Facebook, and Subscribe to our YouTube Channel. If you find any bug or error on this or any other page on our website, please inform us & we will correct it.

If you are looking for free help, you can post your comment below & wait for any community member to respond, which is not guaranteed. You can book Expert Help, a paid service, and get assistance in your requirement. If your timeline allows, we recommend you book the Research Assistance plan. If you want to get trained in MATLAB or Simulink, you may join one of our training modules. 

If you are ready for the paid service, share your requirement with necessary attachments & inform us about any Service preference along with the timeline. Once evaluated, we will revert to you with more details and the next suggested step.

Education is our future. MATLAB is our feature. Happy MATLABing!

About the author 

Abhishek Tyagi

Abhishek Tyagi is an aspiring Automotive engineer who enjoys connecting the dots: be it ideas from different disciplines, people from different teams, or applications from different industries. He has strong technical skills and an academic background in engineering, statistics, and machine learning.

  • Sourabh Dilip Sarode says:

    Hello sir
    Can i have make this project with this blog guidance

  • Sowbhagya Appalla says:

    The overall blog is nice and seems to be perfect. This can be used as a base project to develop applications related to Video surveillance.

  • Priyanshu Singh says:

    sir ai se isi project ko agar bnaye toh kya benfits h

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Connect with MATLAB Helper ®

    Follow: YouTube Channel, LinkedIn Company, Facebook Page, Instagram Page

    Join Community of MATLAB Enthusiasts: Facebook Group, Telegram, LinkedIn Group

    Use Website Chat or WhatsApp at +91-8104622179