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일

0 개 추천

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

Tony
Tony 2014년 1월 20일
편집: Tony 2014년 1월 20일
when i load it shows
a1 =
ListOfImageNames: {1x25 cell}
it does not save in an array as your read out
tried imshow(a2.ListOfImageNames(1))
does not read it as
??? Error using ==> iptcheckinput Function IMAGEDISPLAYVALIDATEPARAMS expected its first input, I, to be one of these types:
double, single, uint8, uint16, uint32, int8, int16, int32, logical
Instead its type was cell.
ListOfImageNames is a cell array. Somehow it got attached as a field of a structure called a1, maybe a1 was the return argument of a load()???? If so, you might want to do
a1 = load(matFileName);
ListOfImageNames = a1.ListOfImageNames;
% Now read in an image
folder = pwd; % or whatever it really is.
% Prepend folder to base file name.
fullFileName = fullfile(folder, ListOfImageNames{1});
myImage = imread(fullFileName); % Read it in.
imshow(myImage); % Display it.
Tony
Tony 2014년 1월 20일
thank you but isn't this kind of going backwards i want the actual image in the .mat file which i believe Jacob's has done. I would like to be able to load it as an array to have a for loop cycle through them
Image Analyst
Image Analyst 2014년 1월 20일
Jacob saved the "ImagesToSave" variable, which are the actual images in a cell array. Apparently you also saved the variable "ListOfImageNames" (which is a list of strings, not image arrays), and you were having trouble recalling that variable (if you check your comment just before mine), so that's what I was trying to help you with.
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일

0 개 추천

can u please anyone help how to convert the .mat file into image file..thanks in advance
Shaveta Arora
Shaveta Arora 2015년 6월 27일

0 개 추천

i have .mat one file which is 1x1 struct. how to read the image from this.

댓글 수: 1

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;

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Convert Image Type에 대해 자세히 알아보기

질문:

2014년 1월 16일

댓글:

2015년 12월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by