How to resize multiple images nifti?

조회 수: 15 (최근 30일)
mohd akmal masud
mohd akmal masud 2022년 7월 24일
댓글: Walter Roberson 2022년 7월 27일
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?

채택된 답변

Shivang Srivastava
Shivang Srivastava 2022년 7월 27일
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
Walter Roberson
Walter Roberson 2022년 7월 27일
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 2022년 7월 27일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by