1
$\begingroup$

I want to get 100 images of 100x100 size 2D image data with "Image3DSlices" command.

randomSpheres = Table[{Sphere[RandomReal[{0, 150}, 3], RandomReal[{1, 10}]]}, {20}]
g3 = Graphics3D[randomSpheres]  (*... eq2*)
si2 = RegionImage[DiscretizeGraphics[g3]];
si3 = si2 // FillingTransform // Binarize;
slice = Image3DSlices[si3]  

However, I cannot set the size of each image of slice... The size seems to be the value of "PlotRange->All", everytime differnet size because the spheres radius and center coodinate are random. I want to make Dimensions of each image of "slice" to be 100x100, as

Dimensions[ImageData[slice[[i]]]] = {100, 100}

for i th image.

Besides, the number of "slice" also seems to depend on the "PlotRange->All" of z direction. I want the number of 2D pictures(="slice") to be 100, but

Dimensions[slice] 

differs everytime... If someone who knows how to get the cubic data, please tell me.

I tried to change eq2 as

g3 = Graphics3D[randomSpheres, PlotRange -> {{0, 100}, {0, 100}, {0, 100}}]

However, the dimensions of each "slice" image was not 100x100 still. Moreover, some of the spheres were not fully filled... In desired images, all spheres should be filled.

$\endgroup$
1
  • $\begingroup$ To make the slices 100 x 100, Maybe try slice = ImageResize[#, {100, 100}] & /@ slice; ? Leaving this as a comment because it doesn't answer the other parts of the question $\endgroup$
    – ydd
    Commented May 23 at 14:14

1 Answer 1

2
$\begingroup$

One way to get 100 slices of 100x100 size, given s random spheres:

SeedRandom[123456];
n = 150; r = 10; s = 20;
spheres = Table[Sphere[RandomReal[{0, n}, 3], RandomReal[{1, r}]], {s}];
mesh = 
  DiscretizeGraphics[Graphics3D@spheres, MaxCellMeasure -> 1, PerformanceGoal -> "Speed"];
bounds = Table[{-r, n + r}, {3}]; size = 100;
img = RegionImage[mesh, bounds, RasterSize -> size]

enter image description here

fimg = img // FillingTransform // Binarize;
ImageDimensions@fimg

{100, 100, 100}

slices = Image3DSlices[fimg];
ImageDimensions /@ slices // Tally

{{{100, 100}, 100}}

Multicolumn[RandomChoice[slices, 9], 3]

enter image description here


Alternatively, Ball can be used instead of Sphere in order to avoid the FillingTransform operation.

SeedRandom[123];
n = 150; r = 10; s = 20;
balls = Table[Ball[RandomReal[{0, n}, 3], RandomReal[{1, r}]], {s}];
mesh = DiscretizeRegion[RegionUnion@balls, MaxCellMeasure -> 1, 
   PerformanceGoal -> "Speed"];

bounds = Table[{0, n}, {3}]; size = 100;
img = RegionImage[mesh, bounds, RasterSize -> size]

enter image description here

slices = Image3DSlices[Binarize@img];
Multicolumn[RandomChoice[slices, 9], 3]

enter image description here

ImageDimensions /@ slices // Tally

{{{100, 100}, 100}}

$\endgroup$
1
  • 1
    $\begingroup$ Thank you so much! Your answer is very useful. Thanks again!! $\endgroup$
    – rani
    Commented May 24 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.