Loading images from a .MAT file

39 ビュー (過去 30 日間)
Tony
Tony 2014 年 1 月 16 日
コメント済み: Jyoti Arora 2015 年 12 月 16 日
i have this function which i used to save a folder of images into a .MAT file as a database. Now i want to load the images from the data to use them but its a struct so i tried converting using struct2cell and that didn't get me anywhere. I seen about 3 different ways to get this to work but non seem to work which made me thing maybe i saved them wrong?
So i want to make a loop that loads all images from this database. all images have the same dimensions and file type .jpg
folder = 'F:\matlab\face\data';
ListOfImageNames = {};
ImageFiles = dir([folder '/*.*']); % Get all files.
for Index = 1:length(ImageFiles)
baseFileName = ImageFiles(Index).name;
[folder, name, extension] = fileparts(baseFileName);
extension = upper(extension);
% Let's save just those we are interested in:
switch lower(extension)
case {'.png', '.bmp', '.jpg', '.tif', '.avi'}
% Allow only PNG, TIF, JPG, or BMP images
ListOfImageNames = [ListOfImageNames baseFileName];
otherwise
end
end
save('F:\matlab\face\testimagebase.mat','ListOfImageNames');

採用された回答

Jacob Halbrooks
Jacob Halbrooks 2014 年 1 月 16 日
The code shown only saves the names of the image files and not the image data itself. This is because the variable 'ListOfImageNames' is a cell array of names, and SAVE just stores it in the MAT-file as such.
If you want to save the actual image data, you should call IMREAD on each image to get its data into MATLAB, and then use SAVE for the actual data. One way to do this is to create a struct and add fields to this struct as you find and read images. Here's how that might look, based off of your code:
function saveImages(folder)
ListOfImageNames = {};
ImagesToSave = struct();
ImageFiles = dir([folder '/*.*']); % Get all files.
for Index = 1:length(ImageFiles)
baseFileName = ImageFiles(Index).name;
[folder, name, extension] = fileparts(baseFileName);
extension = upper(extension);
% Let's save just those we are interested in:
switch lower(extension)
case {'.png', '.bmp', '.jpg', '.tif', '.avi'}
% Allow only PNG, TIF, JPG, or BMP images
ListOfImageNames = [ListOfImageNames baseFileName];
ImagesToSave.(name) = imread(baseFileName);
otherwise
end
end
save('testimagebase.mat','-struct','ImagesToSave');
Now if you call LOAD on your MAT-file, you will be returned the same structure that you originally saved, with a field for each image containing its data:
>> saveImages(pwd)
>> myimages=load('testimagebase.mat')
myimages =
image1: [2178x2448x3 uint8]
myimage2: [676x1088x3 uint8]
  6 件のコメント
poongothai rajan
poongothai rajan 2014 年 4 月 23 日
dear image analyst ..when i run your code i am getting this error..can u plese clarify it
"Reference to non-existent field 'ListOfImageNames'."
Jyoti Arora
Jyoti Arora 2015 年 12 月 16 日
Dear Image Analyst After Loading the .mat file if I am getting this output, groundTruth: {[1x1 struct] [1x1 struct] [1x1 struct] [1x1 struct] [1x1 struct] [1x1 struct] [1x1 struct]} how can I use it further in my code. Each struct represents segment of an image and I want to evaluate for my segmentation results.

サインインしてコメントする。

その他の回答 (2 件)

poongothai rajan
poongothai rajan 2014 年 4 月 23 日
can u please anyone help how to convert the .mat file into image file..thanks in advance

Shaveta Arora
Shaveta Arora 2015 年 6 月 27 日
i have .mat one file which is 1x1 struct. how to read the image from this.
  1 件のコメント
Image Analyst
Image Analyst 2015 年 6 月 27 日
Call load to read the mat file into a structure:
storedStruct = load(yourFileName);
Now, if you had a structure that you stored when you called save() to create the mat file, then that structure will be a field of the storedStuct variable. You can extract it into a variable if you want:
myStruct = storedStruct.myStruct;

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by