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

조회 수: 5 (최근 30일)
Wan Faiz
Wan Faiz 2022년 3월 28일
댓글: Wan Faiz 2022년 3월 29일
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