Creating .mat from images

I want to run the following codes.
load('DatasColor_38.mat','DATA'); % to load the dataset
% the information to split data between training and test set
DIV = DATA{3};
DIM1 = DATA{4};
DIM2 = DATA{5};
lab = DATA{2}; % label
NX = DATA{1}; % cell array that stores the image
fold=1; % in this example only the first fold has been used
trainPattern = (DIV(fold, 1:DIM1)); % id of the training patterns
testPattern = (DIV(fold, DIM1+1:DIM2)); % id of the test patterns
y = lab(DIV(fold, 1:DIM1)); % label of the training set
labelTE = lab(DIV(fold, DIM1+1:DIM2)); % label of the test set
numClasses = max(y);
siz=[224 224]; % input size of ResNet50
% build training set
clear nome trainingImages
for pattern = 1:DIM1 % for all the images
IM = NX{DIV(fold,pattern)}; % image
IM = imresize(IM,[siz(1) siz(2)]);
trainingImages(:,:,:,pattern) = IM;
end
DIM = length(y); % number of images of the training set
% buid testing set
for pattern = ceil(DIM1)+1:ceil(DIM2)
IM = NX{DIV(fold,pattern)};
IM = imresize(IM,[siz(1) siz(2)]);
testImages(:,:,:,pattern-ceil(DIM1)) = uint8(IM);
end
In the above codes, the value of DIM is 880, and trainingImages : 224 x 224 x 3 x 880 uint8 array
The DATA in the above code is => DATA: 1x5 cell array = {1x1320 cell} {[111111.....]} {3x1320 double} {880} {[1320]} )
The output of whos(matfile('DatasColor_38.mat')) is
>> whos(matfile('DatasColor_38.mat'))
Name Size Bytes Class Attributes
DATA 1x5 40590638 cell
But, I could not create the .mat file from our 10000 images which will be classified into 7 classes.
Could you please write the codes that can construct the .mat file (from 10000 images to classify into 7 classes) to use in the above codes
These codes can create a .mat file called "db2.mat". However, it has a different format from the "DatasColor_38.mat".
So, I can not use it in the above codes. Please help :((
myFolder = 'C:\Users\Desktop\IMG_Folder\nv';
filePattern = fullfile(myFolder, '*.jpg');
jpegFiles = dir(filePattern);
DATA = cell(1,10000);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray= imread(fullFileName);
DATA{k} = imageArray;
end
save db2.mat DATA;

11 Comments

Unfortunately you forgot to attach 'DatasColor_38.mat', thus delaying an answer.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Dear Image Analyst, the size of the 'DatasColor_38.mat' is about 24 MB. So, I could not attach it here.
I still need your help :(
"But, I could not create the .mat file from our 10000 images which will be classified into 7 classes." - what does thuis mean?
S C.Carl
S C.Carl on 11 Jun 2022
Edited: S C.Carl on 11 Jun 2022
The codes uses "DatasColor_38.mat" file. I have to create a .mat file from our 10000 images to use in the above codes instead of the "DatasColor_38.mat" file.
But, l could not create it :(( Our images are in a folder. I dont know how to create the mat file from them to be able to use in the above codes
@S C.Carl: You explain that the DatasColor_38.mat file has this format:
DATA: 1x5 cell array = {1x1320 cell} {[111111.....]} {3x1320 double} {880} {[1320]} )
Now you mention 10'000 images and show a code, which store the image data in a cell and writes it to a MAT file. This does not clarify, what the contents of the DatasColor_38.mat is and what is stored in the different fields. The readers cannot guess this detail.
Creating MAT files would be an indirection. Use the code to import the images, produce the data stored in the 1x5 cell and use it directly without saving it to a MAT file at first.
How DIV, DIM1,DIM2, lab and NX are calculated has not been mentioned here yet.
@S C.Carl, since your .mat is huge please provide the output of whos(matfile('DatasColor_38.mat')) which may also help you and us to replicate and answer the question.
S C.Carl
S C.Carl on 12 Jun 2022
Edited: S C.Carl on 12 Jun 2022
Thanks for this comment. I edited the question to add the output of whos(matfile('DatasColor_38.mat')). I hope, you can help me :(((
@S C.Carl: We still do not know the meaning of the 5 elements. If you mention, what DIV, DIM1,DIM2, lab and NX are, it would be easy to provide some code based on the 2nd loop, which imports the image files.
Thanks for your message. According to the comment lines in the codes;
lab=> label of images, DIM1 =>number of training images, DIM2 => number of testing images , NX: cell array that stores the image.
I am really need your help ::((( and looking forward hearing from you. Please help !
The usual way, at at least one of the most common ways, is to put all yhour images of a certain class into a folder named for that class. Then you can use the labels = foldernames option when you setup the imageDatastore which is used to train the network.
S C.Carl
S C.Carl on 13 Jun 2022
Edited: S C.Carl on 13 Jun 2022
Thank you Image Analyst and all

Sign in to comment.

Answers (0)

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Asked:

on 11 Jun 2022

Edited:

on 13 Jun 2022

Community Treasure Hunt

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

Start Hunting!