Converting JPG images into a cell arrays

조회 수: 6 (최근 30일)
Abdussalam Elhanashi
Abdussalam Elhanashi 2020년 10월 24일
댓글: Walter Roberson 2020년 10월 25일
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
dataDir= fullfile('Data/');
exts = {'.jpg','.png','.tif','BMP'};
imds = imageDatastore(fullfile(dataDir),...
'IncludeSubfolders',true,'FileExtensions','.jpg','LabelSource','foldernames');
countEachLabel(imds);
[TrainData, TestData] = splitEachLabel(imds, 0.5);
size(TrainData);
countEachLabel(TrainData);
for i = 1:numImages
img = readimage(TrainData, i);
img = imbinarize(img);
end

채택된 답변

Walter Roberson
Walter Roberson 2020년 10월 24일
편집: Walter Roberson 2020년 10월 25일
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));
See also mat2tiles in the File Exchange
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
Abdussalam Elhanashi
Abdussalam Elhanashi 2020년 10월 24일
Hi Walter Roberson
I tried the code but i got error
dataDir= fullfile('Data/');
exts = {'.jpg','.png','.tif','BMP'};
imds = imageDatastore(fullfile(dataDir),...
'IncludeSubfolders',true,'FileExtensions','.jpg','LabelSource','foldernames');
countEachLabel(imds);
[TrainData, TestData] = splitEachLabel(imds, 0.5);
size(TrainData);
countEachLabel(TrainData);
numImages = numel(TrainData.Files);
for i = 1:numImages
img = readimage(TrainData, i);
img = imbinarize(img);
sz = size(img);
RB = 28; CB = 28;
NRB = floor(sz(1)/RB);
LOR = RB - NRB*RB;
NCB = floor(sz(2)/CB);
LOC = CB - 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));
end
Error
Error using mat2cell (line 89)
Input arguments, D1 through D3, must sum to each dimension of the input matrix size, [200 200 3].
Error in Untitled (line 34)
tiles = mat2cell(img, rbs, cbs, size(img,3));
Walter Roberson
Walter Roberson 2020년 10월 25일
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.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by