Here is a segmented volume with 96 different features. I wish to visualize each feature with a distinct color. I followed this documentation and used Blend to assign colors to the features. However, when I check each individual feature, I find that in some cases the feature is assigned with one color while the rendered output shows a different color.

I think the use of Blend is the problem here.

The alternative that I can think of is to first apply ClusteringComponents using KMeans and then use Colorize. However, using ClusteringComponents on a segmented data is a redundant process and it takes a long time and moreover, there is no guarantee that it will return all the segmented (initial) parts.

How can I render this volume properly so that each segment gets rendered with the same color that is assigned to that segment?


Download the zip that contains the .vtk files and extract it. Then, find all these files in Mathematica

vtks = FileNames["*.vtk", {"/path/to/AbdominalAtlas2011/"}]

After that, use Import[file, "Elements"] to see what Mathematica can extract from vtk files. Use whatever you like, but I guess something like this should help you

mkGraphics[file_String] := {RandomColor[], EdgeForm[None], 
   Import[file, "GraphicsComplex"]};

 mkGraphics /@ Select[vtks, Not[StringFreeQ[#, "Rib"]] &],
 BoxRatios -> Automatic

Et voilà

Mathematica graphics

Bringing in the original colors

The colors are stored in a separate file as far as I can see, but it's a simple format and you can turn it into an association that maps the model-id to its color:

colors = Association @@ Apply[
    #1 -> RGBColor @@ ({##3}/255) &,
    Import["/path/to/AbdominalAtlas2011/AbdominalAtlasColors.ctbl", "Table"], 

Fixing the mkGraphics function

mkGraphics[file_String] := Module[
   {id = ToExpression[StringSplit[file, "_"][[2]]]}, {colors[id], 
    EdgeForm[None], Import[file, "GraphicsComplex"]}

And finally everything without the muscles

 mkGraphics /@ Select[vtks, StringFreeQ[#, "muscle"] &],
 Lighting -> "Neutral"

enter image description here

| improve this answer | |
  • $\begingroup$ This is great. But I have a predefined set of color and opacity values. How can I apply this? $\endgroup$ – Majis Aug 13 '18 at 15:01

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.