# Video Surveillance using MATLAB

August 20, 2021

By  Abhishek Tyagi

`lang: en_US`

## INTRODUCTION

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!

## METHODOLOGY

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

### Webcam

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.

### Alarm

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.

### Display

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.

## MATLAB Code

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

clc;

clear;

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.

% 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.

% Apply morphological operations to remove noise and fill in 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

sound(y,fs);

end

% 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;

release(videoPlayer);

break;

end

imshow(frame1)

end

% Clean up.

Release (videoPlayer);

release (Detector);

## Conclusion

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!

Get instant access to the code, model, or application of the video or article you found helpful! Simply purchase the specific title, if available, and receive the download link right away! #MATLABHelper #CodeMadeEasy

Ready to take your MATLAB skills to the next level? Look no further! At MATLAB Helper, we've got you covered. From free community support to expert help and training, we've got all the resources you need to become a pro in no time. If you have any questions or queries, don't hesitate to reach out to us. Simply post a comment below or send us an email at [email protected].

And don't forget to connect with us on LinkedIn, Facebook, and Subscribe to our YouTube Channel! We're always sharing helpful tips and updates, so you can stay up-to-date on everything related to MATLAB. Plus, if you spot any bugs or errors on our website, just let us know and we'll make sure to fix it ASAP.

Ready to get started? Book your expert help with Research Assistance plan today and get personalized assistance tailored to your needs. Or, if you're looking for more comprehensive training, join one of our training modules and get hands-on experience with the latest techniques and technologies. The choice is yours – start learning and growing with MATLAB Helper today!

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

#### 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"}

#### MATLAB Helper ®

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

Use Website Chat or WhatsApp at +91-8104622179