Effacer les filtres
Effacer les filtres

How to resize multiple images nifti?

29 vues (au cours des 30 derniers jours)
mohd akmal masud
mohd akmal masud le 24 Juil 2022
Commenté : Walter Roberson le 27 Juil 2022
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?

Réponse acceptée

Shivang Srivastava
Shivang Srivastava le 27 Juil 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 commentaires
Walter Roberson
Walter Roberson le 27 Juil 2022
folderNames = {foldersList(3:end).name}; % Start at 3 to skip . and ..
Doing that is incorrect on every operating system that MATLAB has ever been supported on.
  • some of the operating systems had no . or .. entries at all
  • some of the operating systems have . or .. but do not sort the entries returned to MATLAB's internal directory calls. MATLAB never sorts them itself; MATLAB relies upon the order returned by the operating system
  • some of the operating systems only sort for certain kinds of file systems, and it is seldom obvious what kind of file system is being used unless you know the magic to ask
  • some of the operating systems do sort, but permit file and directory names that sort before . and ..
  • the sorting order for some file systems on some operating systems can silently depend upon the language settings of the person who created the file system
The most common file system MATLAB users are likely to use, is MS Window's NTFS. NTFS never promises to sort entries, but typically sorts -- using an order that permits some entries before . and ..
You should never rely upon . or .. being at a particular location in the directory data. You should always deal with those directly:
mask = ismember({foldersList.name}, {'.', '..'});
folderNames = {foldersList(~mask).name};
Walter Roberson
Walter Roberson le 27 Juil 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Convert Image Type dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by