MATLAB: How can i crop images from multiple folders that are located under one main folder to a specific size before i feed it through a CNN

cnn code

categories = {'Cardiomegaly','Infiltration', 'No Finding'};
rootFolder = 'C:\Users\roro\Downloads\Data';
imds = imageDatastore(fullfile(rootFolder, categories), …
'LabelSource', 'foldernames');

Best Answer

Hi,
You can use augmentedImageDatastore to augment the images read by imageDatastore. The cropping function can be achieved using the OutputSizeMode as centercrop’. You may ‘randcrop’ also if you want cropping to be random instead of focusing at center. Below code might help.
rootFolder = 'C:\Users\roro\Downloads\Data';
imds = imageDatastore(fullfile(rootFolder, categories), ...
'LabelSource', 'foldernames');
% imageSize must be defined according to the final cropped image size
imageSize = [28 28 1];
augimds = augmentedImageDatastore(imageSize,imds,'OutputSizeMode','centercrop');
augimds can then feed to CNN instead of imds.