在 Matlab 中读写灰度 TIF 文件
Reading & Writing Grayscale TIF files in Matlab
我正在尝试从大型灰度 TIF 文件中提取(非常)大量的子图像,并使用 MATLAB 将每个图像保存为 GIF、PNG 或什至另一个 TIF。我可以使用 imshow(sub(:,:,1),cmap)
显示单个图像,但是当我尝试将数据写入图像文件时,生成的文件只是 101x101 像素的白色方块。在 imwrite
中使用 cmap
参数会产生相同的结果,就像更改图像格式一样(我尝试过使用 PNG、TIF、GIF 和 JPG,但没有成功)。根据 Windows 资源管理器中的 属性 菜单,文件 a.tif
是 16 位的。任何帮助表示赞赏。我真的无计可施了。
% Import coordinates array and correct for multiplication by 10
datafile = 'data.xlsx';
coords = xlsread(datafile,1,'G2:H13057');
x = coords(:,1) ./ 10;
y = coords(:,2) ./ 10;
r = 50;
[img, cmap] = imread('a.tif'); % import the image
s = 2*r+1; % scalar of size of each submatrix in the array (sise of image)
sub = zeros(s,s,num); % create 3D matrix/array of matrices. Each submatrix corresponds to 50 px box around each point
i = 1:4;
subrgb = zeros(s,s,num);
for i=1:4
sub(:,:,i) = img((y(i)-r):(y(i)+r),(x(i)-r):(x(i)+r));
filename = 'dot_%d.png';
filename = sprintf(filename,i);
imwrite(sub(:,:,i),filename,'png');
end
尝试更改行:
sub = zeros(s,s,num);
至:
sub = zeros(s,s,num,class(img));
我假设问题是 sub 是 double 类型。
祝你好运
我正在尝试从大型灰度 TIF 文件中提取(非常)大量的子图像,并使用 MATLAB 将每个图像保存为 GIF、PNG 或什至另一个 TIF。我可以使用 imshow(sub(:,:,1),cmap)
显示单个图像,但是当我尝试将数据写入图像文件时,生成的文件只是 101x101 像素的白色方块。在 imwrite
中使用 cmap
参数会产生相同的结果,就像更改图像格式一样(我尝试过使用 PNG、TIF、GIF 和 JPG,但没有成功)。根据 Windows 资源管理器中的 属性 菜单,文件 a.tif
是 16 位的。任何帮助表示赞赏。我真的无计可施了。
% Import coordinates array and correct for multiplication by 10
datafile = 'data.xlsx';
coords = xlsread(datafile,1,'G2:H13057');
x = coords(:,1) ./ 10;
y = coords(:,2) ./ 10;
r = 50;
[img, cmap] = imread('a.tif'); % import the image
s = 2*r+1; % scalar of size of each submatrix in the array (sise of image)
sub = zeros(s,s,num); % create 3D matrix/array of matrices. Each submatrix corresponds to 50 px box around each point
i = 1:4;
subrgb = zeros(s,s,num);
for i=1:4
sub(:,:,i) = img((y(i)-r):(y(i)+r),(x(i)-r):(x(i)+r));
filename = 'dot_%d.png';
filename = sprintf(filename,i);
imwrite(sub(:,:,i),filename,'png');
end
尝试更改行:
sub = zeros(s,s,num);
至:
sub = zeros(s,s,num,class(img));
我假设问题是 sub 是 double 类型。
祝你好运