6

After importing a 3D image in STL format, my possibilities in regard to "3D Volumetric Image Processing" seem rather limited. All examples are given with TIFF files. Is there a way to convert from STL to TIFF or work with an STL image in some other way?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Simon
  • 493
  • 2
  • 6

2 Answers2

9

Here is something quick and dirty to convert from a Graphics3D to Image3D (the second argument is sort of a quality knob, the higher the number the more slices are taken):

img3Dify[gr3d_Graphics3D, qu_] := Module[{pr, br, slices},
  pr = PlotRange /. AbsoluteOptions[gr3d, PlotRange];
  br = Map[Norm@Differences[#] &, pr]; 
  slices = ParallelMap[
    Image[Show[gr3d, ViewPoint -> Top, 
       Lighting -> {{"Ambient", White}}, Background -> None, 
       PlotRange -> {pr[[1]], pr[[2]], #}]] &, 
    Partition[FindDivisions[pr[[3]], qu*2], 2, 1]];
  Image3D[slices, BoxRatios -> br, ColorFunction -> Hue]]

on an example image

spikey = Import["ExampleData/spikey.stl", "Graphics3D"]

spikey

img3Dify[spikey, 40]

spikey as Image3D

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
chuy
  • 11,205
  • 28
  • 48
6

I found that with Mathematica 10 one can use the region functions/distance+watershed to achieve something similar but much faster. Something along these lines can generate ~1 million voxels in a couple of seconds in my machine:

voxelify[region_?RegionQ, res_?NumericQ] := 
 Module[{f, bounds, boxsize, voxsize, data, d2},
  f = RegionDistance[region];
  bounds = RegionBounds[region]; 
  boxsize = -Subtract @@ # & /@ bounds;
  voxsize = Max[boxsize]/res; 
  data = ParallelArray[f[{##}] &, Round[boxsize/voxsize], bounds]; 
  d2 = HeavisideTheta[data - voxsize]; 
  2 - ReplaceAll[WatershedComponents[Image3D[1 - d2]], {0 -> 2}]
  ]

And then

spikey = Import["ExampleData/spikey.stl", "MeshRegion"];
Image3D[voxelify[spikey, 100]]

voxelized spikey

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
franjesus
  • 161
  • 1
  • 2
  • I get this error message (MMA 11.3): ParallelArray::nopar1: Array[f$11729[{##1}]&,{100,95,81},{{-1.54435,1.54435},{-1.46876,1.46876},{-1.2494,1.2494}},List] cannot be parallelized; proceeding with sequential evaluation. – Jonathan Kinlay Mar 28 '19 at 09:12
  • Actually I get the same error message using ParallelArray in any version of MMA, including 10.0, 10.3 and 11.3. – Jonathan Kinlay Apr 01 '19 at 17:42