如何在.mat文件中保存多个图像在MATLAB环境中

时间:2014-02-21 06:19:20

标签: matlab image-processing

下面是我尝试过的代码,但它会从文件夹中读取图像,但是当我这样做时 尝试将所有图像存储到mat文件,但它将最后一个数组存储在mat文件中

myFolder = 'C:\Users\protocol\Desktop\Finger-Print';
if ~isdir(myFolder)
  errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
  uiwait(warndlg(errorMessage));
  return;
end
filePattern = fullfile(myFolder, '*.jpg');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
  baseFileName = jpegFiles(k).name;
  fullFileName = fullfile(myFolder, baseFileName);
  fprintf(1, 'Now reading %s\n', fullFileName);
  imageArray = imread(fullFileName);

  save db2.mat imageArray; here it save the last image array to db2.mat file

  imshow(imageArray);  % Display image.
  drawnow; % Force display to update immediately.
end

但我想在mat文件中存储多个图像,以便稍后加载此文件 并访问图像

1 个答案:

答案 0 :(得分:4)

您可以将图像存储在单元格数组中,然后将整个数组保存到mat文件中。

clear all; clc; addpath(genpath(pwd));
myFolder = 'C:\Users\protocol\Desktop\Finger-Print\Matlab\Simple_FingerPrint_Matching\FVC2002\DB2_B';
if ~isdir(myFolder)
  errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
  uiwait(warndlg(errorMessage));
  return;
end
filePattern = fullfile(myFolder, '*.tif');
jpegFiles = dir(filePattern);
 result = cell(1,100);
for k = 1:length(jpegFiles)
  baseFileName = jpegFiles(k).name;
  fullFileName = fullfile(myFolder, baseFileName);
  fprintf(1, 'Now reading %s\n', fullFileName);
  imageArray= imread(fullFileName);
  result{k} = imageArray;
end
save db2.mat result;