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:
- 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:
- 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. :P
- 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.
- The code behind Capture Image button is as follows:
obj=videoinput('winvideo',1); obj.ReturnedColorspace = 'rgb'; A=getsnapshot(obj); axes(handles.axes1); imshow(A); imwrite(A,'A.jpg'); delete(obj);
- 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:
global go; go = true; while go obj=videoinput('winvideo',1); obj.ReturnedColorspace = 'rgb'; B=getsnapshot(obj); axes(handles.axes2); imshow(B); imwrite(B,'B.jpg'); delete(obj); global I1; global I2; I1 = imread('A.jpg'); I2 = imread('B.jpg'); % convert images to type double (range from from 0 to 1 instead of from 0 to 255) Imaged1 = im2double(I1); Imaged2 = im2double(I2); % reduce three channel [ RGB ] to one channel [ grayscale ] Imageg1 = rgb2gray(Imaged1); Imageg2 = rgb2gray(Imaged2); % Calculate the Normalized Histogram of Image 1 and Image 2 hn1 = imhist(Imageg1)./numel(Imageg1); hn2 = imhist(Imageg2)./numel(Imageg2); % Calculate the histogram error/ Difference f1 = sum((hn1 - hn2).^2); %set(handles.text1,'String',f1) serialOne=serial('COM1', 'BaudRate', 9600); fopen(serialOne); if f1 > 0.009 fprintf(serialOne,'a'); set(handles.text1,'String','Change Detected') go = false; end if f1 < 0.009 fprintf(serialOne,'b'); set(handles.text1,'String','No change') end fclose(serialOne);
- 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.