How can i resize image stored in imageDatastore

63 次查看(过去 30 天)
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

采纳的回答

Vijaya Lakshmi Chagi
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

更多回答(1 个)

Dr. Murtaza Ali Khan
% % 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',...
'foldernames');
% % -----------------------------------------------------
% % 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
figure,
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.
reset(imds);
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
figure,
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]);
end
% % -----------------------------------------------------

Community Treasure Hunt

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

Start Hunting!

Translated by