# Converting JPG images into a cell arrays

16 views (last 30 days)
Abdussalam Elhanashi on 24 Oct 2020
Commented: Walter Roberson on 25 Oct 2020
Hi
I have the following code i want to convert images jpg (TrainingData) into cell arrays ,where each cell containing a 28-by-28 matrix representing a synthetic image of fingerprint
exts = {'.jpg','.png','.tif','BMP'};
'IncludeSubfolders',true,'FileExtensions','.jpg','LabelSource','foldernames');
countEachLabel(imds);
[TrainData, TestData] = splitEachLabel(imds, 0.5);
size(TrainData);
countEachLabel(TrainData);
for i = 1:numImages
img = imbinarize(img);
end

Walter Roberson on 24 Oct 2020
Edited: Walter Roberson on 25 Oct 2020
sz = size(img);
RB = 28; CB = 28;
NRB = floor(sz(1)/RB);
LOR = sz(1) - NRB*RB;
NCB = floor(sz(2)/CB);
LOC = sz(2) - NCB*CB;
if LOR ~= 0
rbs = [RB * ones(1,NRB), LOR];
else
rbs = RB * ones(1,NRC);
end
if LOC ~= 0
cbs = [CB * ones(1,NCB), LOC];
else
cbs = CB * ones(1,NCB);
end
tiles = mat2cell(img, rbs, cbs, size(img,3));
And watch out for the titles that are not 28 x 28. This code does not assume that the image is an exact integer multiple of 28 in each direction, and it does not throw away any partial blocks.
##### 2 CommentsShowHide 1 older comment
Walter Roberson on 25 Oct 2020
You are right, I had a bug in the code. I fixed it above.
Caution: your 400 x 400 image will end up with partial tiles. On each dimension, you have 14 full groups of 28, and then you have partial group of 8 to reach 400. Because of this, some of the blocks in tiles will be 8 x 28, and some will be 28 x 8, and one of them will be 8 x 8.