0
$\begingroup$

I'm working with ~ 10^8 voxels which can have RGB values outside of 0 - 1. Currently I go through nested For statements with a bunch of || tests. It would be great to set such voxels to {0,0,0} within Image3D. Better yet would be to turn the voxel transparent.

Here is a small example that obviously doesn't use all possible combinations of out of range elements:

test = {{{1.5, 1, 1}, {-1.5, 0, 0}, {1.5, 0, 0}, {-1.5, 0, 0}}, {{1.5,
      1, 1}, {-1.5, 0, 0}, {1.5, 0, 0}, {-1.5, 0, 0}}};

Image3D[test]
$\endgroup$

1 Answer 1

2
$\begingroup$

R,G,B values should be between 0 and 1. MMA will simply clip you values. E.g, the first voxel {1.5,1,1} will be clipped to {1,1,1} and therefore displayed as "white", what makes it invisible on a white background.

test = {{{{1.5, 1, 1}, {-1.5, 0, 0}, {1.5, 0, 0}, {-1.5, 0, 
      0}}, {{1.5, 1, 1}, {-1.5, 0, 0}, {1.5, 0, 0}, {-1.5, 0, 0}}}};
Image3D[test]

enter image description here

To get the full color palette, you must rescale your data, using e.g. Rescale:

test = {{{{1.5, 1, 1}, {-1.5, 0, 0}, {1.5, 0, 0}, {-1.5, 0, 
      0}}, {{1.5, 1, 1}, {-1.5, 0, 0}, {1.5, 0, 0}, {-1.5, 0, 0}}}};
test = Rescale[test, {-1.5, 1.5}]
Image3D[test]

enter image description here

However, if we want voxel with invalid colors to be invisible, we can add an alpha channel and set it to 0 and use ColorSpace -> "RGB". To demonstrate this we also need to change the data:

test = {{{{0, 1, 1}, {-1.5, 0, 0}, {1, 0, 0}, {0, 0, 0}}, {{0, 0, 
      1}, {0, 0, 1}, {0, -1, 0}, {-1.5, 0, 0}}}};
test = Map[Append[#, If[(0 <= Min[#]) && (Max[#] <= 1), 1, 0.]] &, 
  test, {3}]
Image3D[test, ColorSpace -> "RGB"]

enter image description here

$\endgroup$
3
  • $\begingroup$ When all three RGB values are between 0 and 1, I keep those values. If any RGB value is outside of 0 -to 1, I currently set to black. I am basically setting a window, so rescaling is not an option for me. Like I mentioned in the question, black is OK, but invoking an alpha channel to set the voxel as transparent is even better. To do that, my current code uses nested For statements with inequalities and Or. It takes 10 minutes on 512 x 512 x 512 x 3. Maybe, I am stuck with it. $\endgroup$
    – Youvan
    Mar 30, 2021 at 17:53
  • 1
    $\begingroup$ I added this to my answer. $\endgroup$ Mar 30, 2021 at 18:39
  • $\begingroup$ That's great. Now I need to figure out how to replace the RGB palette with a custom palette, but that's another question if need be. $\endgroup$
    – Youvan
    Mar 30, 2021 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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