How to resize multiple images nifti?

89 views (last 30 days)
Dear all,
I have 75 folder. each folder have 5 set images. each set images have 240x240x155 size.
how to resize all the images (75 x 5 = 375 set images) from 240x240x155 to 100x100x50?

Accepted Answer

Shivang Srivastava
Shivang Srivastava on 27 Jul 2022
Hi,
As per my understanding you want to resize the images present in multiple folders to a new fixed size of 100x100x50.
Since I do not have a sample image of the given dimensions, I will be demonstrating the resizing using a RGB image. You can use the below code to find the images in sub-folders and resize them.
% Find all files present in the parent folder.
currentWorkingDirectory = pwd;
files = dir(currentWorkingDirectory);
% Get a logical vector that tells which is a directory.
dirFlags = [files.isdir];
% Extract only those that are directories.
foldersList = files(dirFlags); % A structure with extra info.
% Get only the folder names into a cell array.
folderNames = {foldersList(3:end).name}; % Start at 3 to skip . and ..
% Make a directory to store the resized images
mkdir resizeImages
for i = 1 : length(folderNames)
folderPath = currentWorkingDirectory + "\" + folderNames{i};
subFolder = dir(folderPath);
dirFlags = [subFolder.isdir];
imageList = subFolder(~dirFlags);
imagesNames = {imageList.name};
for j = 1 : length(imagesNames)
imgPath = folderPath + "\" + imagesNames{j};
% Read Image
I = imread(imgPath);
% Resize Image
Iresize = imresize(I,[100 100]);
% Write Image to the resize folder.
imwrite(Iresize,currentWorkingDirectory+"\"+"resizeImages\"+folderNames{i}+"_"+imagesNames{j});
end
end
For using 3-D volumetric intensity image you can use the following functions:
  1. Read using niftiread
  2. Write using niftiwrite
  3. Resize them using imresize3
  2 Comments
Walter Roberson
Walter Roberson on 27 Jul 2022
folderPath = currentWorkingDirectory + "\" + folderNames{i};
That assumes MS Windows. You should instead be using fullfile:
folderPath = fullfile(currentWorkingDirectory, folderNames{i});
Note: of all of the supported operating systems, MS Windows is the only one that permits \ as a directory separator. The actual internal character used by MS Windows is not \ and is instead / -- so for all currently supported operating systems, / can be used. But using fullfile() is more reliable.

Sign in to comment.

More Answers (0)

Categories

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