trying to speed up image exports
77 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Justin Bell
il 2 Nov 2024 alle 4:35
Commentato: DGM
il 5 Nov 2024 alle 19:45
Have been spending time running profiler to optimize code. At this point I've got things mostly trimmed down but there are a LOT of graphs, and I can spend an hour in print/saveas exporting images. I was looking at this post
and noticed that export was around 5-10% faster than using saveas. I've tried saving as svg instead of png, sometimes it is faster, sometimes its slower (for the same plots/dataset). There are some old posts from 2010-14 on speed-ups, but I've got to imagine those aren't as fast now that Matlab has native functions for this.
Is there any guidance on how to get improvements on these processes? I can be flexible on file format/resolution etc. for anything that going to be at least 10% faster.
2 Commenti
Walter Roberson
il 2 Nov 2024 alle 19:36
To clarify:
You have individual axes that you wish to save as images? Or are they figures that you wish to save as images (including the possibility of subplot() or tiledlayout() ?)
Risposta accettata
Walter Roberson
il 2 Nov 2024 alle 18:48
This test suggests that writing to ppm might potentially be faster, with writing to JPEG2 being notably slower.
data = randi([0 255], 128, 160, 3, 5, 'uint8');
formats = [".bmp"; ".jpg"; ".jp2"; ".pbm"; ".png"; ".ppm"; ".ras"; ".tif"];
n_formats = length(formats);
times = zeros(n_formats,1);
for J = 1 : n_formats
form = formats(J);
tic
for K = 1 : size(data,4)
imwrite(data(:,:,:,K), "test"+K+form);
end
times(J) = toc;
end
T = table(times, 'rownames', formats)
3 Commenti
DGM
il 5 Nov 2024 alle 19:45
FWIW, this is much faster for me.
formats = [".bmp"; ".jpg"; ".pbm"; ".png"; ".ppm"; ".tif"];
n_formats = length(formats);
times = zeros(n_formats,1);
X = rand(1000,1); Y = rand(1000,1);
scatter(X,Y)
fig = gcf;
for J = 1 : n_formats
form = formats(J);
tic
for K = 1 : 5
outpict = frame2im(getframe(gcf));
imwrite(outpict, "test"+K+form);
end
times(J) = toc;
end
T = table(times, 'rownames', formats)
These are the times on a 12 year old computer that came out of a dumpster. PNG with saveas() doesn't look as bad, but using getframe()/imwrite() is faster
% using saveas()
% ______
% .bmp 2.5871
% .jpg 12.795
% .pbm 4.7508
% .png 2.4091
% .ppm 11.929
% .tif 2.3482
% using getframe()/imwrite()
% _______
% .bmp 0.87231
% .jpg 0.25393
% .pbm 0.2676
% .png 0.36382
% .ppm 0.68319
% .tif 0.21874
There seems to be a lot of variability using saveas(), but the general theme is that they're all slower.
% using saveas() (another run)
% ______
% .bmp 5.6396
% .jpg 1.9731
% .pbm 4.5735
% .png 2.497
% .ppm 11.676
% .tif 7.4597
Why are they slower? We shouldn't expect that the images written by the two methods are the same size, but even accounting for the difference in size, saveas() is still a lot slower.
Più risposte (1)
Subhajyoti
il 2 Nov 2024 alle 13:54
Modificato: Subhajyoti
il 2 Nov 2024 alle 13:55
You can export the figures to FIG-files ('.fig') using the “savefig” function. This gives better performance at the major performance bottleneck, which is exporting the figure to external files.
Here, in the following implementation, I have clocked the running time for the function.
tic
% generate random data. Plot them and save the plot to PNG file
for i = 1:1000
%Preparing data for plotting
x = 1:1000;
% generate 1000 random data following normal distribution
y = 255*randn(1,1000);
% Save the plot to PNG file without displaying it
fig = figure('Visible','off');
plot(x,y);
title('Random data','Interpreter','latex');
xlim([0 1000]);
ylim([100 255]);
xlabel('X','Interpreter','latex');
ylabel('Y','Interpreter','latex');
set(gca,'TickLabelInterpreter','latex');
% Save the plot to PNG file
filename = ['plot' num2str(i) '.fig'];
savefig(fig,filename);
close(fig);
end
toc
The above implementation exports at '500 figures/minutes', which is around 5x faster than PNG-format. You can further improve the output time by setting the ‘Visible’ parameter of the ‘figure’ object to ‘off’.
You can open the saved figure using ‘openfig’ from any MATLAB script or Command-Line as follows:
openfig("plot1.fig", "visible")
Refer to the following resources to know more importing and exporting Figures in MATLAB:
- https://www.mathworks.com/help/matlab/ref/savefig.html
- https://www.mathworks.com/help/matlab/ref/openfig.html
Additionally, you can refer to the following resources to know more about saving Plot as Image or Vector Graphics File in MATLAB:
0 Commenti
Vedere anche
Categorie
Scopri di più su Printing and Saving in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!