我正在尝试编写一个多页 tiff 文件,该文件是 128 像素 x 128 像素 x 122000 帧的 16 位无符号整数。 ImageJ 或简短的 Python 脚本可以在快速机器上在一分钟内完成此操作。在同一台机器上,无论使用我尝试过的任何方法,MATLAB 都可能需要几天时间才能完成此操作。这是我到目前为止所尝试过的:
使用imwrite将IMG写入test.tif(MATLAB文档推荐)
tic
numframes=size(IMG,3);
divider=10^(floor(log10(numframes))-1);
imwrite(IMG(:,:,1),'test.tif');
for i=2:numframes;
imwrite(IMG(:,:,i),'test.tif','WriteMode','append');
if (round(i/divider)==i/divider)
fprintf('Frame %d written in %.0f seconds, %2d percent complete, time left=%.0f seconds \n', ...
i, toc, i/numframes*100, (numframes - i)/(i/toc));
end
end
这会产生以下输出:
104 秒写入第 10000 帧,8.196721e+00% 完成,
剩余时间=1163秒 296秒写入第20000帧,
1.639344e+01 完成百分比,剩余时间=1509 秒 590 秒写入帧 30000,2.459016e+01 完成百分比,剩余时间=1809
1035 秒写入 40000 帧,3.278689e+01%
完成,剩余时间=2121秒 1682秒写入第50000帧,
4.098361e+01% 完成,剩余时间=2421 秒
请注意,随着写入更多帧,时间呈指数增长。
直接使用Tiff类
if bigtiff
t = Tiff(fname,'w8');
else
t = Tiff(fname,'w');
end
tagstruct.ImageLength = size(image,1);
tagstruct.ImageWidth = size(image,2);
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
if bitspersamp==16
tagstruct.BitsPerSample = 16;
end
if bitspersamp==32
tagstruct.BitsPerSample = 32;
end
tagstruct.SamplesPerPixel = 1;
tagstruct.RowsPerStrip = 256;
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
tagstruct.Software = 'MATLAB';
t.setTag(tagstruct);
t.write(image(:,:,1));
numframes = size(image,3);
divider = 10^(floor(log10(numframes))-1);
tic
for i=2:numframes
t.writeDirectory();
t.setTag(tagstruct);
t.write(image(:,:,i));
if (round(i/divider)==i/divider)
fprintf('Frame %d written in %.0f seconds, %2d percent complete, time left=%.0f seconds \n', ...
i, toc, i/numframes*100, (numframes - i)/(i/toc));
end
end
t.close();
这会产生以下输出:
Frame 10000 written in 66 seconds, 8.196721e+00 percent complete, time left=743 seconds
Frame 20000 written in 225 seconds, 1.639344e+01 percent complete, time left=1145 seconds
Frame 30000 written in 481 seconds, 2.459016e+01 percent complete, time left=1474 seconds
Frame 40000 written in 915 seconds, 3.278689e+01 percent complete, time left=1877 seconds
Frame 50000 written in 1512 seconds, 4.098361e+01 percent complete, time left=2177 seconds
尝试使用 BigTIFF 库不起作用
以下讨论如下:http://blogs.mathworks.com/steve/2013/08/07/tiff-bigtiff-and-blockproc/ http://blogs.mathworks.com/steve/2013/08/07/tiff-bigtiff-and-blockproc/
我尝试将代码转换为使用 uint16 数据,将第 73 行更改为:
obj.TiffObject.setTag('BitsPerSample', 16);
但写完后
outFileWriter = bigTiffWriter('test.tif', inFileInfo(1).Height, inFileInfo(1).Width, tileSize(1), tileSize(2));
for i=1:122000
blockproc(IMG(:,:,i),tileSize,@(b) b.data,'Destination',outFileWriter);
if rem(i,10000)==0
fprintf('Frame %d done\n',i)
end
end
尝试读回时出现以下错误:
Unexpected Standard exception from MEX file.
What() is:std::bad_alloc
..
Error in imtifinfo (line 27)
raw_tags = tifftagsread(filename,0,0,0);
Error in imfinfo (line 183)
info = feval(fmt_s.info, filename);
Error in TiffReader (line 11)
InfoImage=imfinfo(fname);
在相关说明中,在磁盘上预分配正确大小的文件没有什么区别
我认为这是一个文件 I/O 问题的可能性很小,在这种情况下预分配磁盘上的空间可能是相关的,所以我尝试了这里提到的内容:http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/241072 http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/241072,即:
% Create the file
fh = javaObject('java.io.RandomAccessFile', 'test.dat', 'rw');
% Allocate the right amount of space
fh.setLength(1024);
% Close the file
fh.close();
但这没有什么区别。
任何帮助将不胜感激。