Matlab 读取 Tif 文件显示错误 "Cannot handle different values per sample for "BitsPerSample"。"
Matlab Read Tif File shows Error "Cannot handle different values per sample for "BitsPerSample"."
我有一个可以从 Windows 照片应用程序查看的 tif 文件。可以从这个link下载。
我尝试使用 imread
函数将它加载到 Matlab,但是它在下面显示错误。
TIFF 库错误 - 'TIFFReadDirectory:无法处理每个样本的不同值
"BitsPerSample".'
然后我进一步查看文件的配置文件,发现 BitDepth 和 BitsPerSample 值似乎不正确。此外,MaxSample 值看起来很奇怪。
通过检查 Matlab buildin tiff 文件配置文件,我了解到对于 RGB 图像,BitDepth 应为 24,BitsPerSample 应为 [8,8,8]。然而,当我试图明确地改变它们时,我仍然得到同样的错误。
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,[])
我希望有人可以帮助我将此图像加载到 Matlab 并指出我应该更改哪个配置文件才能成功加载文件。
正如上面评论中提到的,文件似乎已损坏。我也无法用 gimp 或其他程序打开它。所以这是一个黑客。您使用 imfinfo 从 header 中获取大小(高度乘以宽度乘以 3 种颜色),然后从末尾读取文件。稍微重新排序,图像就被挽救了。我不知道你在 header 中的大 BitsPerSample,如果你使用 [8,8,8],你就有你需要的确切长度。如果您使用更大的颜色深度等,则文件中没有足够的数字来填充 520x696 像素的图像。
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)
我有一个可以从 Windows 照片应用程序查看的 tif 文件。可以从这个link下载。
我尝试使用 imread
函数将它加载到 Matlab,但是它在下面显示错误。
TIFF 库错误 - 'TIFFReadDirectory:无法处理每个样本的不同值 "BitsPerSample".'
然后我进一步查看文件的配置文件,发现 BitDepth 和 BitsPerSample 值似乎不正确。此外,MaxSample 值看起来很奇怪。
通过检查 Matlab buildin tiff 文件配置文件,我了解到对于 RGB 图像,BitDepth 应为 24,BitsPerSample 应为 [8,8,8]。然而,当我试图明确地改变它们时,我仍然得到同样的错误。
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,[])
我希望有人可以帮助我将此图像加载到 Matlab 并指出我应该更改哪个配置文件才能成功加载文件。
正如上面评论中提到的,文件似乎已损坏。我也无法用 gimp 或其他程序打开它。所以这是一个黑客。您使用 imfinfo 从 header 中获取大小(高度乘以宽度乘以 3 种颜色),然后从末尾读取文件。稍微重新排序,图像就被挽救了。我不知道你在 header 中的大 BitsPerSample,如果你使用 [8,8,8],你就有你需要的确切长度。如果您使用更大的颜色深度等,则文件中没有足够的数字来填充 520x696 像素的图像。
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)