How can I extract 2d slices images for all the CT volumes in a folder?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Wan Faiz
el 28 de Mzo. de 2022
Comentada: Wan Faiz
el 29 de Mzo. de 2022
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
0 comentarios
Respuesta aceptada
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
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);
Más respuestas (0)
Ver también
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!