Retaining Bit depth when creating a video from Grayscale Images

조회 수: 13 (최근 30일)
Jason 2023년 6월 23일
댓글: Jason 2023년 6월 23일
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?
dt = datestr(now,'dd-mmm-yyyy HH-MM-SS'); %get current dateTime
filename=fullfile(filepath,[dt,'- VidMatlab.mp4']);
diskLogger=VideoWriter(filename,'MPEG-4'); % Use the MPEG-4 Codec
ReportMessage(app,'..Disklogger Opened'); %Report message is my own function
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
ReportMessage(app,'DiskLogger Stopped');
disp('Finished Saving Video')
disp('checking file')
v = VideoReader(filename);
fr=v.FrameRate; %double
ReportMessage(app,['Video Saved as: ',filename, ' @',num2str(fr),'fps (',num2str(dur,'%.1f'),' s)']);

답변 (1개)

Nandini 2023년 6월 23일
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');
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.
  댓글 수: 1
Jason 2023년 6월 23일
Thankyou. I actually already have the images as individual tiffs - I then load them into a cell array to make the video!
Is there anyother non MP4 way of retaining the resolution as a video

댓글을 달려면 로그인하십시오.


Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기




Community Treasure Hunt

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

Start Hunting!

Translated by