How to write a video file without changing the pixel values?

6 views (last 30 days)
Hamed Bolandi
Hamed Bolandi on 2 Dec 2021
Answered: Walter Roberson on 9 Jan 2022
I want to write a video file using 60x60 image with 10 frames and 3 chanels (60,60,3,10). The pixel values of the first channel is between 0 and 1, all the pixel values for 2nd, and 3rd channels are zero. The issue is that the VideoWritter convert the pixel values to uint8 and the pixel values are changing after writing the video. Is there any way to save the orginal pixel values?
s = rng;
data = rand(60,60,10);
v = VideoWriter('data','MPEG-4');
v.FrameRate = 1; % create the video writer with 1 fps
open(v); % open the video writer
images = zeros(60,60,10,'single'); % Allocate space
images_3chn = zeros(60,60,3,10,'single'); % Allocate space for 4D matrix
images_3chn(:,:,1,:) = data ;
for j=1:size(images,3)
images_3chn(:,:,:,j) = im2single((images_3chn(:,:,:,j))); % convert to 3 channels
% images_rgb1=images_3chn(:,:,1,1);
% images_rgb2=images_3chn(:,:,2,1);
% images_rgb3=images_3chn(:,:,3,1);

Answers (3)

DGM on 2 Dec 2021
Edited: DGM on 2 Dec 2021
As far as I know, no. Anyone is free to correct me on that.
I don't know of any common formats for lossless video that use floating-point data and are also supported by MATLAB tools. The video tools support lossless compressed JPEG2000 files via the 'Archival' profile, but not for floating-point data.
See the docs for VideoWriter
If there were such a format, would any other software be able to use it? If not, then what's the point of using a video file format? If all you need to do is store a MxNx3xF numeric array in a floating-point format for use in MATLAB, why not just use a .mat file?
DGM on 2 Dec 2021
If you have 1000 samples of that size, that's 432GB. If you're forced to use lossless storage methods with floating point data, I don't know if you're not going to save much space.
Mat-files support compression
% this array occupies 432MB
A = single(rand(600,600,3,100));
% as a compressed V7.3 matfile, the file is 389MB
and it's about as good as you can get using TiFF stacks
% 388MB using AdobeDeflate or Deflate compression
% call Tiff.Compression for a list of compression options
% most won't be useful in this scenario (32b FP)
filename = 'A.tiff';
t = Tiff(filename, 'w');
tagstruct.ImageLength = size(A, 1);
tagstruct.ImageWidth = size(A, 2);
tagstruct.Compression = Tiff.Compression.Deflate;
tagstruct.SampleFormat = Tiff.SampleFormat.IEEEFP;
tagstruct.Photometric = Tiff.Photometric.RGB;
tagstruct.BitsPerSample = 32;
tagstruct.SamplesPerPixel = size(A,3);
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
for f = 1:size(A,4)
Of course, a random matrix probably won't compress as well as actual image data, lossless or otherwise. Using a test image:
% this array occupies 432MB
A = im2single(repmat(imresize(imread('cameraman.tif'),[600 600]),[1 1 3 100]));
This compresses to 118MB using a Mat-File. Using Deflate compression with TIFF, it's 35MB.
A video compression format that can also reduce redundant interframe information would be beneficial, but like I said, I don't know of any that are lossless, support FP, and are supported in MATLAB.

Sign in to comment.

yanqi liu
yanqi liu on 3 Dec 2021
yes,sir,may be use
v = VideoWriter('out.avi','Uncompressed AVI');
to creat avi file use 'Uncompressed AVI'
but,as @DGM said,the out file will use some method to compress data and save to file, it is fact

Walter Roberson
Walter Roberson on 9 Jan 2022

Community Treasure Hunt

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

Start Hunting!