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

3 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Rik
Rik el 29 de Mzo. de 2022
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 comentarios
Rik
Rik el 29 de Mzo. de 2022
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);

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Computer Vision Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by