How do I create a video mp4 output from the following code?

5 views (last 30 days)
I am using Video Stabilization Using Point Feature Matching - MATLAB & Simulink (mathworks.com) code to stabilze a video. I am wanting to adapt it for my own use. The code uses the Computer Vision Toolbox video player, but I would like to have an mp4 output rather than just the MATLAB videoplayer that only works 20% of the time. Below is the code (Step 6 of the link) I want to change. I am not quite sure how to create an MP4 output.
% Reset the video source to the beginning of the file
read(hVideoSrc,1);
hVPlayer = vision.VideoPlayer; % Create video viewer
% Process all frames in the video
movMean = rgb2gray(im2single(readFrame(hVideoSrc)));
imgB = movMean;
imgBp = imgB;
correctedMean = imgBp;
ii = 2;
cumulativeTform = simtform2d;
while hasFrame(hVideoSrc) && ii < 10
% Read in new frame
imgA = imgB; % z^-1
imgAp = imgBp; % z^-1
imgB = rgb2gray(im2single(readFrame(hVideoSrc)));
movMean = movMean + imgB;
% Estimate transformation from frame A to frame B, and fit as an s-R-t
tformAffine = cvexEstStabilizationTform(imgA,imgB);
sRtTform = cvexTformToSRT(tformAffine);
cumulativeTform = simtform2d(cumulativeTform.A * sRtTform.A);
imgBp = imwarp(imgB,cumulativeTform,OutputView=imref2d(size(imgB)));
% Display as color composite with last corrected frame
step(hVPlayer,imfuse(imgAp,imgBp,ColorChannels='red-cyan'));
correctedMean = correctedMean + imgBp;
ii = ii+1;
end
correctedMean = correctedMean/(ii-2);
movMean = movMean/(ii-2);
% Here you call the release method on the objects to close any open files
% and release memory.
release(hVPlayer);

Accepted Answer

Walter Roberson
Walter Roberson on 31 Oct 2022
Note that the step() call is entirely valid but in the last years the style is to use the name of the video object as a function. Instead of
step(object, data)
You would now typically write
object(data)
  2 Comments
Diego
Diego on 1 Nov 2022
Hello. Thank you for catching that. I made the following change to the code.
hVPlayer(frame)
Do you have any suggestions on how to create an mp4 output from the code above? It does not seem like the vision.player is very reliable. It sometimes opens a player and most of the time it does not so I would rather not depend on it.
Walter Roberson
Walter Roberson on 1 Nov 2022
video_filename = 'SomeAppropriateFile.mp4';
% Reset the video source to the beginning of the file
read(hVideoSrc,1);
hVWriter = VideoWriter(video_filename);
open(hVWriter);
% Process all frames in the video
movMean = rgb2gray(im2single(readFrame(hVideoSrc)));
imgB = movMean;
imgBp = imgB;
correctedMean = imgBp;
ii = 2;
cumulativeTform = simtform2d;
while hasFrame(hVideoSrc) && ii < 10
% Read in new frame
imgA = imgB; % z^-1
imgAp = imgBp; % z^-1
imgB = rgb2gray(im2single(readFrame(hVideoSrc)));
movMean = movMean + imgB;
% Estimate transformation from frame A to frame B, and fit as an s-R-t
tformAffine = cvexEstStabilizationTform(imgA,imgB);
sRtTform = cvexTformToSRT(tformAffine);
cumulativeTform = simtform2d(cumulativeTform.A * sRtTform.A);
imgBp = imwarp(imgB,cumulativeTform,OutputView=imref2d(size(imgB)));
% Display as color composite with last corrected frame
hVWriter( imfuse(imgAp,imgBp,ColorChannels='red-cyan') );
correctedMean = correctedMean + imgBp;
ii = ii+1;
end
correctedMean = correctedMean/(ii-2);
movMean = movMean/(ii-2);
% Here you call the release method on the objects to close any open files
% and release memory.
close(hVWriter);

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!