How can I save the video file from matlab videoPlayer

49 views (last 30 days)
How can I save the video file from matlab videoPlayer? I want to export the processed video https://kr.mathworks.com/help/vision/examples/detecting-cars-using-gaussian-mixture-models.html Or how can I change the videoPlayer to videoWriter command?
foregroundDetector = vision.ForegroundDetector('NumGaussians', 3, ...
'NumTrainingFrames', 50);
videoReader = vision.VideoFileReader('visiontraffic.avi');
for i = 1:150
frame = step(videoReader); % read the next video frame
foreground = step(foregroundDetector, frame);
end
figure; imshow(frame); title('Video Frame');
figure; imshow(foreground); title('Foreground');
se = strel('square', 3);
filteredForeground = imopen(foreground, se);
figure; imshow(filteredForeground); title('Clean Foreground');
blobAnalysis = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
'AreaOutputPort', false, 'CentroidOutputPort', false, ...
'MinimumBlobArea', 150);
bbox = step(blobAnalysis, filteredForeground);
result = insertShape(frame, 'Rectangle', bbox, 'Color', 'green');
numCars = size(bbox, 1);
result = insertText(result, [10 10], numCars, 'BoxOpacity', 1, ...
'FontSize', 14);
figure; imshow(result); title('Detected Cars');
videoPlayer = vision.VideoPlayer('Name', 'Detected Cars');
videoPlayer.Position(3:4) = [650,400]; % window size: [width, height]
se = strel('square', 3); % morphological filter for noise removal
while ~isDone(videoReader)
frame = step(videoReader); % read the next video frame
% Detect the foreground in the current video frame
foreground = step(foregroundDetector, frame);
% Use morphological opening to remove noise in the foreground
filteredForeground = imopen(foreground, se);
% Detect the connected components with the specified minimum area, and
% compute their bounding boxes
bbox = step(blobAnalysis, filteredForeground);
% Draw bounding boxes around the detected cars
result = insertShape(frame, 'Rectangle', bbox, 'Color', 'green');
% Display the number of cars found in the video frame
numCars = size(bbox, 1);
result = insertText(result, [10 10], numCars, 'BoxOpacity', 1, ...
'FontSize', 14);
step(videoPlayer, result); % display the results
end
release(videoReader); % close the video file
  1 Comment
KD IS
KD IS on 28 Oct 2019
videoReader = vision.VideoFileReader('visiontraffic.avi');
videoFWriter=vision.VideoFileWriter('result.avi',...
'FrameRate',videoReader.info.VideoFrameRate);
...
...
step(videoPlayer, result); % display the results
step(videoFWriter,result); % saves video
...
...
release(videoReader); % close the videoReader
release(videoFWriter); % close the videoWriter

Sign in to comment.

Answers (2)

Harish Ramachandran
Harish Ramachandran on 17 Jan 2018
Edited: Harish Ramachandran on 17 Jan 2018
In the piece of code you attached above, you are using a VideoReader in order to get the individual frames and implement the car detection algorithm.
In a similar manner, you need to create a VideoWriter object in order to write the frames and process/save them into a video.
(Think of it as 'fread' and 'fwrite' but for video files)
Start a 'for' loop and use the 'open' command. Inside the loop, you will need to use the method 'writeVideo'. Then close the file after the loop.
video_object = VideoWriter('getframes');
open(video_object);
for i = 1:number of input frames
writeVideo(video_object, format, other options);
end
close(video_object );
Reference:

Vemana
Vemana on 6 Sep 2023
Create a video reader named turtleVideo for the video file turtles.avi.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!