Reputation: 473
In the following Matlab GUI code I save a TIFF image and used the waitbar function since it take sometimes few seconds, the bar is not updating and is closing automatically when the file is saved. Is it possible to add an elapsed time counter to indicate time of operation?
code snippet:
h = waitbar(0,'In process');
export_fig(handles.imageAxes,saveFileAs, '-r500');
close(h);
Upvotes: 2
Views: 432
Reputation: 32084
The following answer applies tiff images specifically.
I tried to find a general solution for imwrite
function, but I couldn't.
The solution uses Tiff class instead of using imwrite
.
Tiff class allows saving a tiff image file strip by strip, instead of saving entire image at once.
See: http://www.mathworks.com/help/matlab/ref/tiff-class.html
The following code sample saves a (relatively) large tiff image file, and display an advancing waitbar while saving is in progress:
%Simulate large image (to be saved as tiff later)
I = imread('peppers.png');
I = repmat(I, [10, 10]); %Image resolution: 5120 x 3840.
t = Tiff('I.tif', 'w');
width = size(I, 2);
height = size(I, 1);
rows_per_strip = 16; %Select 16 rows per strip.
setTag(t, 'ImageLength', height)
setTag(t, 'ImageWidth', width)
setTag(t, 'Photometric', Tiff.Photometric.RGB)
setTag(t, 'BitsPerSample', 8)
setTag(t, 'SamplesPerPixel', 3)
setTag(t, 'RowsPerStrip', rows_per_strip)
setTag(t, 'PlanarConfiguration', Tiff.PlanarConfiguration.Chunky)
setTag(t, 'Compression', Tiff.Compression.LZW)
n_strips = ceil(height / rows_per_strip); %Total number of strips.
h = waitbar(0, 'In process');
%Write the tiff image strip by strip (and advance the waitbar).
for i = 1:n_strips
y0 = (i-1)*rows_per_strip + 1; %First row of current strip.
y1 = min(y0 + rows_per_strip - 1, height); %Last row of current strip.
writeEncodedStrip(t, i, I(y0:y1, :, :)) %Write strip rows y0 to y1.
waitbar(i/n_strips, h); %Update waitbar.
drawnow %Force GUI refresh.
end
close(t)
close(h)
Upvotes: 2