Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

FUNCTIONS

clc - Clear Command Window


This MATLAB function clears all the text from the Command Window, resulting in a clear screen.

clear - Remove items from workspace, freeing up system memory


This MATLAB function removes all variables from the current workspace, releasing them from system
memory.

close - Close one or more figures


This MATLAB function closes the current figure.

VideoWriter - Create object to write video files


Use a VideoWriter object to create a video file from an array or MATLAB movie.

VideoReader - Create object to read video files


Use a VideoReader object to read files containing video data.

exist - Check existence of variable, script, function, folder, or class


This MATLAB function returns the type of name as a number.

repmat - Repeat copies of array


This MATLAB function returns an array containing n copies of A in the row and column dimensions.

writeVideo - Write video data to file


This MATLAB function writes data from an array to the video file associated with v.

disp - Display value of variable


This MATLAB function displays the value of variable X without printing the variable name.

implay - View 2-D medical image series in Video Viewer app


Medical Imaging Toolbox extends the functionality of the implay (Image Processing Toolbox) function
to display a medicalImage object.
SOURCE CODE

PROGRAM:

clc % Clear command window.

clear all %Clear variables and functions from memory

close all % closes all the open figure windows

% Create a VideoReader object (replace 'your_video.mp4' with your video file)


videoFile = 'C:\Users\user\OneDrive\Pictures\Camera Roll';
videoReader = VideoReader('testvideo.mp4');

% Create a VideoWriter object for saving the output video


outputVideoFile = 'output_motion_detected_video.avi';
outputVideoWriter = VideoWriter(outputVideoFile, 'Uncompressed AVI');
open(outputVideoWriter);

% Set motion detection threshold


motionThreshold = 5;

% Loop through each frame of the video


while hasFrame(videoReader)
% Read the frame
frame = readFrame(videoReader);

% Convert the frame to grayscale


grayFrame = rgb2gray(frame);

% Compute the difference between consecutive frames


if exist('prevFrame', 'var')
frameDiff = abs(grayFrame - prevFrame);

% Create a binary mask based on motion threshold


motionMask = frameDiff > motionThreshold;

% Apply the motion mask to the original frame


motionDetectedFrame = frame;

motionDetectedFrame(repmat(motionMask, [1, 1, 3])) = 255;


% Write the frame with motion detection to the output video
writeVideo(outputVideoWriter, motionDetectedFrame);
end

% Save the current frame for the next iteration


prevFrame = grayFrame;
end

% Close the video writer


close(outputVideoWriter);

% Display a message indicating completion


disp('Motion detection completed.');

% Optionally, play the output video


implay(outputVideoFile);

You might also like