How can I add timer to a stack of image and play it as a video with timer going on in the corner of it?

9 visualizaciones (últimos 30 días)
I have a stack of radiography images which are taken in time and would like to save it as a video with a TIMER in the video. The time is important to see when video plays.

Respuesta aceptada

DGM
DGM el 22 de En. de 2023
See this thread
There are various ways to generate the text as a raster image, and there are various ways to combine the text image with the main image. If you have CVT, you can use inserttext(). If you don't, your options depend on how large your images are.
If your images are large enough, you might try text rendering via figure capture. This has limitations and is generally cumbersome and can alter image content if you're not careful, but it's possible. One convenient approach to figure capture is this FEX tool:
If your images are relatively small, using figure capture is going to be severely limited by the antialiasing and available fonts. Simply put, it can't render readable text at small scales. Either you'd have to upscale your images, or you'd have to be careful about font choices. Alternatively, you can use any one of the text-to-image conversion tools that's listed in the first link.
This example uses textim() from MIMT, though no other MIMT tools are used.
% i assume you already have some image stack in the workspace
% i assume it is arranged with frames on dim 4
% i assume it is an RGB image of class uint8
[h,w,nchans,nframes,~] = size(framestack);
% create output object
outvid = VideoWriter('garbage.avi');
outvid.FrameRate = 29.97;
% load dummy image to set axes and create image object for display purposes
hi = imshow(zeros(h,w,nchans));
% process frames
invid.CurrentTime = timerange(1);
open(outvid);
for f = 1:nframes
% read the frame
thisframe = framestack(:,:,:,f);
% get text as an image block
thistime = rand(1); % generate your number somehow (this is a placeholder)
stampstr = sprintf('time: %.4f',thistime); % format it as a char somehow
stampimg = textim(stampstr,'ibm-vga-16x9'); % turn it into an image somehow
% insert text block into image somewhere
[hs,ws,~] = size(stampimg);
stampimg = repmat(stampimg,[1 1 nchans]); % expand to match number of channels
stampimg = im2uint8(stampimg); % cast and rescale to match class
thisframe(1:hs,1:ws,:) = stampimg; % northwest corner
% write and display frame
writeVideo(outvid,thisframe); % write the frame
hi.CData = thisframe; % update display
pause(1/outvid.FrameRate);
end
close(outvid);
If your images are arranged differently, or if they have different class or depth, then you'll have to make those changes. You'll have to figure out how to generate and format the timestamps you want. I'm just printing random numbers as a placeholder.

Más respuestas (0)

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by