MATLAB: How to export 3D image from matlab and import it into maya

import into mayamaya

I have a 3D image in Matlab and I want to deform it using Maya,But I have no idea how I can 1)export it from Matlab and 2) how to import it into Maya.
Thanks alot!

Best Answer

Hi Ava,
I am assuming the image you are working with has already been segmented (or labelled) into distinct components. If so, here is what you can try:
Suppose the object of interest contained in your image is represented by voxels with values equal to 1, while the background is represented by values equal to -1.
1) First you may want to remove the voxelization (aka staircase) artefacts. You can do this by applying a low-pass filter to your image using the built-in MATLAB function titled ' smooth3'. Alternatively, you can implement your own pre-processing routine.
2) Extract the isosurface using marching cubes algorithm. For example, you can use the built-in function 'isosurface' or you can use the routine submitted by Peter Hammer: http://www.mathworks.com/matlabcentral/fileexchange/32506-marching-cubes
3) Export the mesh to a .stl file using the 'stlwrite' function that can be download from here: http://www.mathworks.com/matlabcentral/fileexchange/20922-stlwrite-write-binary-or-ascii-stl-file
Note that while using the above function, you may encounter the following error:
??? Undefined function or method 'narginchk' for input arguments of type 'double'.
Error in ==> stlwrite at 55
narginchk(2, inf)
To get rid of it you will have to edit out 'narginchk(2, inf)' on line 55
4) Finally, you can now import the .stl file into Maya and do what you have to ...
Here is an example:
% Simulate a segmented image
x=linspace(-1.1,1.1,50);
[x,y,z]=meshgrid(x);
F=(x.^4 + y.^4 + z.^4) - 10*(0.13)^2*(x.^2 + y.^2 + z.^2); % pill-box
F(F<=0)=-1;
F(F>0)=1;
figure('color','w'), isosurface(x,y,z,F,0), axis equal
% Smooth
F=smooth3(F);
figure('color','w'), isosurface(x,y,z,F,0)
% Extract the surface mesh
M=isosurface(x,y,z,F,0);
tr=TriRep(M.faces,M.vertices);
figure('color','w'), h=trimesh(tr); axis equal
set(h,'FaceColor','w','EdgeClor','b')
% Write to .stl
stlwrite('PillBoxExample.stl',tr.Triangulation,tr.X)
Good luck!
Related Question