File Exchange

image thumbnail


version (9.49 MB) by Dirk-Jan Kroon
MIP, Color, Slice and Shaded 3D (shearwarp) Volume Rendering, interactive 3D view/measurement GUI


Updated 15 Sep 2016

View Version History

View License

Editor's Note: Popular File 2016

This function can be used to interactively view any kind of 2D,3D and 4D image volumes like CT and MRI. Allowing segmentation and distance measurements or ROI selection in 2D slices.
It includes Maximum intensity projections (MIP), Volume Rendering (VR) , Slice render, and colored and Phong shaded Volume rendering.
Voxel opacity and color tables can be changed on the fly allowing also iso surface kind of renders.
The fast render algorithm which renders a 3D volume to a 2D image, is based on a Matlab coded implementation of the Shear-Warp transform, in which instead of rotating the viewer rays the slices are sheared.
There are two major Matlab files:
- viewer3d.m : The interactive GUI
- render.m : The function which renders volume data with the shear-warp transform to an 2D image.

File-formats supported:
Matlab Files (.mat)
Dicom Files ( .dcm , .dicom )
V3D Philips Scanner ( .v3d )
GIPL Guys Image Processing Lab ( .gipl )
HDR/IMG Analyze ( .hdr )
ISI Files ( .isi )
NifTi ( .nii )
RAW files ( .raw , .* )
VMP BrainVoyager ( .vmp )
XIF HDllab/ATL ultrasound ( .xif )
VTK Visualization Toolkit ( .vtk )
Insight Meta-Image ( .mha, .mhd )
Micro CT ( .vff )

This version is 100% Matlab code, but to make it faster if you run "compile c-code" in the config menu. I also included an older version with the shearwarp algorithm in c-code / mex-code

Please report bugs, successes and if you know some possible code speedups.

Cite As

Dirk-Jan Kroon (2021). Viewer3D (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (132)

Akarsh Singh

Mary Walsh

I'm having trouble uploading and viewing a .vff file. Any suggestions?

Hanh Nguyen


heba sal

Nancy Wu

Hi All,

I keep getting this error when I try to visualize my .nii file in 3D. I've tried using 2016b and 2019b. I've tried precompiling the C code. I've tried to step through the code with the debugger and it doesn't even call the first line in affine_transform_2d_double or step into it. I've tried to trigger the breakpoints before it and it's perfectly fine. I've checked the variables and they seem to be properly defined and reasonable sizes going in. I genuinely don't know what to do...

Error using affine_transform_2d_double
Requested 1717986918800x1 (12800.0GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may
take a long time and cause MATLAB to become unresponsive. See array size limit or preference panel for more information.


Hi all,

I am trying to upload a .vtk file which seems to take too long to load and causes matlab to freeze..
any ideas on what might be the issue?


seems takes forever to upload an mha file

Anand P

Why do i get an all black image when i am using viewer3d(V) for both 3 dimensional and 4 dimensional values of V ? Any solutions ?

Kylie Smith

Hi, thank you for providing this code. I have tried to open it where the V is a 4-D file as well as a DICOM (.dcm) file. For both situations, I get an error that reads as shown below when i run the code vol = render('Rhesus_Head_MPI.dcm'). I am very new to matlab so please respond with very basic terminology. Are modifications to the default options required to run?

Undefined function or variable 'affine_transform_2d_double'.

Error in render>warp (line 872)

Error in render (line 202)
data = warp(data);


Thank you so much!
Very useful for our visualization of 3d light sheet imaging data!


naveen B

Im trying to open BRATS .mha file but showing following error

Error using fopen
First input must be a file name or a file identifier.

Error in mha_read_volume (line 28)

Error in ReadData3D>loaddata (line 379)

Error in ReadData3D>pushbutton_load_Callback (line 365)

Error in gui_mainfcn (line 95)

Error in ReadData3D (line 70)
gui_mainfcn(gui_State, varargin{:});

Error in>@(hObject,eventdata)ReadData3D('pushbutton_load_Callback',hObject,eventdata,guidata(hObject))
Error using uiwait (line 81)
Error while evaluating UIControl Callback.

Please help any one help me

Girvani Manoharan

The problem solved. The original mat file was in the format unit8. When I converted to double then it is working.
Many thanks.

Colby Dillion


Thank you very much for such a useful thing. I am benefitting a lot. Could you please let me know how to cite your work as I am using images rendered by your program, in my research paper? Thanks


anyone can tell me tat how to analyze and also how to show mha omages please?

Stelios Fanourakis

I use viewer3d(A) where A is my dicomread set of images and I get the error "Sample values must be a single or double array.
". Please help.

Mete Naz

Is there any way to read seg.nrrd file with any of your code?

Thanks in advance!

Marcia Costa

Hi, after I use the function dicom_read_volume, I only get the first slice... anyone else with this issue? I can't get a 3D volume, just that first slice.

gamini rohith

Kassandra Kist

I am trying to load a .vff and when I click load nothing happens? Any help?

lichao fang


rd junze

Luoluo Liu

Nice software, may i ask how to set viewpoint automatically? Thanks so much

George Ralli

Works great!

For those people just getting a black window, the way I solved it (I'm using 2016a) is first to click volume in viewer3d and select volume 1. Then click render and select the option that you want (e.g. colour, view x slice etc...). At least that seemed to solve the problem for me...

Nathan Hughes

Works great, is there a way to turn off the green text overlayed on the image?


this isnt working with 2016b, its a black window .

the volume im working with is 65 slices of CT, they show up great in imshow3D but as seprace slices, im trying to view them as a volumetric quantity, can anyone help/advise? :)

Shengnan Liu



I am also getting the blank screen with R2015b.
And I click on Volume in view3d, choose Volume 1. The window is still blank.
Looking forward to your reply!
best regards

encai zhang


Dirk-Jan Kroon

*Zhensen Chen, thank you for pointing out the bug with R2014 and above.
Now solved in new version by making the UIpanel invisible..

Zhensen Chen

I also used Matlab 2015b. When running viewer3d, the window doesn't show a menubar (i.e. the one with menus: Render, Volume, Config, Measure, Segment) and a black image in its display zone. The solutions for these are: 1. Right click the mouse, you will see the menubar; 2. Explicitly specify uipanel as parent of the axes in viewer3d.m line 3093: data.subwindow(i).handles.axes=axes('parent',data.subwindow(i).handles.uipanelmenu); Then you will see the region (axes) where images can be displayed.

Raja Sekhar Bandaru

Hi Dirk,
I am using 2015b. Loaded from a raw data file (.raw). 8bit uchar, size 400x300x300. Selected Volume1 in the viewer. I have set the windowwidth windowlevel to automatic (127,255). But I don't see anything (blank window).
PS-I confirmed from another viewer that I have proper data.

Sébastien Tosi

Would it be possible to make this script compatible with Matlab >=2015? There is an issue with the GUI and it does not work. Anybody as a fix?

Ilya Belevich

Xiao Wang

great tool. For those who cannot see a 3D image, click on Volume in view3d, choose Volume 1.

Yi Ji

Just gives blank page, even sample data.

OSX 10, MATLAB R2014b

Golchehr N

I run the program with R2015a and just gives blank page. Even sample data doesn't work. Does anybody has idea how to make the program work? thanks

Sharan Duggirala

Works well but GUI glitches for MATLAB 2015

Mustafa Umit Arabul

There is a bug in the GUI. It is not working properly with MATLAB version 2015.

Can you please tell me how can i open .mat file in viewer3d?

Seok Kim

Thank you for your work.
What is the mat file matrix size?
L x M x N? or L x M x N x O?


first of all thanks sharing code

i want to ask a question

i have dwi brain image .gipl format.i want to tick on .gipl image with roi and then want to make logic one in roi area and logic zero outside of roi area

if someone knows how it is done, kindly help me

waiting feedback



It works well.Thank you!
I have a problem.Any body can help me please?!
I have a series of images of a blood clot in the coronary artery angiography .(These photos are from different angles and different thicknesses of clot) .I want process these two-dimensional images to estimate the VOLUME of the clot anymore. please help me!
thank you so much....


I would like to know if it is possible to extract the volume data rendered without the matlab viewer (plot). Can anyone help me? Thanks.



I am also getting the black screen with 2014a.


Thank you for this easy-to-use tool. I keep getting the same black scree with MATLAB 2013a. Please let us know if someone figured out the problem.



This looks like a very good tool; however i keep getting a black screen!!

I get the GUI but have a black screen within it!!

I'm using Matlab version R2013a.

Can someone give me an idea of what might be wrong?

Thanks in advance


Hi Dirk,
very nice software.
Is it possible to change the background color ? Say, from black to white ?
How should it be referenced if used to generate images for a paper ?


This works very well for me. Thank you!

(the rating button does not seem to be working. I would give it a 5-star if I could)

Kimberly Riley

This looks like a very neat tool however i keep getting a black screen. i've set my path to include all folders within the main folder viewer3d_version11. i've loaded the .mat file CommandlineData.mat. then just typed viewer3d(V) in the command line. i get the GUI but have a black screen within it. I'm using matlab version 2013a. can someone give me an idea of what might be wrong?


This is very nice.

Some suggestions for future.
1.) Manually set view point. Maybe even output a movie.
2.) Manually set alpha/color. Dragging is very cumbersome.


Ramiro Massol

hi Dirk
can your application render multi-coloured volumes (e.g. RGB)?


How do you keep the contrast in the 3D view ?



Great program;

I've added some mirror functionalities, so it automatically creates the mirror ROI.

Unfortunately, I can't compile it on a MAC, using llvm--twolevel_namespace -undefined error


Hi, thanks for this great piece of software. Is there a possibility to change the background color from black to white?

fred sampedro

Beautiful piece of software :). How can I change the title of the figure? (I mean the 'viewer3d' on the top left of the OS window)

Thanks a lot!

André Zeug

Dirk, please let us know how to set dataset parameters like voxel size for the render.m function. I would like to process my data in a script using your render.m function rather than your viewer3d GUI.

Thanks in advance!


How can I change the voxel sizes, window width, etc from my Matlab function rather than using the GUI every time again?


Hi Dirk,

How does the example of function "[Mshear,Mwarp2D,c]=makeShearWarpMatrix(Mview,sizes)" in render.m works ???? Anyone knows ??? Please help me out here.... Thanks


I'm still new with this matlab. i've an interpolated image that need to be projected out with MIP. But how do i go about doing this MIP ?? Or could you show me which part from your render.m is programming the MIP function??

Thanks in advance :D


I'm new to the dicom format. I have a directory of hundreds of CT slices of a wrist. How do I read all them into the tool to obtain a 3D image?


Murat Shagirov

Urs Utzinger

If Dirk-Jan has spare time I was hoping he could consider implementing commands to:
1) reset rotation to be North/South/East/West on top (or have a field where the rotation, translation and zoom can be entered by hand).
2) Option to apply rotation, zoom and translation performed in one widow to the other windows (linking view setting between windows)
3) Function to crop data cube by placing planes perpendicular to x/y/z
4) 3D cursor to probe intensity while also showing location.

Urs Utzinger

Brian MacDonald

I am having trouble getting the voxel size function to work. When I increase the voxel size nothing happens. Any thoughts?


I want to visualize a 3D *.mat data with the "viewer3d",I first tried to visualize the "matlab mri image" by the following code, but nothing displayed!
>> load mri
>> D = squeeze(D);
>> viewer3d(D);

what's the problem? maybe there is an especial format for the *.mat files that I don't know, please help me.thanks.

bahar cham


oh got it ... I figured out to use render() (instead of viewer3d) with the corresponding arguments.


Great tool!
Is it possible to generate multiple TOF-frames (and save them) for a movie from the command line?
As far as I see, only the volume-data is specified as input parameter for viewer3d!?


...problem solved! The issue is not the code, it's the fact that I don't have the Image Processing Toolbox installed. Oops.


I'm trying to run this file on CentOS. I tried to simply run the GUI by typing "viewer3d" (no quotes) into the command window while in the correct directory. I get the following error messages:

??? Undefined function or method 'imshow' for input arguments of type 'uint8'.

Error in ==> menubar>CreatMenuBar at 181

Error in ==> menubar>UpdateMenu at 353

Error in ==> menubar at 83

Error in ==> viewer3d>addWindowsMenus at 3239

Error in ==> viewer3d>addWindows at 3098

Error in ==> viewer3d>viewer3d_OpeningFcn at 155

Error in ==> gui_mainfcn at 221
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});

Error in ==> viewer3d at 47
gui_mainfcn(gui_State, varargin{:});

I also tried working with some of the example data. I typed in the command that the help file suggests:


And I get more errors: ??? Error using ==> load
Unable to read file ExampleData\CommandlineData.mat: No such file or directory.

I'm very confused. I'm definitely working in the right directory. Any insight?


Terrific code. mha_read_volume can be slightly improved for reading 3-D deformation field (i.e., a vector field) by replacing line 80 to the code as follows:

V = reshape(V,info.Dimensions);
V = reshape(V, [nchannel info.Dimensions]);


File -> open medical 3d file.
Image File Name appears to contain a patient name in folder. Might want to remove that:
Other\P****s A****s 05***1603\201***9\1.2.826.0.1

Al Ka

Is there a copyright disclaimer? Can it be used for research purposes?


Norbert Gal

When i try to render a set of slices of DICOM files i have the following error:

??? Operands to the || and && operators must be convertible to logical scalar values.

Error in ==> viewer3d>LevelRenderImage at 531

Error in ==> viewer3d>show3d at 394

Error in ==> viewer3d>menu_ChangeRender_Callback at 3057

Error in ==> gui_mainfcn at 96

Error in ==> viewer3d at 47
gui_mainfcn(gui_State, varargin{:});

??? Error while evaluating uimenu Callback

Can you help mee?


* I would like to convert *


I would convert a .mat file into .mha file. Can I do this with Viewer3d and if so, how? Thanks


Great tool..
25 Jan 2011 version implements some interesting new features , however it seems like there sre some bugs too :
1 - viewer3d.m line 2203:
i've changed it to:
since i've got an error after

volume - 1
renderer - MIP
config->quatily vs speed -> Store volume for all 3 major directions (ON)->Apply

??? Input argument "dvs" is undefined.

Error in ==> viewer3d>makeVolumeXY at 2227

Error in ==> viewer3d>qualityspeed_pushbutton_applyconfig_Callback at 2203

2- if any of 2 checkboxes in config GUI was enabled and saved with 'Save as default', later >>viewer3d(uint8(my_data));
leed to
??? Reference to non-existent field 'volume'.

Error in ==> viewer3d>computeNormals at 2237

Error in ==> viewer3d>addOneVolume at 216

Error in ==> viewer3d>addVolume at 183

Error in ==> viewer3d>viewer3d_OpeningFcn at 170
if(isnumeric(V)), addVolume(V,[1 1 1],info); end

And.... it would be nice to have ability to specify Renderer and volume selection in a function call....Those routine actions can be quite booring...

Dirk-Jan Kroon

If you have your Volume in a Matlab command prompt.
Calculate an inverse of your data for example:
V = max(V(:))-V;
Now the lungs and the background have an high intensity.
To give the background a low intensity, you can use something like:
for i=1:n, s(i)=sum(L(:)==i); end;


Using a different data set, I was able to view the 3d reconstruction of the dicom data so problem solved. Does anyone know how I could focus on just the 3d reconstruction of the lungs from a CT scan? Though I get a 3D reconstruction, all I can visualize is the bone structure (rib cage, spine etc) around the lungs. Does anyone know how I could get around this problem?


I have also used a .mat file
volume_image: [512x512x367 double]
I am still getting an error in this case,

??? Undefined function or method 'max' for input arguments of type 'struct'.

Error in ==> viewer3d>viewer3d_OpeningFcn at 165
volumemax=double(max(V(:))); volumemin=double(min(V(:)));

Error in ==> gui_mainfcn at 221
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});

Error in ==> viewer3d at 47
gui_mainfcn(gui_State, varargin{:});

What does this mean and how can I fix this? Perhaps my method of making a .mat file is wrong. I used the dicom23D function developed by Ariel Balter. Is there any other way of making a .mat file from Dicom files that I could use?


I found out where the online help is, so problem solved there.


Hi Dirk,
I have a CT dicom data. The files though have no extension name to them. How can I use this code to 3D render and view the data? You mention you have put an extended online html help online. How can I access this?
Thanks in advance


Working well with a few sample datasets. It would be great to have this work as an MPR slice viewer and measurement tool, with centering and rotation handles on each orthogonal plane. Seems it is only possible to view and measure from the cardinal axes, correct?

Dirk-Jan Kroon

*Kai Steppenwolf
You can change the Pixel scaling in one of the sub-menus of "Volume 1".

Please note, that a major improved version (11), will be available tonight, or 26 Jan 2011.

Kai Steppenwolf

glad to see the new version, great job! But it seems to have voxel size mistake errors when I read some MRI-Dicom-data. The 3D-view looks so flat, just like a 2D-picture. In the early version (8) I can use the voxel size option in the menu to define the right size, but this time the option seems to disappear in the newest version...

Justin Fernandes

Great Job. I've been looking for something like this for a while. Hope I will be able to program like this in matlab some day. Many thanks.


I are working in matlab2009a. And the files can work at first. But report some errors such as "??? Undefined function or method 'viewer3d' for input arguments of type 'int16'." , soon! And It is ame as to use "double" or "single" type, and so on. Why do this happen? I am too eger to solve this problem. Thank you!

Dave Welch

Yuanming Suo

It works just great right now. Thanks for the hard work.

Dirk-Jan Kroon

*Yuanming Suo , *Xinghua Lou
The newest version 9, has a whole new c-coded file

Yuanming Suo

It works great on Linux. But still Dirk, could you kindly see how can we get around the bug in Windows? This is really a cool tool. If this bug is fixed, it will be even better. I tried to use Visual Studio as the compiler, Matlab still crashes......

Dirk-Jan Kroon

*Xinghua Lou,

Thanks for your comment. I think the crash is caused by the (free) lcc-compiler of Matlab, If you install the Visual Studio (express) version it will probably work fine.

Xinghua Lou

This is no question a wonderful tool for visualization in Matlab.

One bug I found: Matlab crashes after compiling the c mex files. My Matlab version is r2009a, os is Win XP.


Hi Dirk,

Can you add a few quick demos based on the test data and test volume.



Zubin Master

Hey Dirk,

This is a great tool. I've actually been struggling to find some code to visualize CT/MRI data from different angles as MIPs. I could only manage it from the orthogonal angles (couldn't figure out what algorithm to use for rendering from arbitrary angles). This implements it really well.

I was wondering what algorithm would be needed to render the MIP with perspective instead of an orthographic projection. We use these MIP images (Digitally Reconstructed Radiographs or DRRs) when planning radiation therapy treatments and they're usually displayed with perspective to simulate the distance between the radiation source and the patient.

Another poster also mentioned something about simulating DRRs. I was wondering if you could guide me to the algorithm
that I would need to look up to do this.



Another poster mentioned something about rendering your views as DRRs.

Dirk-Jan Kroon

*Kai Steppenwolf

If you select one dicom file from a dataset with a lot of files representing a 3D volume, then the program will automatically load also all other files to build the 3D volume.

Best Regards, Dirk-Jan Kroon

Kai Steppenwolf

Hi Dirk-Jan,

Just found your fantastic Viewer3D.

I have a series of DICOM files, which contain MR slices images of the brain. But the 'open medical 3D files' option can import only one dicom file every time.

Then I tried to read 3D data in to a Mathlab variable V, but the command VIEWER3D(V, RENDERTYPE, SCALES) works still incorrect。

Could you tell me, how can man handle a series of slices dicom image by using your Viewer3d?


Andrew Brennan

Hi Dirk-Jan,
Looking at the code it doesn't appear to support pseudo coloured
bitmaps without first running rgb2grayscale() first. Then the act of applying pseudocolouring again on the 3d volume doesn't appear to work successfully. Is this feature available?

Dirk-Jan Kroon

*Jackson Shen

Thanks for your comment, I will have a look at malloc and mxMalloc ...

Jackson Shen

Great Job, Again!

1. Multi-threading is not working properly in my computer. Matlab is frozen. (Matlab 2008b or above, XP 32bit, Intel Core2 Quad with 2GB memory)
2. Memory leaking...

Using malloc and free causes memory leaking in Matlab. You must use mxMalloc and mxFree instead.

Type "feature memstats" in matlab to monitor the memory.

Looking forwards to see your next version.


Jian-Kun Shen

chandrakala Gowda

Dirk-Jan Kroon

* oscar SanEmeterioNateras
First you have to load your data in to a Matlab variable V, the functions needed to load your medical format can be found on Mathworks.

Hello Dirk,

I want to use your code to open a nii file of and MRA img that I have so I can do the MIP.
I tried to use the command:
VIEWER3D(V, 'MIP', [0.156, 0.29, 0.156]);
to open and run viewer but i am not sure what I should put for the 'V' input.
Can you give me and example or a suggestion on how to work on this thanks a lot.

Dirk-Jan Kroon

* wang ning
If you only sea a blackbox enable auto contrast, because then your values are probably not ranged between 0 and 2^16-1

wang ning

hi Dirk-Jan Kroon

my volume data is 512*512*272 uint16 why files don't work ?

ning thanks



Solid work!

The RENDER_MIP -- updatebuffer function-- can be modified to create a DRR of the volume. What's your take on it.

Hans Müller

Wonderful! I wish I had found this 6 months ago!


excellent work. thanks for sharing!

Aaron Schurger

Outstanding! Sometimes has an error when I switch to a different rendering mode. Thanks for this application.

Siyi Deng


Dirk-Jan Kroon

It runs on most Matlab versions, but not on the very old ones because Matlab figure file format changed in time, and saving to an older figure-file-format doesn't work that well.


Must it be run in the matlab 7.8 version? My matlab version is 7.0, will it be OK? I can't even run the sample: load TestVolume;viewer3d(V). Is it because of the lower matlab version?? Thanks..

jichao zhao

it is very cool staffs. many thanks

Shi vera


Dirk-Jan Kroon

*Cris Luengo
Thanks for your Feedback, I will use it in my nex file update.
(non-isotropically display, can done by viewer3d(Image, [2 1 1]) with [2 1 1] the scaling)

Cris Luengo

Very nice! But it's not very fast. I will try the old MEX-file code next, to see if that speeds it up or not. I tried it with an image 1000x500x250 pixels and found the rendering took 30 s or more.

Some suggestions:

- Don't update the display every time a node is moved in the alpha colors window. Instead, have a button "preview" to update the rendered image.

- Add checkmarks next to the menu items of the activated modes (render and config menus).

- Add an option to enter pixel spacing for non-isotropically sampled images (common in 3D imaging)

- Put the support M-files either within the main M-file or into a private/ subdirectory. That would make it easier to get started...

Great tool! Thanks!

Dirk-Jan Kroon

Tommorow (06-2008) a version with extended HTML help will be online

* Jean-Yves Tinevez
The TestVolume.mat example:
load TestVolume;

Jean-Yves Tinevez

Tested successfully on MacOSX-Tiger, MATLAB 2008b & a.

Worked absolutely fine with matrices entered from command line (e.g. viewer3d(I)).

I had plenty of errors when trying to load a .mat file (e.g. the 2 ones given in demo), and putting tyhe mouse over the window. Here is the 1st one

??? Reference to non-existent field 'histogram_positions'.

Error in ==> viewer3d>createAlphaColorTable at 154
if (i<data.histogram_positions(1)), alpha=0; color=data.histogram_colors(1,:);

Error in ==> viewer3d>menu_load_view_Callback at 542

Error in ==> gui_mainfcn at 96

Error in ==> viewer3d at 50
gui_mainfcn(gui_State, varargin{:});

Error in ==>

Dirk-Jan Kroon

Load View only works with Matlab files created by Save View. Some help can be found in menu -> help, and with help function.m in the console window.

Joaquim Luis

Sorry, I found a little help on the function's help section.

Joaquim Luis

There is absolutely no help on how to use this. When I try to "Load view" than an endless number of errors occur.

MATLAB Release Compatibility
Created with R2015a
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!