【问题标题】:How to Read and Store a Folder of Images (MATLAB)如何读取和存储图像文件夹 (MATLAB)
【发布时间】:2023-03-03 17:57:01
【问题描述】:

我希望从一个文件夹中读取一组图像,然后将其存储在一个数组中,这样如果我询问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

但是,当我这样做时,我收到以下错误:

无法执行分配,因为左侧和右侧有一个 不同数量的元素。

ImportTry 中的错误(第 16 行)imageArray(k) = imread(fullFileName);

如何使用单个索引来索引高维数组?

【问题讨论】:

    标签: matlab image-processing data-storage


    【解决方案1】:

    与这里的其他解决方案相反,我不会去单元格,因为众所周知,单元格比矩阵慢。我会选择一个 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
    

    请注意,这仅适用于所有图像的形状完全相同的情况。如果没有,细胞或结构就是要走的路。

    【讨论】:

      【解决方案2】:

      您的代码的问题在于,在循环的最后一行中,您将具有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
      

      【讨论】:

        【解决方案3】:

        错误来自您使用零初始化数组,因此图像的大小与数组元素的大小不对应。

        你想要的是使用cells

        所以你会初始化你的单元格:

        imageCell = cell(480, 1);
        

        然后将图像分配给元胞数组的元素:

        imageCell{k} = imread(fullFileName);
        

        编辑: 正如 Adriaan 在他的 answer 中指出的那样,如果您的图像大小在您的文件夹中是一致的,那么您最好使用 4D 矩阵。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-25
          相关资源
          最近更新 更多