Hello friends, hope you all are fine and having fun with your lives. Today, I am going to share a new project which is Motion Detection in MATLAB. In this project, I am gonna detect the motion in MATLAB. This project was designed for security purposes and the condition was to use MATLAB instead of PIR Sensor. We all know that PIR sensor is used for motion detection but for that we have to design a hardware but using this software we can easily detect any motion using MATLAB.
In this project, I have used the webcam and then applied a simple image processing algorithm, designed in MATLAB. Using this algorithm I have detected the motion in the environment. That’s a quite simple project and you are gonna like this one. The code and complete simulation is given below for download. So, let’s get started with Motion Detection in MATLAB:
You may also like:
- Convolution Calculator in MATLAB
- Speech Recognition in MATLAB using Correlation
Motion Detection in MATLAB
- First of all download the Motion Detection in MATLAB simulation by clicking the below button:
Matlab Code Example. Automatic Surface Crack Detection in Concrete Structures Using. Automatic surface crack detection in. Keywords: Crack Detection. Eggshell crack detection based on acoustic response and support vector data description algorithm. It could be easily extended and used in 3. D video conferencing applications.
Download the MATLAB SimulationOnce you downloaded the rar file, you will get two files in it, named as:Comparison.mComparison.figNow open the Comparison.m file and run your simulation.When you run the file a GUI will open up as shown in below figure:
- Now you can see it has three buttons on it.
Press the first button which says Capture Image, so click this button and an image will be saved in the same directory and will also be shown in the first zone as shown in below figure:
- Now, click on the Start Comparison button and the software will start and will start capturing from the webcam.
- It will also show the captured image in the second zone and if there’s no change then the text below will show No Change, as shown in below figure:
- You can see as there’s no motion in the room that’s why it says No change in above figure.
- Now, let’s create some motion, so I am taking my hand in front of the cam and let’s see what results we got in the below figure:
You can see in the above figure that as I placed my hand in the range of webcam, the below text changed to Change Detected. Btw it should be motion detected. 😛Once the software captures the motion it will indicate as well as stop and if there’s no motion then it will keep on monitoring.If you want to stop the comparison then simply click the Stop Comparison button and it will automatically stop.Moreover, the software will also send character to the serial port, if you want you can receive it on any serial port.Now let’s discuss the codes behind these buttons.
MATLAB Codes
- The code behind Capture Image button is as follows:
- In the above code, first of all, I created an object of the webcam and then took a screenshot from that webcam.
- After that I saved that image in the project directory and named it as A.jpg.
- If you check your project directory then you will find this image.
- Now let’s have a look at the code behind Start Comparison Button:
- This is the main code of this Motion detection in MATLAB project.
- Here, again I am creating an object of the webcam and taking a screenshot.
- After that I am saving this screenshot in the project’s directory and I have renamed it as B.jpg.
- So, now I have two images A and B.
- After that I have converted both of these images to grey scale and calculated their histogram error and on the basis of this error I have detected the motion.
- Finally I have created a Serial Port object, if you don’t wanna use serial port then simply remove this code.
So, that’s all for today, I hope you have enjoyed the motion detection in MATLAB. If you have any questions, then ask in comments and I will resolve them.
JLCPCB – Prototype 10 PCBs for $2 (For Any Color)
China’s Largest PCB Prototype Enterprise, 600,000+ Customers & 10,000+ Online Orders Daily
How to Get PCB Cash Coupon from JLCPCB: https://bit.ly/2GMCH9w
Category: MATLABBy Syed Zain Nasir7 Comments
Author: Syed Zain Nasir
https://www.theengineeringprojects.com/
I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>Permalink
Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign up
Find file Copy path
Cannot retrieve contributors at this time
%% created by renny thomas |
%% date 2/2/2017 |
%% load image |
I=imread('3.jpg'); |
figure,imshow(I) |
title('Original image') |
%% Image adjust |
Istrech = imadjust(I,stretchlim(I)); |
figure,imshow(Istrech) |
title('Contrast stretched image') |
%% Convert RGB image to gray |
Igray_s = rgb2gray(Istrech); |
figure,imshow(Igray_s,[]) |
title('RGB to gray (contrast stretched) ') |
%% Image segmentation by thresholding |
%use incremental value to run this selection |
%till required threshold 'level' is |
%achieved |
level = 0.2; |
Ithres = im2bw(Igray_s,level); |
figure,imshow(Ithres) |
title('Segmented cracks') |
Ithres = ~Ithres; |
figure,imshow(Ithres) |
BW = bwmorph(Ithres,'thin', inf); |
figure,imshow(BW) |
title('Thinned image') |
BW = imfill(Ithres, 'holes'); |
figure,imshow(BW) |
title('Filled image') |
%% Image morphological operation |
BW = bwmorph(Ithres,'clean',10); |
figure,imshow(BW) |
title('Cleaned image') |
figure,imshow(BW) |
title('final output') |