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

5 visualizaciones (últimos 30 días)
Hamed Bolandi
Hamed Bolandi el 2 de Dic. de 2021
Respondida: Walter Roberson el 9 de En. de 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?
clc;
clear;
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
end
% images_rgb1=images_3chn(:,:,1,1);
% images_rgb2=images_3chn(:,:,2,1);
% images_rgb3=images_3chn(:,:,3,1);
writeVideo(v,images_3chn);
close(v);

Respuestas (3)

DGM
DGM el 2 de Dic. de 2021
Editada: DGM el 2 de Dic. de 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?
  2 comentarios
Hamed Bolandi
Hamed Bolandi el 2 de Dic. de 2021
I am not sure about the other softwares. If I save it as a .mat file the size of each sample will be around 60 MB since the orginal size of each sample is (600,600,3,100) and there thousands of those.
DGM
DGM el 2 de Dic. de 2021
Editada: DGM el 2 de Dic. de 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
save('A.mat','A')
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;
t.setTag(tagstruct);
for f = 1:size(A,4)
setTag(t,tagstruct);
write(t,A(:,:,:,f));
writeDirectory(t);
end
t.close();
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.

Iniciar sesión para comentar.


yanqi liu
yanqi liu el 3 de Dic. de 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 el 9 de En. de 2022

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by