Multipage Tiff write in MATLAB doesn't work

2019-08-30 23:45发布

I'm reading in a Tiff using the below function, which works fine, but when I try to use my write function to write that same Tiff back to a different file, it's all 255's. Does anyone know how to fix this? Thanks, Alex.

function Y = tiff_read(name)
% tiff reader that works

info = imfinfo(name);
T = numel(info);

d1 = info(1).Height;
d2 = info(1).Width;

Y = zeros(d1,d2,T);
for t = 1:T
    temp = imread(name, t, 'Info',info);
    Y(:,:,t) = temp(1:end,1:end);
end

% Tiff writer that doesn't work
function tiff_write(Y,name)
% Y should be 3D, name should end in .tif
T = size(Y,3);
imwrite(Y(:,:,1),name);
for t = 2:T
    imwrite(Y(:,:,t),name,'WriteMode','append');
end

1条回答
时光不老,我们不散
2楼-- · 2019-08-31 00:35

Try using this line :

Y = zeros(d1,d2,T,'uint16');

instead of this one:

Y = zeros(d1,d2,T);

Your data are likely in uint16 format and when you export you clip the maximum value to 255 (uint8), which makes pixel with values greater than 255 (a LOT of them if your data is in uint16) appear white.

Otherwise you might want to use this line:

function tiff_write(Y,name)
   % Y should be 3D, name should end in .tif
   for t = 2:T
     imwrite(Y(:,:,t)/255,name,'WriteMode','append');
   end
查看更多
登录 后发表回答