# Combining Image3D and Graphics3D

I want to combine an Image3D and a Graphics3D using Show. Here is the code:

data = Table[
Abs[((3 - 3 z) (.5 Sign[3 - 3 z] + .5)) (x Sin[x] + y Cos[y])],
{x, 0, 15, .1}, {y, 0, 10, .1}, {z, 0, 3, .1}
];
Show[Image3D[data/Max@data],
Graphics3D[{AbsoluteThickness[5], Blue,
Line[{{0, 0, 0}, {2, 2, 2}, {3, 3, 2}, {8, 5, 2}, {14, 9, 2}, {15,
10, 0}}]}]]


While the coordinates are aligned, the output is not.

See the blue line on the right corner. How can I fix the problem?

Edit: The issue is resolved. There are two answers below. Both are helpful but neither of them is complete. Once any of them is competed, I'll mark it as accepted. I write one possible answer to the question here (I added {#[[3]], #[[2]], #[[1]]}/delta & /@ pointList to my code):

With[{delta = .1},
data = Table[
Abs[((3 - 3 z) (.5 Sign[3 - 3 z] + .5)) (x Sin[x] + y Cos[y])],
{x, 0, 15, delta}, {y, 0, 10, delta}, {z, 0, 3, delta}
];
Show[Graphics3D[{AbsoluteThickness[5], Blue,
Line[{#[[3]], #[[2]], #[[1]]}/
delta & /@ {{0, 0, 0}, {2, 2, 2}, {3, 3, 2}, {8, 5, 2}, {14, 9,
2}, {15, 10, 0}}]}], Image3D[data/Max@data]]
]

• Maybe you'll have better luck with the alignment if you use Raster3D[] instead... Apr 22, 2013 at 10:45

While the coordinates are aligned

Unfortunately, they are not because the moment you create a Image3D, the coordinate system is different. Usually, if your image has the dimensions $n_x \times n_y \times n_z$ the coordinate system of your volume ranges from {{1,nx},{1,ny},{1,nz}} depending on the implementation.

If you want to combine an Image3D and some other stuff, you have to rescale your coordinates. This can be done using RescalingTransform:

data = Reverse@Table[
Abs[((3 - 3 z) (.5 Sign[3 - 3 z] + .5)) (x Sin[x] + y Cos[y])], {z,
0, 3, .1}, {y, 0, 10, .1}, {x, 0, 15, .1}];
img3d = Image3D[data/Max@data];
rescale =
RescalingTransform[{{0, 15}, {0, 10}, {0, 3}},
Transpose[{{1, 1, 1}, ImageDimensions[img3d]}]];
Show[img3d,
Graphics3D[{AbsoluteThickness[5], Blue,
Line[rescale /@ {{0, 0, 0}, {2, 2, 2}, {3, 3, 2}, {8, 5, 2}, {14,
9, 2}, {15, 10, 0}}]}], Axes -> True]


Additionally, I'm pretty sure you think your Table call does something different. Please note the following:

Table[0, {x, 5}, {y, 6}, {z, 7}] // Dimensions


This gives a volume having dimension z=5, y=6 and x=7! If you look at my Table call in the code, you see that I reversed the iterators.

At last, please note that data in Image and Image3D is usually in reverse order compared to the usual coordinate system.

• You beat me! but there still axes problem as described by cormullion. I used Line[1/0.1 {#[[3]], 10 - #[[2]], 15 - #[[1]]} & /@ lineNodes] to transform them. Apr 22, 2013 at 10:41
• @Silvia Look at my Table call. What people forget is, that Table[0, {x, 5}, {y, 6}, {z, 7}] does not the expected thing. Dimension is reversed! Maybe I should add this to the post. Apr 22, 2013 at 10:45
• You are right Silvia. I realized the same thing. See my comment under cormullion's answer. Apr 22, 2013 at 10:45
• @halirutan Sorry I didn't notice it. But I think it should be {z, 3, 0, -.1}? Compare the composed result with that composed with say ContourPlot3D and the line graphics. Apr 22, 2013 at 10:49
• @Silvia Yes, this is maybe true. I didn't know what to OP wanted but the first dimension in Image/Image3D is reversed. Apr 22, 2013 at 10:54

This may not be the problem (and this may not be an answer), but I noticed the axes were different:

Graphics3D[{AbsoluteThickness[5], Blue,
Line[{{0, 0, 0}, {2, 2, 2}, {3, 3, 2}, {8, 5, 2}, {14, 9, 2},
{15, 10, 0}}]},  Axes -> True, AxesLabel -> {"x", "y", "z"}]


Image3D[data/Max@ data, Axes -> True, AxesLabel -> {"x", "y", "z"}]


• Thanks cormullion. I noticed the same issue. A simple transform on the line points resolves the issue: {#[[3]], #[[2]], #[[1]]}/delta & /@ pointList. But I wonder if it is a bug? Apr 22, 2013 at 10:37
• The docs say 'slices, rows, and columns' correspond to x, y, and z..., so it may be deliberate. @halirutan as usual knows what's going on. Apr 22, 2013 at 10:43
• @Mohsen I don't consider it as a bug. IMO Image3D, like Image, uses coordinates like Array/Matrix, which is different from Graphics by convention. Apr 22, 2013 at 10:44

Everybody seems happy already at the time of this answer. But the following resources will help a newcomer develop good habits: