File Exchange

image thumbnail

Dicom directory (of slices) to 3D volume image

version (3.69 KB) by Ariel Balter
Read a series of dicom slice images to a 3D volume image and meta data.


Updated 07 Apr 2009

View Version History

No License

This function reads all good dicom files in a given directory and outputs a volume image. The slices are sorted by the SliceLocation tag in the image header. If no directory is specified, it opens a folder browser.

It also collects user specified (or default) header information for each slice and calculates some additional data for each slice such as the physical width and height of the image (e.g. PhysicalWidth = PixelWidth(1)*Rows). It also determines some meta data about the image such as the physical aspect ratio which is useful for volume rendering utilities.

Finally, it saves all the data to the dicom directory as a MAT file. In the future, the user will be able to switch the file saving on and off, and also specify alternate formats.

Cite As

Ariel Balter (2021). Dicom directory (of slices) to 3D volume image (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (23)

Dan Smith

Can anyone tell me how to convert the values of a CT back to show HU in matlab?

Stelios Fanourakis

Function is not defined for 'uint8' inputs.

Mehmet Subasi

This program doesnt give a 3D image as a output? How am going to visualize 3D ?

alaa shamasneh

its not working with me can i send the dicom volume to you and try to run your code on it please

Sangita Sahu


I used the code and it worked well for me. It took the the dicom slices from the directory and created a volume_image.mat file in same directory. But can you please tell me how to visualize the output 3D volume made by stacking 2d slices. Is there any command for it??



Dylan O'Connell

Füsun Er


Thank you for nice tool. It worked for me.
Could you help me about to save the produced variables into one 3D .dcm file?


Leighton Warmington

So i was looking through this and it looks like it give this message when you are using some kind of dicom dir with multiple image sets. The MR localizer image for example might not have the same resolution as your main image and it complains when you try an assign the image from one set to the matrix that was defined based on a different image set. You might want to separate the acquisitions to avoid this.

bahar cham

Ariel Balter

Folks, I'm sorry, but I can't support this script at this time. I'm inclined to pull it, but since some folks are getting some use out of it, I'll leave it up for the time being.

The code is pretty well commented, so I think it should be possible for someone to get it working, and I'd be happy to update with bug fixes as they come in.

I wrote this code a long time ago when I was doing a specific project with dicom files. That was the last time I looked at such files, and I no longer have any to test with.

Good luck!

Mitchell Blenden

Mitchell Blenden

Will this code allow me to take Philips 3D data from their 3D echo and play it in matlab? Also will this allow me to make a 3D image from their data. The rep says the file is 3D dicom native data and I can not find out how to load it in as a 3D image, it only comes into matlab as a 2D image.


I was able to get a mat file running the dicom23D function. How do I use this output? Can I visualize the data?


Ariel Balter

Sorry, I made that for a project I have long since abandoned. I don't use dicom or do any imaging stuff any more at all.

Nevertheless, I will try to take a look and see if I can fix it. At the very least I will extensively comment in the hopes that someone else can debug.

Again, sorry for it not working out of the box.

Crtistian Capannini


Hakan Gustafsson

I also have the same problem :-( DICOM from a Philips achieva.

Best regards,

Tangarua Tangarua

Hi Unfortunately, I am getting a similar error:
ans =

header did not contain the field SpacingBetweenSlices

??? Index exceeds matrix dimensions.

Error in ==> dicom23D at 270
[rows, cols] = size(slice_data(1).SliceData);

Could you please help me to figure that out?
If you need my DICOM files, just send me an email and I'll send them to you.

Best Regards

Ariel Balter

I think it is possible that you tried it on an empty directory -- at least that is how I could reproduce the error you got.

In any case, I just uploaded an updated version with improvements and more catches. Please let me know how it works for you. --AB


Hey, I'm really excited about the prospect of having a DICOM file viewer in MATLAB. Unfortunately, I get the following error when running dicom23D:

?? Index exceeds matrix dimensions.

Error in ==> dicom23D at 218
if isfield(slice_data(1), 'SliceLocation')

MATLAB Release Compatibility
Created with R2008b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!