Create an MP4 video from a set of 16 bit images (mono)

37 views (last 30 days)
Hello, I have a cell array of images (app.imgArray) that are all 16 bit. Im trying to output them as a video file (preferable mp4), but Im not having any sucess yet. I've realised that the format has to be either double or unit8 (from error messages!), so when I convert to uint8, the code finishes, however, the mp4 video file is just white.
it also seems to ignore the frame rate=1 I code. It reads back 30fps.
dt = datestr(now,'dd-mmm-yyyy HH-MM-SS'); %get current dateTime
filepath='F:\Temp\';
filename=fullfile(filepath,[dt,'- VidMatlab.mp4']);
diskLogger=VideoWriter(filename,'MPEG-4'); % Use the MPEG-4 Codec
disklogger.FrameRate=1; %only have about 8 images and want to see them.
open(diskLogger);
ReportMessage(app,'..Disklogger Opened'); %My reporting function
data=app.imgArray;
class(data{1})
numFrames=size(data,2);
ReportMessage(app,['..Recording To Disk Started ', num2str(numFrames)]);
for x=1:numFrames
writeVideo(diskLogger, uint8(app.imgArray{x}));
end
ReportMessage(app,'DiskLogger Stopped');
close(diskLogger);
disp('Finished Saving Video')
%check
disp('checking file')
v = VideoReader(filename);
fr=v.FrameRate; %double
dur=v.Duration;
ReportMessage(app,['Video Saved as: ',filename, ' @',num2str(fr),'fps (',num2str(dur,'%.1f'),' s)']);
  1 Comment
Richard Burnside
Richard Burnside on 14 Jun 2023
In the past I have found it necessary to render each frame on a figure then use "getframe" before encoding the frame to video. That was years ago so maybe that it no longer necessary.

Sign in to comment.

Answers (1)

Jason
Jason on 14 Jun 2023
I think I've nearly done it myself.
1: The framerate issue was just a typo (missed the capital "L"
it should be diskLogger.FrameRate=1; instead of disklogger.FrameRate=1;
2: I can now see the video, i.e. its no longer white by dividing by 256 when I cast to uint8.
writeVideo(diskLogger, uint8(app.imgArray{x}/256));
They are however slightly darker than the full 16 bit. Are there any tricks to try to lighten up the images (video) more - i.e. make the darker areas a bit more lighter rather than black?
Thanks

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!