필터 지우기
필터 지우기

Retaining Bit depth when creating a video from Grayscale Images

조회 수: 23 (최근 30일)
Jason
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?
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)']);

답변 (1개)

Nandini
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');
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.
  댓글 수: 1
Jason
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
Thanks

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

카테고리

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by