View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Marching Cubes

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

4.625
4.6 | 18 ratings Rate this file 55 Downloads (last 30 days) File Size: 6.39 KB File ID: #32506 Version: 1.3
image thumbnail

Marching Cubes

by

 

11 Aug 2011 (Updated )

Use vectorized marching cubes algorithm to compute triangulated mesh of an isosurface from 3D matrix

| Watch this File

File Information
Description

This function uses a vectorized version of the marching cubes algorithm to compute a triangulated mesh of the isosurface within a given 3D matrix of scalar values at a given isosurface value. The output is a triangulated mesh specified in terms of a face list and a vertex list. The orientation of the triangles is chosen such that the normals point from the higher values to the lower values. Optional arguments COLORS ans COLS can be used to produce interpolated mesh face colors.

This function was used to create a surface mesh from the CT scan dataset for the Stanford bunny, a 461 x 339 x 330 array of floats. See uploaded image. Elapsed time on an AMD Opteron 64-bit computer with 4 GB of RAM is 24.7 seconds. For comparison, a surface mesh was computed from the same dataset using Matlab's isosurface function, and the elapsed time was 98.6 seconds.

This function was adapted for Matlab by Peter Hammer in 2011 by making minor syntax changes to an Octave function written by Martin Helm <martin@mhelm.de> in 2009 (http://www.mhelm.de/octave/m/marching_cube.m)

Revised 30 September, 2011 to add code by Oliver Woodford for removing
duplicate vertices.

MATLAB release MATLAB 7.12 (R2011a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (73)
05 Jul 2016 thirumagal jayaraman

Actually I have set of MRI images. 19 slices of the brain image. Using the marching cubes algorithm I created a 3D mesh by stacking all the slices. Now I want to get the histogram of this mesh having 19 slices-entire brain. Is it possible to get the histogram of the 19 slices using the mesh?

Comment only
05 Jul 2016 G. Sken

G. Sken (view profile)

..but if you include also the normals as additional output you deserve 6 stars

04 Jul 2016 Athul sukumar

Hello Peter,
I want to convert 2d mri brain image to 3d image. how to use this code for executing the above problem?

Comment only
23 Jun 2016 Peter Hammer

Dear Ahmed, I cannot understand your question. Is it related to my marching cubes function?
-Pete

Comment only
23 Jun 2016 Peter Hammer

Dear Thirumagal, what exactly do you mean by plotting a histogram of the mesh? The mesh is a list of the Cartesian coordinates of points in 3D plus a list of triangles, identified by the indices of the points that comprise them. If a histogram is a set of sorted bins, what is the quantity that you want to sort?
-Pete

Comment only
23 Jun 2016 ahmed kareem1985

Hi I need ,code to compute photo exterior orientation please

Comment only
14 Jun 2016 Paramesh  
11 Jun 2016 thirumagal jayaraman

I want to plot histogram of the output mesh. But the output of Marching cubes is a list of faces and vertices. How can a store it in a variable and plot histogram?

Comment only
02 Jun 2016 Anna Whelan  
06 Apr 2016 Peter Hammer

Dear Sherly - I do not understand your question. Are you asking for the syntax of how to call the function with appropriate input and output arguments? Are you asking if I can reduce the entire algorithm to a "formula"? Please clarify.
-Pete

Comment only
06 Apr 2016 Sherly sari

Hello peter dou you have formula from your marching cubes function?

Comment only
05 Apr 2016 Peter Hammer

Dear Jayant,
Marching cubes does not create a 3D mesh - rather it returns a 2D surface that intersects the 3D, volumetric data such that voxels on one side of the surface have an intensity value above a threshold value while those on the other side have an intensity below the threshold value. It does the same thing as Matlab's "isosurface" function. The primary advantage to my implementation of marching cubes over Matlab's isosurface is that the former is faster and the method is straightforward/transparent.
-Pete

Comment only
05 Apr 2016 jayant prajapat

I have CT image(128 x 128 gray scale) of brain tumor. I want to create 3D mesh of the tumor. How to use this Marching Cube for this purpose

Comment only
07 Nov 2015 Sarah Miller

Hi Peter - Per your response to Eleo on 24 Jul 2015, if a binary stack (0s and 1s) is converted to a grayscale stack (0s and 255s), running the code with an iso of 127 (or anything but 0 or 255) should get you the correct results. Can you please confirm this? Thanks!

Comment only
27 Oct 2015 atmax

atmax (view profile)

Hi Peter,

Could you tell me how to calculate the number of cube containing surface and how to change the cube size please?

Best

Chris

16 Sep 2015 Ryan

Ryan (view profile)

Awesome! Thanks so much!

-Ryan

Comment only
16 Sep 2015 Peter Hammer

Ryan - If I understand your problem correctly, you could use the example code fragment below to create two separate surface models with marching cubes then plot them on the same axes in different colors.
Pete

[f1,v1] = MarchingCubes(X1,Y1,Z1,C1,ISO1);
[f2,v2] = MarchingCubes(X2,Y2,Z2,C2,ISO2);
figure
ph1=patch('vertices',v1,...
'faces',f1,...
'facecolor',[1 0 0],...
'facelighting','phong');
hold on
ph2=patch('vertices',v2,...
'faces',f2,...
'facecolor',[0 1 0],...
'facelighting','phong');
axis equal
light

Comment only
16 Sep 2015 Ryan

Ryan (view profile)

Hi Peter,

I was interested to see if there is a way that I could plot multiple isosurfaces at once with different colors on the same plot?

Thanks,
-Ryan

Comment only
18 Aug 2015 Peter Hammer

Ali - I am having a difficult time understanding exactly what you are trying to do. Marching cubes is a very straightforward algorithm: you pass it a 3D grayscale image along with the grayscale intensity value at which you want to construct a surface (i.e., the isovalue). The function returns the surface, defined as a list of triangles whose vertices are determined by interpolating between image voxels that lie on opposite sides of the isovalue. It does not select the isovalue automatically - you must specify it. Is this what you are trying to do?
Pete

Comment only
17 Aug 2015 ali Hasan

Dear Peter
I am wondering how can I use marching cube algorithm for segmentation because I already use snake algorithm for segment tumors and I want to compare it with marching cube
Regards
Ali

Comment only
14 Aug 2015 Peter Hammer

Dear Ali - I am glad you were able to use marching cubes with your data. To increase smoothness, you could simply insert a slice between every two adjacent slices of your original MRI data that is the mean of the two adjacent slices.
Pete

Comment only
14 Aug 2015 ali Hasan

Dear Peter
I success to implement your code on segmented MRI slices and it gives me good 3D model but I need to make the 3D model more smooth because the margin of slices is appear clearly on 3D model do you know how can I make interpolation between slices in order to increase the smoothness between slices?
Regards
Ali

Comment only
11 Aug 2015 ali Hasan

Dear Peter
Thank you for your reply
Is it possible to use Marching cube to segment the tumors such as other segmentation methods or not?
Regards
Ali

Comment only
28 Jul 2015 Peter Hammer

Dear Ali,
No, marching cubes is not for building 3d models. It is for fitting a triangulated isosurface to 3d volumetric data. You can think of it as segmenting the 3d image...which it sounds like you have already done.
Pete

Comment only
28 Jul 2015 ali Hasan

Dear Peter
Thank you for your code, I segment the tumor over a series of MRI slices successfully and I want to built a 3D model is it possible to use your code to build my 3D model or not
Regards
Ali

Comment only
24 Jul 2015 Peter Hammer

Dear Eleo T,
Marching cubes operates on 3d grayscale images, not on segmented volumes. Try running it on the MRI data, with isovalue set to the same value used to arrive at your segmentation.

-Pete

Comment only
22 Jul 2015 Eléo T

Dear Peter,

Thank you for your function, it helps a lot !

I've to reconstruct three segmented volumes of a MRI one (WM, GM and CRL) (dimensions : 181 X 217 X 181)
I've used your function and tried a lot to work it out without success ...
I'm thinking I'm not far for it to work, but Matlab returns me this result :
F=[ ]
V=[ ]

I've put iso=1, (according to your last comment, I've done the his to and obtained 2 pics : one in 0 and one in 1, because my volumes are segmented from the original MRI volume).

Can you help ?

Thanks for your work

Eleo

Comment only
16 Jul 2015 Peter Hammer

Dear Karen,
The MarchingCubes function is used simply by executing the single command:

[F,V] = MarchingCubes(x,y,z,c,iso);

Note that x, y, and z are matrices of the same size as c, which is the 3-d matrix of image intensity (grayscale) values. You can create coordinate matrices x, y, and z using Matlab's meshgrid command:

[x,y,z]=meshgrid(xmin:xdelta:xmax,ymin:ydelta:ymax,zmin:zdelta:zmax);

Here, xmin and xmax are the x-coordinates of the lower and upper extents of the 3-d image and xdelta is the x resolution. (Similarly for y and z). In the call to MarchingCubes, iso is the isovalue, i.e., the value of image intensity at which you want the output surface to lie. For example, for a CT scan in which you want to segment the surfaces of bone, iso is a value of image intensity that lies between the black background and the darkest elements that you consider to be bone. There is no universal method for selecting this value, as it depends on exactly what the user hopes to achieve with the segmentation. One suggestion is to look at the histogram of image intensity values with a command like this:

hist(c(:),200)

Sometimes the shape of the histogram shows 2 or more distinct peaks which are identifiable as, for example, bone and soft tissue. In this case just choose iso as a value that separates the two peaks.

Comment only
15 Jul 2015 Karen

Karen (view profile)

Hello, I was wondering if you have an example script available somewhere that shows how to use this function correctly? (Along with sample DICOM files?) Thanks for sharing your work!

Comment only
30 Mar 2015 Peter Hammer

Dear iul,
In my experience, the dicom format for CT images is not always the same. Matlab has some tools for reading dicom images; if they don't work, you might have to use matlab's fread. Good luck.

Comment only
29 Mar 2015 iul

iul (view profile)

Hi,Dr.Peter,
i succesfully use your code for the CT head data set from this site(http://graphics.stanford.edu/data/voldata/),meanwhile my goal is to use the code in a dicom file format image each image have a size of 515kb,how to load these data in order to made the 3d matrix.

Thanks for your great support.

11 Mar 2015 brhm

brhm (view profile)

I fixed the problem Mr. Hammer. Ty for answering. Btw I'm dealing with organ segmentation, that's why I had to convert my CT data to the form of a logical 3D matrix in order to use that logical matrix as the input of another algorithm. Also I have some questions about landmarking of a volume (liver, kidney, etc.). If you have a background, I'd like to send a detailed e-mail that includes a couple of images attached.

Thank you very much again.

Regards..

Comment only
06 Mar 2015 Peter Hammer

Dear Marcos,
I am not sure what you mean by a "three-dimensional mesh". Marching cubes performs the same task as Matlab's isosurface function: it creates a surface mesh of triangles that passes through volumetric grayscale data at a constant grayscale value. If this is what you want, marching cubes might be an appropriate tool. If you are looking for a three-dimensional volumetric mesh (e.g., a connected mesh of tetrahedra that occupies volume), it is not an appropriate tool. Before working on your 2nd problem (example of passing the arguments to the code), it makes sense to ensure that marching cubes is appropriate for your task.
Pete

Comment only
06 Mar 2015 Peter Hammer

Dear brhm, the marching cubes method was developed for segmenting volumetric grayscale data. For grayscale data, ISO defined the grayscale value at which to construct the isosurface. For logical data, verying the isovalue between zero and one will not have much effect on the ouput surface...because the isosurface will always pass through the same voxel edges. Why is your CT data in the form of a logical 3D matrix?
Pete

Comment only
06 Mar 2015 Marcos Albarracin

Hi, Dr. Peter,

Thank you very much for your code.
I have two problem.
Firts i am new in the topic.
My goal is to build a three-dimensional mesh from a set of tomographic images of a porous medium.

I would like to know how your code would help me in my goal.

On the other hand like you could give a small example of how to run your code because I'm not getting.

Very thanks

Comment only
25 Feb 2015 brhm

brhm (view profile)

Hello Dr. Peter,

Thank you very much for your code. I'm planning to use your code to get faces and vertices informations which are critical for me. I executed your code with different ISO values (between 0 and 1 ) by holding other parameters the same for my logical 3D matrix which is loaded by CT slices and couldn't figure out how it works. What's the deal with ISO? I'd be really grateful if you could explain me how ISO affects the output. Thank you for your helps in advance.

Regards..

[F,V] = MarchingCubes(X,Y,Z,C,???ISO???)

Comment only
12 Jan 2015 Peter Hammer

Dear iul,
You must first load the CT slices into a single 3d matrix, much like Hwathanie has done in the code fragment in the comments below. Have you been able to do this?

Comment only
10 Jan 2015 iul

iul (view profile)

Hello Dr,Peter,
Hello all,
Thank you very much for your code.
can u help me in my request of 3 jan 2015.

Regards;

Comment only
05 Jan 2015 Peter Hammer

Dear Hwathanie,
For the CT data that I read, it was necessary to use big-endian byte ordering when reading the file. E.G.,

data=fread(fid,[512,512],'uint16','ieee-be');

Does this solve your problem?

Sincerely,
Pete

Comment only
03 Jan 2015 iul

iul (view profile)

Hello Dr.Petter,

Could you help me plz.
i have multiple Ct-scan slices of paratoid galnde(dicom file),and i want to make a 3D reconstruction .so how to create my 3d grid from these slices???

Regards;

03 Jan 2015 Hwathanie

Hello Peter, Thank you very much for your code. I was trying to use it with the bunny data set provided in [http://graphics.stanford.edu/data/voldata/] with the following Matlab code.

for i = 1:361
fid = fopen(strcat('bunny\',num2str(i)));
v(:,:,i) = reshape(fread(fid, inf, '*uint16'), [512 512]);
fclose(fid);
end

[x y z] = meshgrid(1:512, 1:512, 1:361);
x = single(x);
y = single(y);
z = single(z);

[F,V] = MarchingCubes(x,y,z,v,500);

This took a long time and gave me an output like this [http://tinypic.com/r/22axb7/8]. Could you please tell how to correctly use your code.

09 Jul 2014 Peter Hammer

Rao - The function is not flexible with input and output arguments. To properly choose the input arguments, please read the long section of commented text at the beginning of the mfile. I cannot answer your question about antenna design without knowing more about your approach. What type of image/data are you passing to marching cubes?

Comment only
08 Jul 2014 nageswara rao

hi peter,
it shows an error
Error using ==> MarchingCubes at 33
wrong number of input and/or output arguments
can i use it for antenna designs?

Comment only
07 Jul 2014 Peter Hammer

Vivek - It could be that the isovalue needs to be adjusted. I have noticed that when image is somewhat noisy and I choose too low an isovalue, the resulting surface is noisy as you describe.

Comment only
06 Jul 2014 vivek Kandasamy

Hi Peter, I have tried to create a mesh from volumetric CT data. But the final surface has spiky triangles on the surface creating an uneven surface. How can i avoid this?

22 Jun 2014 Itzik Ben Shabat  
12 Mar 2014 Peter Hammer

Vedpal - Marching cubes is an algorithm to produce a mesh of a surface at a given isovalue contained within 3d volumetric data. In other words, it identifies only bounding surfaces and does not fill the volume within the boundaries with "3-d" mesh elements like tetrahedra or cubes. You will have to come up with a meshing algorithm to do this...or find a software package that can do this.

Comment only
01 Mar 2014 dai hong

Thanks Peter!

27 Feb 2014 Vedpal Singh

Thanks Peter, for your valuable suggestion.

Regarding the 2nd question:
Using marching cube, I got a 3D result. But this result is not perfect because resultant 3D area is not perfectly filled. Boundary is clear but some inside space is vacant.
So, how we fill out this vacant space inside the 3D image boundary.

26 Feb 2014 Peter Hammer

Vedpal - The construction of the edgeTable and triTable values is tricky. I think the original paper on marching cubes by Lorensen & Cline does a pretty good job of explaining. I will email you the paper. Regarding your second question on filling holes, I am not sure I understand what you are asking. Can you be more specific?
-Pete

Comment only
22 Feb 2014 Vedpal Singh

Hello,
Really a very good work.
Actually,I am confused in function

[edgeTable, triTable] = GetTables();

How can we calculate the edgeTable and triTable values.

and

How we can feel the holes in 3D desined through marching cube algorithm.

Thanks

31 Oct 2013 Peter Hammer

Itzik - Yes, the OpenGL renderer runs faster than default zbuffer - possibly a lot faster depending on hardware. I use a Phong lighting model in MarchingCubes, and Matlab's OpenGL renderer does not support Phong. Using the OpenGL renderer with an alternate lighting model, like Gouraud, is a good choice for large meshes where speed is an issue - and effects of the lighting model are less noticeable.

Comment only
31 Oct 2013 Itzik Ben Shabat

Is there a particular reason why you didnt use the opengl renderer property for the figure? it runs much faster for me when adding it instead of the default renderer.

Comment only
21 Oct 2013 Peter Hammer

JK - Marching cubes works on volumetric data, not on point clouds. If your point cloud data represents points on a surface, you might explore convex hull based triagulation methods. See Matlab's DELAUNAY function, for example.

Comment only
21 Oct 2013 JK Hwang

Hi Peter, I have tried to create mesh from 3D point cloud. How can I use your code to get my goal?

13 Aug 2013 VD

VD (view profile)

Thank you, Peter.

Comment only
12 Aug 2013 Peter Hammer

VD - Rather than converting your (grayscale) CT data to binary, pass the grayscale data to the function. Marching cubes interpolates between nodal values of image intensity to produce a smooth isosurface.

Comment only
10 Aug 2013 VD

VD (view profile)

Hi Peter, I have database of about 100 CT scan images and I've converted it into binary images having bones as white. Using your package I am generating 3D skull from binary slices, however it shows step-wise(spiky) surface. looks like each slice as separate step. How can I get smooth continuous surface using this package?

Comment only
25 Jul 2013 Anton Semechko

Thanks for the submission Peter! It works great!

23 Jul 2013 VD

VD (view profile)

Thank you very much Peter.

Comment only
23 Jul 2013 Peter Hammer

Hi VD - Input arguments x, y, and z are expected to be the same as would be passed to Matlab's isosurface function. In fact, I posted the vectorized marching cubes function to provide a fast alternative to isosurface. The help section for the isosurface function gives a pretty clear explanation of what is expected for these input arguments: the arrays X, Y, and Z represent a Cartesian, axis-aligned grid. C contains the corresponding values at these grid points. The coordinate arrays (X, Y, and Z) must be monotonic and conform to the format produced by meshgrid. C must be a 3D volume array of the same size as X, Y, and Z.

I hope this is helpful.

Pete

Comment only
22 Jul 2013 VD

VD (view profile)

Hi,I have difficulty understanding what I need to pass as x,y,z & c matrices? I have 99 slices of binary image for which I need to generate surface. Would you please give me some example?

Comment only
14 Jun 2013 Anton Semechko

I think it may be helpful, for some people who are concerned about the efficiency of the code, if you added to your description the actual size of the CT dataset that was used to test the function. In other words, how big was the CT image of the bunny?

Also did you have a chance to benchmark this code against the built-in 'isosurface' function?

Comment only
10 Jun 2013 Marleen

Hi Peter,

Never mind the question below, I figured it out.
Thanks for your m-file, it works perfectly!

10 Jun 2013 Marleen

Hi Peter,

Thanks for the marching cubes algorithm!
I am not so familiar with imaging yet, and I wonder how I should create the matrix C? I know for each point in the mesh (x,y,z) whether it is inside the structure or not. How do I translate this to C, or where can I find more information on such a matrix?

Thanks a lot!

Comment only
09 Apr 2013 zhonglee323@gmail.com

hello Hammer,

what is "3D matrix of scalar values C" actually mean? If I get a sample point matrix, which ndims = 2, how can I transform them to 3D matrix?

Thanks

Comment only
09 Apr 2013 Peter Hammer

Sarah - the algorithm assumes no knowledge of the data outside of the 3D matrix, so it does not "close" the surface on the boundary. For example, if you take a CT scan of a basketball and apply marching cubes to a subvolume containing only a hemisphere, it will return two disconnected surfaces: one hemispherical shell for the outside surface of the ball and one hemispherical shell for the inside surface of the ball.

Comment only
08 Apr 2013 Sarah Miller

Question: If I have an item that does not terminate at the edges of my 3D matrix (the scan is only a part of the sample), will this algorithm count the exterior of the matrix as well? Let me know if more info is needed.

Comment only
26 Jan 2013 gang

gang (view profile)

hi,Peter,Tks for your work!
Is it the same as the funciton 'isosurface' of matlab 2012b?

14 Nov 2012 Chen Vic

thanks a lot!

Comment only
30 Sep 2011 Oliver Woodford

Brilliant. Very fast.

01 Sep 2011 Shusen

Shusen (view profile)

 
20 Aug 2011 wu

wu (view profile)

good

Comment only
Updates
18 Aug 2011 1.1

Added image of Stanford bunny meshed using this function. Added text describing this example to description.

30 Sep 2011 1.2

Revised 30 September, 2011 to add code by Oliver Woodford for removing duplicate vertices.

23 Jul 2013 1.3

Changed description to include benchmarking results, and changed help section of mfile to more explicitly describe input arguments.

Contact us