Importing a "Large (2.5GB)" TIFF file with 'imread' function

2 views (last 30 days)
Hello,
The end goal of this code is to calculate surface roughness from a large TIFF file where the z value is the greyscale. Here I am trying to read the TIFF image.
function open_image_Callback(hObject, eventdata, handles)
[filename, pathname]=uigetfile('*.*','MultiSelect','on','Open images');
cd(pathname)
[N,M]=size(filename);
image=imread(char(filename{1}));
[n,m,p]=size(image);
if p==3
image=rgb2gray(image);
data(:,:,1)=double(image);
data(:,:,M)=double(image);
else
data(:,:,1)=double(image);
data(:,:,M)=double(image);
end
for i=2:M
image=imread(char(filename{i}));
[n,m,p]=size(image);
if p==3
image=rgb2gray(image);
data(:,:,i-1)=double(image);
else
data(:,:,i-1)=double(image);
end
end
but I get the error :
Brace indexing is not supported for variables of this type.
Error in roughnessGUI>open_image_Callback (line 86)
image=imread(char(filename{1}));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in roughnessGUI (line 45)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)roughnessGUI('open_image_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating Menu Callback.
Thanks for your help!

Answers (2)

Jan
Jan on 6 Apr 2021
Edited: Jan on 6 Apr 2021
The error message means, that filename is a char vector. Then the index {1} would work with a cell string, which would be replied by uigetfile, if multiple files are selected.
If only 1 file is selected, convert it to a cell string also, which is scalar in this case:
[filename, pathname] = uigetfile('*.*','MultiSelect','on','Open images');
filename = cellstr(filename); % Remains a cell string if it is one already
M = numel(filename);
Instead of using cd() to change the current folder, it is much safer to use absolute path names:
image = imread(fullfile(pathname, filename{1}));
Note: filename{1} replies a char already. There is no need to convert this by char() again.
In your code data(:,:,1) is overwritten in all cases. A simplified version:
function open_image_Callback(hObject, eventdata, handles)
[filename, pathname] = uigetfile('*.*','MultiSelect','on','Open images');
filename = cellstr(filename); % Remains a cell string if it is one already
M = numel(filename);
for k = 1:M
img = imread(fullfile(pathname, filename{i}));
p = size(image, 3);
if p == 3
img = rgb2gray(img);
end
index = k - 1;
if index == 0
index = M;
end
data(:, :, index) = double(img);
end
end
Using "image" as name of a variable shaddows an important Matlab function.

DGM
DGM on 6 Apr 2021
Edited: DGM on 6 Apr 2021
This problem will occur when you only select one file. In that case, the variable filename is a single char array instead of a cell array of char arrays. You could conditionally test for this event and fix it prior to using the variable
if ischar(filename)
filename={filename};
end
or something like that.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!