Creating .mat from images

4 views (last 30 days)
S C.Carl
S C.Carl on 11 Jun 2022
Edited: S C.Carl on 13 Jun 2022
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
Image Analyst
Image Analyst on 12 Jun 2022
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)

Community Treasure Hunt

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

Start Hunting!