添加到等待栏的经过时间计数器 - Matlab

时间:2016-08-07 04:54:46

标签: matlab

在下面的Matlab GUI代码中,我保存了一个TIFF图像并使用了waitbar函数,因为它有时需要几秒钟,条形图不会更新,并且在保存文件时会自动关闭。是否可以添加经过时间计数器来指示操作时间?

代码段:

h = waitbar(0,'In process'); 
export_fig(handles.imageAxes,saveFileAs, '-r500');
close(h);

1 个答案:

答案 0 :(得分:2)

以下答案专门适用tiff张图片 我试图找到imwrite函数的一般解决方案,但我不能。

解决方案使用 Tiff类,而不是使用imwrite Tiff类允许逐条保存tiff图像文件,而不是一次保存整个图像 请参阅:http://www.mathworks.com/help/matlab/ref/tiff-class.html

以下代码示例保存(相对)大的tiff图像文件,并在保存过程中显示前进的等待栏:

%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)

enter image description here