如何读取和存储图像文件夹 (MATLAB)
How to Read and Store a Folder of Images (MATLAB)
我想从一个文件夹中读取一组图像,然后将其存储在一个数组中,这样如果我询问 imshow(imageArray(5))
它会显示数组中的第 5 个图像。使用我从类似问题中找到的一些代码,到目前为止我有这个:
% Specify the folder where the files live.
myFolder = 'C:\Users\MyName\Documents\MATLAB\FolderName';
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.tif'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
imageArray = zeros(480, 1);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray(k) = imread(fullFileName);
end
然而,当我这样做时,出现以下错误:
Unable to perform assignment because the left and right sides have a
different number of elements.
Error in ImportTry (line 16) imageArray(k) = imread(fullFileName);
如何使用单个索引来索引高维数组?
错误是因为您用零初始化数组,因此图像的大小与数组元素的大小不对应。
你想要的是使用cells。
所以你要初始化你的单元格:
imageCell = cell(480, 1);
然后将图像分配给元胞数组的元素:
imageCell{k} = imread(fullFileName);
编辑:
正如 Adriaan 在他的 中指出的那样,如果您的图像大小在整个文件夹中保持一致,那么您最好使用 4D 矩阵。
您的代码的问题在于,在循环的最后一行中,您将尺寸为 1080x1920x3 uint8
(对于全高清图像)的图像分配给大小为 480x1
的向量。这自然不行。因此,请尝试以下操作并使用元胞数组。
% Specify the folder where the files live.
myFolder = 'C:\Users\MyName\Documents\MATLAB\FolderName';
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.tif'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
imageArray = cell(size(theFiles)); % initialize cell array for speedup
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray(k) = {imread(fullFileName)};
end
% iterate cell array and display all images 1-by-1
for k = 1 : length(imageArray)
imshow(cell2mat(imageArray(k)))
pause; % press a button to continue
end
与此处的其他解决方案相反,我不会使用单元格,因为众所周知,单元格比矩阵慢。我只想要一个 4D 矩阵,其中前三个是 N 行、M 列、3 个 RGB 通道的图像,然后是图像的索引:
imageArray = zeros(N,M,3,480); % Where N and M are your image size
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray(:,:,:,k) = imread(fullFileName);
end
请注意,这仅适用于所有图像的形状完全相同的情况。如果没有,细胞或结构是可行的方法。
我想从一个文件夹中读取一组图像,然后将其存储在一个数组中,这样如果我询问 imshow(imageArray(5))
它会显示数组中的第 5 个图像。使用我从类似问题中找到的一些代码,到目前为止我有这个:
% Specify the folder where the files live.
myFolder = 'C:\Users\MyName\Documents\MATLAB\FolderName';
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.tif'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
imageArray = zeros(480, 1);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray(k) = imread(fullFileName);
end
然而,当我这样做时,出现以下错误:
Unable to perform assignment because the left and right sides have a different number of elements.
Error in ImportTry (line 16) imageArray(k) = imread(fullFileName);
如何使用单个索引来索引高维数组?
错误是因为您用零初始化数组,因此图像的大小与数组元素的大小不对应。
你想要的是使用cells。
所以你要初始化你的单元格:
imageCell = cell(480, 1);
然后将图像分配给元胞数组的元素:
imageCell{k} = imread(fullFileName);
编辑:
正如 Adriaan 在他的
您的代码的问题在于,在循环的最后一行中,您将尺寸为 1080x1920x3 uint8
(对于全高清图像)的图像分配给大小为 480x1
的向量。这自然不行。因此,请尝试以下操作并使用元胞数组。
% Specify the folder where the files live.
myFolder = 'C:\Users\MyName\Documents\MATLAB\FolderName';
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.tif'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
imageArray = cell(size(theFiles)); % initialize cell array for speedup
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray(k) = {imread(fullFileName)};
end
% iterate cell array and display all images 1-by-1
for k = 1 : length(imageArray)
imshow(cell2mat(imageArray(k)))
pause; % press a button to continue
end
与此处的其他解决方案相反,我不会使用单元格,因为众所周知,单元格比矩阵慢。我只想要一个 4D 矩阵,其中前三个是 N 行、M 列、3 个 RGB 通道的图像,然后是图像的索引:
imageArray = zeros(N,M,3,480); % Where N and M are your image size
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray(:,:,:,k) = imread(fullFileName);
end
请注意,这仅适用于所有图像的形状完全相同的情况。如果没有,细胞或结构是可行的方法。