Create video from array without imagesc figure displaying

14 visualizaciones (últimos 30 días)
Zachary Swinnerton
Zachary Swinnerton el 13 de Jul. de 2020
Comentada: DGM el 2 de Dic. de 2024 a las 13:16
How do I create a video file without displaying the figure? I currently am using imagesc for each frame of the data cube (PIXELSxPIXELSxFRAMES) format. I have tried turning off visibility and it seems to step through the process even slower when not displaying the figure. This seems odd. I want the code to run faster and not display the intermediate figures as it builds the video. I already have a code that will display the final video file. I am using App Designer.
Is there anyway around displaying the figure in order to create a video file from a stack of images??

Respuestas (1)

Rahul
Rahul el 2 de Dic. de 2024 a las 9:42
In order to create a video file without using 'imagesc' function and displaying the image on figures to create a video, consider using 'VideoWriter' whose object can be created and looping through the frames of the video, or images, the video can be written to the object using 'writeVideo' function. Here is an example of 'VideoWriter' initialization:
  • Creating a 'VideoWriter' object by giving an output filename
  • Looping through array of images, 'im2frame' function to convert an image to type ' movie frame'
  • 'writeVideo' function to write the 'frame' to the video object
% Considering 'images' to be a cell array containing 3 images 'im1', 'im2', 'im3'
writerObj = VideoWriter('myVideo.avi');
open(writerObj);
for u=1:length(images)
frame = im2frame(images{u});
writeVideo(writerObj, frame);
end
close(writerObj);
I have attached the 3 images files that I have used as an example for your reference.
The following MATLAB Answer explains the example in more detail:
The following MathWorks documentations can be referred to know more:
Hope this helps! Thanks.
  1 comentario
DGM
DGM el 2 de Dic. de 2024 a las 13:16
OP never gave us any code or really told us what was going on, but the image is described as a monolithic data volume. It's implied that it's being rendered using imagesc(), screenshotted, and saved. While relying on screenshots for an image processing workflow is ridiculous, the fact that we're dealing with arbitrary volumes of data and we're using imagesc() to display it suggests that we can't simply display these frames as they come from the array.
Consider this volumetric image. Each frame is a single-channel grayscale image. The total data range is [0 2239], so it's not on standard scale (i.e. [0 65535] for uint16). If we want this to appear as it would in a plausible imagesc() rendering scheme, we'd have to do the colormapping ourselves.
% the data is a monolithic MxNxF numeric array
% in other words, it's a sequence of F gray images
% which are likely on an arbitrary scale (hence the use of imagesc())
fname = fullfile(matlabroot,'toolbox/images/imdata/BrainMRILabeled/images/vol_001.mat');
load(fname)
% setup
nframes = size(vol,3);
climits = imrange(vol);
CT = hot(256);
% process frames
writerObj = VideoWriter('myVideo.avi');
open(writerObj);
for f = 1:nframes
% convert this arbitrary-scaled gray image
% into a pseudocolored image like imagesc() would
thisframe = gray2pcolor(vol(:,:,f),CT,climits,'cdscale');
% write the frame
thisframe = im2frame(thisframe);
writeVideo(writerObj, thisframe);
end
close(writerObj);
If we had instead just fed those frames directly to im2frame(), we'd get an error.
% the data is a monolithic MxNxF numeric array
% in other words, it's a sequence of F gray images
% which are likely on an arbitrary scale (hence the use of imagesc())
fname = fullfile(matlabroot,'toolbox/images/imdata/BrainMRILabeled/images/vol_001.mat');
load(fname)
% setup
nframes = size(vol,3);
% process frames
writerObj = VideoWriter('myVideo.avi');
open(writerObj);
for f = 1:nframes
% blindly assume that the data is on a conventional scale
% frame data must be RGB or indexed color. this data is neither.
% frame data must be uint8 or unit-scale double. this data is nonstandard-scale uint16
thisframe = vol(:,:,f);
% write the frame
thisframe = im2frame(thisframe); % fails with error
writeVideo(writerObj, thisframe);
end
Error using im2frame
Can only make movie frames from image matrices of type double or uint8
close(writerObj);
If we had instead tried to expand the image to RGB and change the class to something accepted, we'd just get a completely black video. I'm not going to include the video for this one, since there's no point.
% the data is a monolithic MxNxF numeric array
% in other words, it's a sequence of F gray images
% which are likely on an arbitrary scale (hence the use of imagesc())
fname = fullfile(matlabroot,'toolbox/images/imdata/BrainMRILabeled/images/vol_001.mat');
load(fname)
% setup
nframes = size(vol,3);
% process frames
writerObj = VideoWriter('myVideo.avi');
open(writerObj);
for f = 1:nframes
% blindly assume that the data is on a conventional scale
% frame data must be RGB or indexed color. this data is neither.
% frame data must be uint8 or unit-scale double. this data is nonstandard-scale uint16
thisframe = repmat(im2uint8(vol(:,:,f)),[1 1 3]);
% write the frame
thisframe = im2frame(thisframe);
writeVideo(writerObj, thisframe);
end
close(writerObj);

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by