How can I extract 2d slices images for all the CT volumes in a folder?

5 次查看(过去 30 天)
I have tried to use for loop to read all the volumes and another for loop to extract the slices but I only obtained one image for each volume. I need to obtain all the slices in axial plane for each skull volume in a folder to train my neural network. Any idea on how can I fixed my code as below?
clc; clear all;
Folder = "C:\Users\User\Documents\SEM 7\FYP\testestestse\volume";
destinationFolder = "C:\Users\User\Documents\SEM 7\FYP\testestestse\image";
filePattern = fullfile(Folder, '*.gz');
gzFiles = dir(filePattern);
for k = 1:length(gzFiles) % read each volumes
baseFileName = gzFiles(k).name;
fullFileName = fullfile(Folder, baseFileName);
Va = double(niftiread(fullFileName));
Vmax = max(Va,[],'all'); % Maximum value in the whole scan
Vmin = min(Va,[],'all'); % Minimum value in the whole scan
Va_prime = (Va-Vmin)./(Vmax-Vmin); % Normalization -> values between 0 and 1
slice = size(Va_prime,3); % get max value for axial
for n = 1:slice % extract all slices
axial = Va_prime(:,:,n); % axial plane
axial_rotate = imrotate(axial,-90);
axial_resize = imresize(axial_rotate,[128 128]);
baseFileName = sprintf('slice_0%d.png', slice); % rename
fullFileName = fullfile(destinationFolder, baseFileName); % set destination
imwrite(axial_resize,fullFileName);
end
end

采纳的回答

Rik
Rik 2022-3-29
Your file name is based on slice (which contains the number of slices, not the slice number), instead of n and k. If all scans have equal numbers of slices, that means you only get 1 file.
  3 个评论
Rik
Rik 2022-3-29
Your current code is overwriting everything. What format do you actually need? Do you need all scans in a single folder? Do you need all images together in a single folder? If it is the latter:
baseFileName = sprintf('scan_%02d__slice_%03d.png', k, n);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Computer Vision Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by