# Visualize 3D slices of an 3D image volume?

**URL:** <https://discourse.julialang.org/t/visualize-3d-slices-of-an-3d-image-volume/1255>\
**Category:** Visualization\
**Tags:** question, glvisualize, plotting\
**Created:** [January 3, 2017, 1:50am UTC](https://discourse.julialang.org/t/visualize-3d-slices-of-an-3d-image-volume/1255 "2017-01-03T01:50:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jwu/32/3775_2.png) [@jwu](https://discourse.julialang.org/u/jwu)\
**Post date:** [January 3, 2017, 1:50am UTC](https://discourse.julialang.org/t/visualize-3d-slices-of-an-3d-image-volume/1255/1 "2017-01-03T01:50:16Z")

</div>

I’d like to visualize a 3D image volume, but did not find an example in Plots.jl and GLVisualize.jl.

one example is here

 ![](https://global.discourse-cdn.com/julialang/original/3X/4/1/41380a17961dfeea8dcf22d33311535a5bf95742.png)

I found that there is an example in the tutorial of GLAbstraction, but it requires an existing fragment shadder file for the texture image.

> <https://github.com/JuliaGL/GLAbstraction.jl/blob/master/tutorials/depth_stencils1.jl>

I got this pretty result:

 ![](https://global.discourse-cdn.com/julialang/original/3X/0/0/00d3fa32045b413721b2e77cee60f8480e363b0d.jpeg)  
But I did not find a way to visualize other images.

Do we have this functionality ready? If not, could anyone give a hint to implement?

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [January 3, 2017, 7:42am UTC](https://discourse.julialang.org/t/visualize-3d-slices-of-an-3d-image-volume/1255/2 "2017-01-03T07:42:05Z")

</div>

It should be possible with glvisualise and GLPlot, see [interactive volume plots on Vimeo](https://vimeo.com/180307247) . I cannot seem to find the example code, unfortunately ☹

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [January 3, 2017, 9:26am UTC](https://discourse.julialang.org/t/visualize-3d-slices-of-an-3d-image-volume/1255/3 "2017-01-03T09:26:04Z")

</div>

I think the example code is: [https://gist.github.com/SimonDanisch/7f700728485a4742ad3c0767fb573828#file-volume\_example-jl](https://gist.github.com/SimonDanisch/7f700728485a4742ad3c0767fb573828#file-volume_example-jl), but only @sdanisch can tell us if that is still the correct way.

---

<div class="post-metadata">

**Author:** ![jwu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jwu/32/3775_2.png) [@jwu](https://discourse.julialang.org/u/jwu)\
**Post date:** [January 3, 2017, 3:31pm UTC](https://discourse.julialang.org/t/visualize-3d-slices-of-an-3d-image-volume/1255/4 "2017-01-03T15:31:40Z")

</div>

Thanks for your note. The example did not show the lower right orthogonal visualization.

![](https://global.discourse-cdn.com/julialang/original/3X/3/8/38ce990b041b9857c34304dd9429b6cb977b659d.jpeg)

I can show single slices with sprites, but I did not find a way to rotate the images…

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [January 3, 2017, 3:43pm UTC](https://discourse.julialang.org/t/visualize-3d-slices-of-an-3d-image-volume/1255/5 "2017-01-03T15:43:17Z")

</div>

I don’t think there is a function to do that type of plot automatically. But you might try to make a copy of `vol` (from the example), then replacing all the points you want to be transparent with `NaN`, eg

```julia
vol2 = copy(vol)
for k in 1:size(vol,3)
  for j in 1:size(vol,2)
    for i in 1:size(vol,1)
      i == 75 | j == 75 | k == 75 | (vol2[i, j, k] = NaN)
    end
  end
end
plot(vol2)

```

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [January 3, 2017, 3:56pm UTC](https://discourse.julialang.org/t/visualize-3d-slices-of-an-3d-image-volume/1255/6 "2017-01-03T15:56:21Z")

</div>

Yes you can do this:

> [@Julia counterparts to slice and contourslice in Matlab](https://discourse.julialang.org/t/julia-counterparts-to-slice-and-contourslice-in-matlab/686/5):
>
> Yeah you can use GLVisualize it slight_smile[[Volume slicer] ](https://www.youtube.com/watch?v=upWE58DmaR8) Code: Sorry for any inconveniences, I just right now fixed up GLPlot to work with the newest GLVisualize, so it might still act buggy. It would be possible to add this code sample as a visualization style. IJulia integration is not great right now, since the interactivity will mostly be missing confused It’s implemented for the Plots.jl package, since it is quite static to begin with, but not for GLVisualize/GLPlo…

If this example doesn’t work for you, let me know;)

---

<div class="post-metadata">

**Author:** ![jwu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jwu/32/3775_2.png) [@jwu](https://discourse.julialang.org/u/jwu)\
**Post date:** [January 5, 2017, 7:07pm UTC](https://discourse.julialang.org/t/visualize-3d-slices-of-an-3d-image-volume/1255/7 "2017-01-05T19:07:28Z")

</div>

@sdanisch Thanks a lot for the examples!  
the `volome_slice.jl` works after fixing a few small bugs.

- manually build and precompile Plots.jl
- change `create_window` to GLPlot.init()
- add `sleep(1000)` after glplot to keep the window running

All of other examples do not work for me…  
I have created a pull request to GLPlot.jl to fix a few known issues:

> <https://github.com/SimonDanisch/GLPlot.jl/pull/35>
>
> fix example errors, still not working.
> 
> \- replaced \`create\_window\` by GLPlot.i…nit()
> \- add typealias for Vec2/3/4 types.

 ![](https://global.discourse-cdn.com/julialang/original/3X/9/3/93bf375838c9e7d52e3b9dfa22fab11d376cf8c5.jpeg)  
another question is that how can I control the RGB value? I would like to see gray scale image here, but it is bluish.  
In other use cases, I would like to see RGB or RGBA image. where can I switch these? In the shader file of GLVisualize.jl?

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [January 5, 2017, 10:44pm UTC](https://discourse.julialang.org/t/visualize-3d-slices-of-an-3d-image-volume/1255/8 "2017-01-05T22:44:45Z")

</div>

You can pass a colormap like this to the plotting function:

```nohighlight
glplot(
        slice,
        stroke_width = 0.0f0,
        color_norm = Vec2f0(0, 1), # normalize values to be between 0 and 1, before color lookup
        color_map = RGBA{Float32}[....] # you can pass an array of RGBA values as a colormap
    )

```

---

<div class="post-metadata">

**Author:** ![jwu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jwu/32/3775_2.png) [@jwu](https://discourse.julialang.org/u/jwu)\
**Post date:** [January 5, 2017, 10:58pm UTC](https://discourse.julialang.org/t/visualize-3d-slices-of-an-3d-image-volume/1255/9 "2017-01-05T22:58:09Z")

</div>

it works 🙂

 ![](https://global.discourse-cdn.com/julialang/original/3X/e/f/ef74cd54ffcd6930b03561e30b892943c64bb63e.jpeg)

I used the gray scale colormap.  
color\_map = RGBA{Float32}[RGBA{Float32}(0,0,0,1), RGBA{Float32}(1,1,1,1)]

I have another format with RGBA images. If the slice image is already an Array{RGBA, 2}, GLPlot will display it directly without specifying colormap?

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [January 5, 2017, 11:11pm UTC](https://discourse.julialang.org/t/visualize-3d-slices-of-an-3d-image-volume/1255/10 "2017-01-05T23:11:16Z")

</div>

That should work pretty similar.  
I adapted the volume slice example to work with volume of colors:  
[https://github.com/SimonDanisch/GLPlot.jl/blob/master/example/rgb\_volume\_slice.jl](https://github.com/SimonDanisch/GLPlot.jl/blob/master/example/rgb_volume_slice.jl)

---

<div class="post-metadata">

**Author:** ![jwu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jwu/32/3775_2.png) [@jwu](https://discourse.julialang.org/u/jwu)\
**Post date:** [January 5, 2017, 11:17pm UTC](https://discourse.julialang.org/t/visualize-3d-slices-of-an-3d-image-volume/1255/11 "2017-01-05T23:17:57Z")

</div>

Again, it works great 🙂

 ![](https://global.discourse-cdn.com/julialang/original/3X/3/e/3ea891c430d0bfcc09bc913517fbeccf1909e3e8.jpeg)

Thanks a lot!
