使用matlab将多个图像存储在mat文件中 [英] store multi images in mat file using matlab
问题描述
下面的matlab代码是从文件夹中读取6张图像并将其保存在mat文件中 然后返回以读取mat文件并检查其中的图像
The bellow matlab code is to read 6 images from folder and save them in mat file then return to read the mat file and check the images inside it
问题是 只是最后一张图像存储在mat文件中
the problem is that just the last images stores in mat file
问题出在保存功能上::
the problem is in save function::
要使保存功能将存储在结果单元格中的所有图像存储到mat文件中,我应该进行哪些编辑
what should I edit to make save function store all images that stored in result cell into mat file
%Generate mat file
srcFile = dir('C:\Users\Desktop\images\*.jpg');
result = cell(1,length(srcFile));
for i = 1 : length(srcFile)
filename = strcat('C:\Users\Desktop\images\',srcFile(i).name);
I = imread(filename);
%figure, imshow(I);
I = imresize(I,[128 128]);
result{i} = I;
figure, imshow(result{i});
end
save images, result;
%Read mat file
for j =1 :length(srcFile)
filename = strcat('C:\Users\Desktop\images\',srcFile(j).name);
I = imread(filename);
a='I';
input = load('images.mat',a);
figure, imshow(input.(a));
end
推荐答案
您的第一个循环,直到save
,都很好.
Your first loop, up to the save
, is fine.
在加载数据时,在第二个循环之前使用load('images.mat')
.然后,在工作区中返回result
变量并对其进行迭代:
When you load the data, use load('images.mat')
before the second loop. Then, you have back the result
variable in your workspace and you iterate on it:
load('images.mat')
for j = 1:length(srcFile)
figure, imshow(result{j});
end
您要记住的是,.mat文件仅包含您保存的变量,但是您不能直接使用load
访问它们.您首先要加载它们,然后访问已加载的变量(通常与文件具有不同的名称).
What you have to remember is that your .mat file only contains the variables you saved, but you can't access them directly with load
. You first load them, and then access the loaded variables (which have a different name from the file, usually).
最后,如果要检查此代码,则需要在保存后clear
工作区,否则,您可能不会注意到所使用的某些变量不再存在(例如I
).
Finally, if you want to check this code, you need to clear
the workspace after the save, otherwise you may not notice that some of the variables you use are not there anymore (like the error you have got with I
).
这篇关于使用matlab将多个图像存储在mat文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!