MATLAB: How can i resize image stored in imageDatastore

datasroreimage resizingimagedatastore

im tring to resize the images so that all image have the sane size i tried every thing nothing works and function of augmenterImageDatastore is not availible so if you have a solution that works please help me

Best Answer

Hi Rawan,
The following workflow can help you to resize all the images to the same size (and see example code at the bottom):
1. Write a custom read function for your image datastore.
This doc page describes the 'ReadFcn' for imageDatastore objects: https://www.mathworks.com/help/matlab/ref/matlab.io.datastore.imagedatastore.html#butueui-1-ReadFcn.
You can start with the default 'ReadFcn', and make a copy with a different name. Then, set the 'ReadFcn' property of your datastore to this custom function.
2. At the end of the custom ReadFcn, add a line that resizes the images to your desired image size. Example:
data = imresize(data, [224 224]);
Run your script again, and see if the issue is resolved.
3. If you still get the error message, add another line to the custom ReadFcn that sets all the images to have the same number of channels as the image with the fewest channels. This line should go right before the 'imresize' line. Example:
data = data(:,:,min(1:3, end));
The code below is an example of how to use a custom read function as described above.
imageDS = imageDatastore(imgPath);
imageDS.ReadFcn = @customReadDatstoreImage;
function data = customReadDatastoreImage(filename)
% code from default function:
onState = warning('off', 'backtrace');
c = onCleanup(@() warning(onState));
data = imread(filename); % added lines:
data = data(:,:,min(1:3, end));
data = imresize(data,[224 224]);
end