trying to speed up image exports

77 次查看(过去 30 天)
Justin Bell
Justin Bell 2024-11-2,4:35
评论: DGM 2024-11-5,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 个评论
Walter Roberson
Walter Roberson 2024-11-2,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() ?)
Justin Bell
Justin Bell 2024-11-2,20:14
@Walter Roberson good point, to clarify the use case is along the lines of
X=rand(1000,1); Y = rand(1000,1);
figure()
scatter(X,Y)
saveas(gcf, outname.png, 'png')
about 1000 times inside of some loops. currently not using subplots or tiled layouts, so a robust solution is always nice - but if something works for all plots but those, that is still a win for me.
I need to do a few more tests, but interstingly enough, using saveas ppm was much slower, but when I duplicate your test using imwrite it is faster. I couldnt test this with exportgraphics(gcf, 'outfile.fileType') because export graphics doesnt support ppm

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2024-11-2,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)
T = 8x1 table
times ________ .bmp 0.028122 .jpg 0.016113 .jp2 0.33083 .pbm 0.025394 .png 0.028225 .ppm 0.012019 .ras 0.015207 .tif 0.11636
  3 个评论
Justin Bell
Justin Bell 2024-11-5,15:10
Thanks for the detailed response and useful code snippet! I've been experimenting using this with different plot types. Interestingly enough, different plots will save out at different relative rates. For example, if there are multiple colors in the scatter or if there are alpha layers, the faster plot will be different.
The overall answer seems like "it depends on the machine and type of plot" but now using the loops you suggested, I can dial things in. Interestingly, exportgraphics tends to match saveas in terms of which formats are fastest, but imwrite (which I don't really use) seems inversely related.
DGM
DGM 2024-11-5,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)
T = 6x1 table
times _______ .bmp 0.68261 .jpg 0.45181 .pbm 0.53088 .png 0.50899 .ppm 0.45058 .tif 0.4842
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.

请先登录,再进行评论。

更多回答(1 个)

Subhajyoti
Subhajyoti 2024-11-2,13:54
编辑:Subhajyoti 2024-11-2,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
Elapsed time is 119.392816 seconds.
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")
ans =
Figure (1) with properties: Number: 1 Name: '' Color: [1 1 1] Position: [1629 1510 583 437] Units: 'pixels' Use GET to show all properties
Refer to the following resources to know more importing and exporting Figures in MATLAB:
Additionally, you can refer to the following resources to know more about saving Plot as Image or Vector Graphics File in MATLAB:

类别

Help CenterFile Exchange 中查找有关 Printing and Saving 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by