I have a tif file that I can view it from Windows Photos App. You can download it from this link.
I tried to load it to Matlab using imread
function, however it shows an error below.
TIFF library error - 'TIFFReadDirectory: Cannot handle different values per sample for "BitsPerSample".'
I then further looking into the file's profile and find that the BitDepth and BitsPerSample value seems not correct. Also, the MaxSample value looks weird.
By checking the Matlab buildin tiff file profile, I learned that for a RGB image, BitDepth should be 24 and BitsPerSample should be [8,8,8]. However, when I tried to explicitly change them, I still get the same error.
fname = 'TifImg.tif';
info = imfinfo(fname);
% Explicitly Assign Correct Value to BitDepth and BitsPerSample (still doesn't work)
for i = 1: length(info)
info(i).BitDepth = 24;
info(i).BitsPerSample = [8 8 8];
end
% Read Tif Image
frame = imread(fname, 1, 'Info', info);
imshow(frame,[])
I hope someone can help me to load this image to Matlab and point me which profile I should change to successfully load the file.
As mentioned in the comments above, the file seemed to be corrupt. I was also unable to open it with gimp or other programs. So here is a hack. You get the size from the header with imfinfo (height by width by 3 colors), then you read the file from the end. A bit of reordering and the image is salvaged. I don't know about the large BitsPerSample you have in the header, if you use [8,8,8] you have the exact length you need. If you use larger color depth etc you don't have enough numbers in the file to fill the 520x696 pix image.
info = imfinfo('TifImg.tif');
fid = fopen('TifImg.tif','r');
s = fread(fid,'uint8=>uint8');
fclose(fid);
len = info.Height*info.Width*3;
data = s(length(s)-len+1:end);
img = reshape(data,3,info.Width,info.Height);
img = permute(img,[3,2,1]);
figure;
imshow(img)
Smart answer. I have only one question before awarding you the bounty. Actually, I am trying to read multi page tifs in the same size. I tried your way to read it but get some weired results. Can you help? Here is my code: info = imfinfo(fname); numofFrames = length(info); fid = fopen(fname,'r'); s = fread(fid,'uint8=>uint8'); fclose(fid); len = info(1).Height*info(1).Width*3*numofFrames; data = s(length(s)-len+1:end); imgStacks = reshape(data,numofFrames, 3,info(1).Width,info(1).Height); imgStacks = permute(imgStacks,[4,3,2,1]); implay(imgStacks)
are all frames in uint8? can you link an example?
Yes the image is just one frame from this multi-page tif file. Here is the link. Thanks! dropbox.com/s/oi8t10z0n57l8t7/imagestacks.tif?dl=0
your data samples are s(2797:end), I am looking into this
This requires heavy detective work. I can get some images this way but their color seems off, and if you take ii = 450 the picture is mashed up. fname = 'imagestacks.tif'; info = imfinfo(fname); numofFrames = length(info); fid = fopen(fname,'r'); s = fread(fid,'uint8=>uint8'); fclose(fid); data = s(2797:end); len = info(1).Height*info(1).Width*3; figure; for ii = 1:4 start = (ii-1)*len+1; finish = start+len-1; img = reshape(data(start:finish),3,info(1).Width,info(1).Height); img = permute(img,[3,2,1]); imshow(img) title(ii) pause end