How can i resize image stored in imageDatastore

262 views (last 30 days)
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

Accepted Answer

Vijaya Lakshmi Chagi
Vijaya Lakshmi Chagi on 11 Mar 2019
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:
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]);

More Answers (1)

Dr. Murtaza Khan
Dr. Murtaza Khan on 20 Sep 2019
% % Resize images in data store using custom reader function
clc, close all, clear all
% % Read datastore already provided in MATLAB
setDir = fullfile(toolboxdir('vision'),'visiondata','imageSets');
imds = imageDatastore(setDir,'IncludeSubfolders',true,'LabelSource',...
% % -----------------------------------------------------
% % We will use 'read' function to read images from the datastore
% % First call of read returns first image
% % Second call of read returns second image, and so on
I = read(imds); % % read first image from imds
subplot(121); imshow(I); title('First image, before resize'); axis on;
% % -----------------------------------------------------
% % Now from this point, use the custom reader function
imds.ReadFcn = @customreader;
% % Reset the datastore to the state where no data has been read from it.
J = read(imds); % % read the first image again (because we reset read)
subplot(122); imshow(J); title('First image, after resize'); axis on;
K = read(imds); % % read the second image
L = read(imds); % % read the third image
subplot(121); imshow(K); title('Second image, after resize'); axis on;
subplot(122); imshow(L); title('Third image, after resize'); axis on;
% % -----------------------------------------------------
% % Code of custom image datastore reader function
function data = customreader(filename)
onState = warning('off', 'backtrace');
c = onCleanup(@() warning(onState));
data = imread(filename);
data = data(:,:,min(1:3, end));
data = imresize(data, [64 64]);
% % -----------------------------------------------------


Find more on Read, Write, and Modify Image in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by