[英]store multi images in mat file using matlab
下面的matlab代碼是從文件夾中讀取6個圖像並將它們保存在mat文件中然后返回讀取mat文件並檢查其中的圖像
問題是只有最后一個圖像存儲在mat文件中
問題在於保存功能::
我應該編輯什么使保存功能將存儲在結果單元格中的所有圖像存儲到mat文件中
%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
,很好。
加載數據時,請在第二個循環之前使用load('images.mat')
。 然后,您在工作區中返回result
變量並迭代它:
load('images.mat')
for j = 1:length(srcFile)
figure, imshow(result{j});
end
您必須記住的是.mat文件只包含您保存的變量,但您無法使用load
直接訪問它們。 首先加載它們,然后訪問加載的變量(通常具有與文件不同的名稱)。
最后,如果你要檢查這個代碼,你需要clear
后,保存工作區,否則,你可能沒有注意到,有些使用的變量是不存在了(如錯誤,你得與I
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.