Search code examples
matlabimage-processing3ddicommedical

How can I convert Dicom directory (series of dicom images) to 3D volume image?


I have almost 80 dicom images in a folder. Now, I want to convert these series of dicom images to a 3d volume.

Below is the code which loads MRI data, forms counter slices and then generates 3d volume. But, it loads inbuilt MRI images.

How can I change this program to load dicom images from my folder and display 3d volume??

load mri
D = squeeze(D);
%figure
colormap(map)
image_num = 8;
image(D(:,:,image_num))
axis image
x = xlim;
y = ylim;
cm = brighten(jet(length(map)),-.5);
figure
colormap(cm)
contourslice(D,[],[],image_num)
axis ij
xlim(x)
ylim(y)
daspect([1,1,1])
figure
colormap(cm)
contourslice(D,[],[],[1,12,19,27],8);
view(3);
axis tight
figure
colormap(map)
Ds = smooth3(D);
hiso = patch(isosurface(Ds,5),...
   'FaceColor',[1,.75,.65],...
   'EdgeColor','none');
   isonormals(Ds,hiso)
hcap = patch(isocaps(D,5),...
   'FaceColor','interp',...
   'EdgeColor','none');
view(35,30) 
axis tight 
daspect([1,1,.4])
lightangle(45,30);
lighting gouraud
hcap.AmbientStrength = 0.6;
hiso.SpecularColorReflectance = 0;
hiso.SpecularExponent = 50;

Solution

  • we can use fullfile which returns a string containing the full path to the file. and dir which returns a list of files and folders that match the string name. When name is a folder, dir lists the contents of the folder. Specify name using absolute or relative path names. name can include wildcards (*). and rest of the things are well commented in program.

        clear all;
        close all;
        clc;
        fileFolder = fullfile('series 9');
        files = dir ( fullfile (fileFolder, '*.dcm'));
        fileNames = {files.name};
    
        %examine file header (metadata , from dicom stack)
    
        info = dicominfo(fullfile(fileFolder,fileNames{1}));
    
        %extract size info from metadata
        voxel_size = [info.PixelSpacing;info.SliceThickness];
    
        %read one file to get size
        I = dicomread(fullfile(fileFolder,fileNames{1}));
        classI = class(I);
        sizeI = size(I);
        numImages = length(fileNames);
    
        %read slice images populate 3d matrix
        hWaitBar = waitbar(0,'reading dicom files');
    
        %create array
        mri= zeros(sizeI(1),sizeI(2), numImages , classI );
        for i=length(fileNames):-1:1
            fname = fullfile(fileFolder, fileNames{i});
            mri(:,:,i) = uint16(dicomread(fname));
            waitbar((length(fileNames)-i+1)/length(fileNames))
        end
    
        delete(hWaitBar);