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

4 visualizaciones (últimos 30 días)
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);

Respuesta aceptada

Walter Roberson
Walter Roberson el 31 de Oct. de 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 comentarios
Diego
Diego el 1 de Nov. de 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 el 1 de Nov. de 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);

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Feature Detection and Extraction en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by