问题描述
我正在尝试使用MATLAB函数imwrite保存许多(约500张,每张2MB)图像的蒙太奇,但是我不断收到此错误:
I am trying to save a montage of many (~500, 2MB each) images using MATLAB function imwrite, however I keep getting this error:
Error using imwrite>validateSizes (line 632)
Images must contain fewer than 2^32 - 1 bytes of data.
Error in imwrite (line 463)
validateSizes(data);
这是我正在使用的代码:
here is the code I am working with:
close all
clear all
clc
tic
file = 'ImageRegistrations.txt';
info = importdata(file);
ImageNames = info.textdata(:,1);
xoffset = info.data(:,1);
yoffset = info.data(:,2);
for i = 1:length(ImageNames);
ImageNames{i,1} = imread(ImageNames{i,1});
ImageNames{i,1} = flipud(ImageNames{i,1});
end
ImageNames = flipud(ImageNames);
for i=1:length(ImageNames)
diffx(i) = xoffset(length(ImageNames),1) - xoffset(i,1);
end
diffx = (diffx)';
diffx = flipud(diffx);
for j=1:length(ImageNames)
diffy(j) = yoffset(length(ImageNames),1) - yoffset(j,1);
end
diffy = (diffy)';
diffy = flipud(diffy);
matrix = zeros(max(diffy)+abs(min(diffy))+(2*1004),max(diffx)+abs(min(diffx))+(2*1002));
%matrix(1:size(ImageNames{1,1},1),1:size(ImageNames{1,1},2)) = ImageNames{1,1};
for q=1:length(ImageNames)
matrix((diffy(q)+abs(min(diffy))+1):(diffy(q)+abs(min(diffy))+size(ImageNames{q,1},1)),(diffx(q)+abs(min(diffx))+1):((diffx(q)+abs(min(diffx))+size(ImageNames{q,1},2)))) = ImageNames{q,1};
end
graymatrix = mat2gray(matrix);
graymatrix = flipud(graymatrix);
figure(2)
imshow(graymatrix)
imwrite(graymatrix, 'montage.tif')
toc
我使用imwrite是因为它可以将最终蒙太奇保留在完整分辨率的文件中,而如果我仅单击图形文件上的保存",则会将其保存为低分辨率文件.
I use imwrite because it perserves the final montage in a full resolution file, whereas if I simply click save on the figure file it saves it as a low resolution file.
谢谢!
推荐答案
错误确实在锡纸上说了什么.在imwrite
中输入变量的大小有某种内在的限制,现在您可以遍历它.
Error does what it says on the tin, really. There is some sort of inbuilt limitation to input variable size in imwrite
, and you're going over it.
请注意,大多数图像都以uint8
的形式存储,但是我想您最终会由于处理而导致翻倍.那会增加内存使用量.
Note that most images are stored as uint8
but I would guess that you end up with doubles as a result of your processing. That increases the memory usage.
因此,强制转换为另一种类型可能会有所帮助.在调用imwrite
之前,尝试使用im2uint8
(假设变量graymatrix
为双精度,缩放范围为0和1).
It may be, therefore, that casting to another type would help. Try using im2uint8
(presuming your variable graymatrix
is double, scaled between 0 and 1), before calling imwrite
.
这篇关于使用MATLAB以全分辨率将许多图像的蒙太奇图像保存为一个大图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!