pcbway




motion tracking, motion detection, detect motion, matlab motion detectionHello 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:

Motion Detection in MATLAB

  • First of all download the Motion Detection in MATLAB simulation by clicking the below button:

Download the MATLAB Simulation

  • Once you downloaded the rar file, you will get two files in it, named as:
  • Comparison.m
  • Comparison.fig
  • Now open the Comparison.m file and run your simulation.
  • When you run the file a GUI will open up as shown in below figure:

motion tracking, motion detection, detect motion, matlab motion detection

  • 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:

motion tracking, motion detection, detect motion, matlab motion detection

  • 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:

motion tracking, motion detection, detect motion, matlab motion detection

  • 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:

motion tracking, motion detection, detect motion, matlab motion detection

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

4 Comments

  1. hi
    very nice and very good work

    i try to run this code in my lap top but the text is not change in all states only one text appear “static text”

    i dont know why ??

    can you tell me pleeaz

    hank you

  2. hi

    nice code and very good work

    I try t run this code but the next is not change in all states appear ” static text”

    i dont know why

    can you tell me pleeez

    thank you

  3. well done
    but there is some static errors like
    Error in comparison>CaptureImage_Callback (line 81)
    obj=videoinput(‘winvideo’,1);

    Error in gui_mainfcn (line 95)
    feval(varargin{:});

    Error in comparison (line 42)
    gui_mainfcn(gui_State, varargin{:});

    Error in @(hObject,eventdata)comparison(‘CaptureImage_Callback’,hObject,eventdata,guidata(hObject))

    Error while evaluating UIControl Callback

    >> comparison
    Error: File: comparison.m Line: 12 Column: 1
    Function definitions are not permitted in this context.

    can you solve this …………

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment