Retaining Bit depth when creating a video from Grayscale Images
23 views (last 30 days)
Show older comments
Hello, I am trying to create a video (MP4 file) from a stack of 16 bit Grayscale images. My code is below. I see that using the VideoWriter with the MPEG-4 codec, 16 bit resolution isn't possible and the images need to be down sampled to 8 bit. Is there anyway at all to create a video file and retain the original resolution?
Thanks
Jason
dt = datestr(now,'dd-mmm-yyyy HH-MM-SS'); %get current dateTime
filepath=app.SaveVidEditField.Value;
filename=fullfile(filepath,[dt,'- VidMatlab.mp4']);
diskLogger=VideoWriter(filename,'MPEG-4'); % Use the MPEG-4 Codec
diskLogger.FrameRate=app.FrameRateEditField.Value;
open(diskLogger);
ReportMessage(app,'..Disklogger Opened'); %Report message is my own function
numFrames=size(data,2);
ReportMessage(app,['..Recording To Disk Started (', num2str(numFrames),' images)']);
b=0; %Holder if want to add background to change contrast in 8 bit video
for x=1:numFrames
writeVideo(diskLogger, uint8((data{x}+b)/ 256)); %Annoyingly, Need to convert images to 8bit
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)']);
0 Comments
Answers (1)
Nandini
on 23 Jun 2023
I see that you're using MATLAB to create a video from a stack of 16-bit grayscale images. As you mentioned, the VideoWriter in MATLAB with the MPEG-4 codec does not directly support 16-bit resolution. The images need to be downsampled to 8-bit before creating the video.
In your code, you are converting the images to 8-bit by dividing them by 256 (uint8((data{x}+b)/256)), which effectively scales the pixel values from the original 16-bit range to the 8-bit range (0-255).
Unfortunately, there is no way to create a video file with the MPEG-4 codec and retain the original 16-bit resolution. The MPEG-4 codec is specifically designed for 8-bit color or grayscale images. If you want to preserve the full 16-bit resolution, you would need to use a different codec or file format that supports higher bit depths, such as a lossless format like TIFF or PNG.
Here's an example of how you can modify your code to save the images as a TIFF sequence instead of an MP4 video file:
dt = datestr(now, 'dd-mmm-yyyy HH-MM-SS'); % Get current dateTime
filepath = app.SaveVidEditField.Value;
filename = fullfile(filepath, [dt, '- VidMatlab.tif']); % Save as TIFF file
numFrames = size(data, 2);
ReportMessage(app, ['..Recording To Disk Started (', num2str(numFrames), ' images)']);
for x = 1:numFrames
imwrite(data{x}, filename, 'WriteMode', 'append', 'Compression', 'none');
end
ReportMessage(app, 'DiskLogger Stopped');
disp('Finished Saving Video');
% Check TIFF sequence properties
info = imfinfo(filename);
numFramesSaved = numel(info);
fr = app.FrameRateEditField.Value;
dur = numFramesSaved / fr;
ReportMessage(app, ['Video Saved as: ', filename, ' @', num2str(fr), 'fps (', num2str(dur, '%.1f'), ' s)']);
In this modified code, each image in the data array is saved as a separate frame in a TIFF file using the imwrite function. The Compression option is set to 'none' to ensure lossless saving. Please note that saving each frame as a separate TIFF image will result in a sequence of individual images rather than a single video file. However, this allows you to retain the original 16-bit resolution of the images.
See Also
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!