This error message typically arises when MATLAB attempts to load a very large TIFF image entirely into memory, exceeding the allowable size for a single variable. This limit depends on the operating system, MATLAB version, and available system memory. Additional details on maximum allowed variable size can be found in the following MATLAB Central discussion:
To address this issue, we can access the image incrementally rather than loading it in full. MATLAB’s low-level “Tiff” class provides a suitable approach by enabling tile-by-tile or strip-by-strip reading, thereby conserving memory.
Below is a code snippet demonstrating how to read a TIFF image in strips which works well on a sample 625MB TIFF image:
t = imread('test_046.tif', 'tiff');
% Get image metadata
imageInfo = imfinfo(' test_046.tif');
rowsPerStrip = t.getTag('RowsPerStrip');
imageHeight = imageInfo.Height;
imageWidth = imageInfo.Width;
numChannels = t.getTag('SamplesPerPixel');
% Preallocate full image array
fullImage = zeros(imageHeight, imageWidth, numChannels, 'uint16');
% Read image strip by strip
for row = 1:rowsPerStrip:imageHeight
stripIndex = ceil(row / rowsPerStrip);
strip = t.readEncodedStrip(stripIndex);
numRows = size(strip, 1);
fullImage(row:row+numRows-1, :, :) = strip;
end
t.close();
For more information, refer to the following documentation of “tiff” and “imfinfo” used in the above code snippet:
