insert text into videos from 4 different folders containing *png files

1 Ansicht (letzte 30 Tage)
yijun guo
yijun guo am 13 Apr. 2023
Kommentiert: DGM am 11 Dez. 2023
I would like to generate videos from 4 different folders, which are "CamPersp_kurzDach", "CamPersp_sDach", "CamX", "CamY"
and i would like also to add the text containing the filename information into the videos.
i have tried the following code. Which doesn't work for 4 folders, but only 2 folders.
and the text which I added, does't work.
by the way, i don't have library which support inserttext
clc
clear all
cd CamPersp_kurzDach
FIR = dir('*.png');
cd ..
cd CamPersp_sDach
FOR = dir('*.png');
cd ..
cd CamX
FIR1 = dir('*.png');
cd ..
cd CamY
FOR1 = dir('*.png');
cd ..
v1 = VideoWriter('output.avi');
open(v1);
for kk = 1:min(numel(FIR),200) % loop through the first 200 png files
filePath_IR = fullfile(FIR(kk).folder,FIR(kk).name);
filePath_OR = fullfile(FOR(kk).folder,FOR(kk).name);
filePath_IR1 = fullfile(FIR1(kk).folder,FIR1(kk).name);
filePath_OR1 = fullfile(FOR1(kk).folder,FOR1(kk).name);
%I = imread(filePath);
%writeVideo(v,I);
F_IR = imread(filePath_IR1);
F_OR = imread(filePath_OR1);
F_IR = imread(filePath_IR1);
F_OR = imread(filePath_OR1);
if kk == 1
size_OR = size(F_OR,[1,2]);
end
F_IR = imresize(F_IR,size_OR); % <- Added this line
F_OR = imresize(F_OR,size_OR); % <- Added this line
bothImages = [F_IR, F_OR];
figure;
imshow(bothImages);
% add the filename as text to the image
%textImg = insertText(bothImages,[10 10],FIR(kk).name, 'FontSize',18,'BoxColor','white','BoxOpacity',0.7);
[filepath,name,ext] = fileparts(FIR(kk).name);
textString = strcat('time=', num2str(str2num(name)/1000),'s');
position = [10 10];
fontSize = 18;
color = [100 100 100]/255;
boxColor = [0 0 0];
boxOpacity = 0.7;
text(position(1),position(2),textString,'FontSize',fontSize,'Color',color,'BackgroundColor',boxColor,'Margin',1);
hFrame = getframe(gca); %// Get content of figure
%imwrite(hFrame.cdata,'MyImage.tif','tif') %// Save the actual data, contained in the cdata property of hFrame
imwrite(bothImages,strcat(fullfile(FIR(kk).folder,name),'_new.tif'));
writeVideo(v1, bothImages);
close all
end
close(v1);

Antworten (1)

DGM
DGM am 13 Apr. 2023
There are a number of other tools on the File Exchange for inserting text into images or for creating text images which can then be composited with an image.
It depends what sort of properties you want (colors, font face, size, antialiasing). Few of the tools provide antialiasing, and getting both FG/BG coloring will probably require an extra compositing step beyond what the example shows.
For reasons of fidelity and consistency, I'd personally avoid figure capture if at all possible.
  4 Kommentare
DGM
DGM am 13 Apr. 2023
As to the code not working,
% you get four file paths
filePath_IR = fullfile(FIR(kk).folder,FIR(kk).name);
filePath_OR = fullfile(FOR(kk).folder,FOR(kk).name);
filePath_IR1 = fullfile(FIR1(kk).folder,FIR1(kk).name);
filePath_OR1 = fullfile(FOR1(kk).folder,FOR1(kk).name);
% ...
% but you only use two
% these assignments are redundant
F_IR = imread(filePath_IR1);
F_OR = imread(filePath_OR1);
F_IR = imread(filePath_IR1);
F_OR = imread(filePath_OR1);
DGM
DGM am 11 Dez. 2023
FWIW, MIMT now has a higher level convenience tool for doing this. See puttext()
inpict = imread('peppers.png'); % an RGB image (uint8)
% get whatever text
thistext = sprintf('Frame %04d',123); % however you want
% text props
gravity = 'nw'; % the reference location from which to offset
offset = [80 20]; % offset from Nw corner [y x]
fgcolor = [0 1 0.5]; % tuples can be I/IA/RGB/RGBA
bgcolor = [0.5 1 1 0.3];
font = 'wyse-700b'; % same fonts used by textim()
% assemble the image
outpict = puttext(inpict,thistext,'font',font,'offset',offset,...
'fgcolor',fgcolor,'bgcolor',bgcolor,'gravity',gravity);
% show it
imshow2(outpict,'invert')
It basically does the same composition as described before.

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by