# Split Volume by Label values

**URL:** https://discourse.vtk.org/t/split-volume-by-label-values/5629
**Category:** Support
**Created:** [April 23, 2021, 2:13pm UTC](https://discourse.vtk.org/t/split-volume-by-label-values/5629 "2021-04-23T14:13:55Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Chuck\_Spencer](https://discourse.vtk.org/user_avatar/discourse.vtk.org/chuck_spencer/32/2854_2.png) [@Chuck\_Spencer](https://discourse.vtk.org/u/Chuck_Spencer)
#### Post date: [April 23, 2021, 2:13pm UTC](https://discourse.vtk.org/t/split-volume-by-label-values/5629/1 "2021-04-23T14:13:55Z")

</div>

In vtk.js, I have VTI files that I’m reading in using the `vtkXMLImageDataReader`. The image data has a known, discrete set of label values corresponding to regions. I’d like to separate this volume into separate volumes (ultimately into separate ISO surfaces via MarchingCubes).

How could I go about separating (thresholding? filtering?) my volume data according to the labels on the voxels so I could create one marching-cube actor per label value?

---

<div class="post-metadata">

### Author: ![Chuck\_Spencer](https://discourse.vtk.org/user_avatar/discourse.vtk.org/chuck_spencer/32/2854_2.png) [@Chuck\_Spencer](https://discourse.vtk.org/u/Chuck_Spencer)
#### Post date: [April 23, 2021, 6:53pm UTC](https://discourse.vtk.org/t/split-volume-by-label-values/5629/2 "2021-04-23T18:53:35Z")

</div>

I managed to come up with this based on the [VolumeOutline Example](https://kitware.github.io/vtk-js/examples/VolumeOutline.html). Seems to work. Have I reinvented a wheel here? Anyone see any problems with doing it like this?

```
getVolumeFilteredByThreshold(sourceData, thresholdMin, thresholdMax) {
  const imageData = vtkImageData.newInstance(sourceData.get("spacing", "origin", "direction"));
  imageData.setExtent(sourceData.getExtent());
  const values = sourceData
    .getPointData()
    .getScalars()
    .getData()
    .map((x) => {
      return x >= thresholdMin && x < thresholdMax ? x : 0;
    });
  const dataArray = vtkDataArray.newInstance({
    numberOfComponents: 1,
    values,
  });
  imageData.getPointData().setScalars(dataArray);

  return imageData;
}
```

---

<div class="post-metadata">

### Author: ![Chuck\_Spencer](https://discourse.vtk.org/user_avatar/discourse.vtk.org/chuck_spencer/32/2854_2.png) [@Chuck\_Spencer](https://discourse.vtk.org/u/Chuck_Spencer)
#### Post date: [April 23, 2021, 7:26pm UTC](https://discourse.vtk.org/t/split-volume-by-label-values/5629/3 "2021-04-23T19:26:40Z")

</div>

Not surprisingly, this is pretty slow

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [April 25, 2021, 6:10pm UTC](https://discourse.vtk.org/t/split-volume-by-label-values/5629/4 "2021-04-25T18:10:37Z")

</div>

There should be no need for thresholding at all. You can specify multiple iso-values to MarchingCubes/ContourFilter (at least in VTK proper). You need to apply smoothing to the resulting isosurfaces to remove staircase artifacts.

There are several other possible pipelines for visualizing segmentation results. For example, you can use RGBA volume rendering or multi-volume rendering instead of creating surfaces at all. It all depends on the underlying volume (imaging modality, size, kind of structures you want to visualize), whether you want to allow quick editing, etc.

---

<div class="post-metadata">

### Author: ![Chuck\_Spencer](https://discourse.vtk.org/user_avatar/discourse.vtk.org/chuck_spencer/32/2854_2.png) [@Chuck\_Spencer](https://discourse.vtk.org/u/Chuck_Spencer)
#### Post date: [April 25, 2021, 8:25pm UTC](https://discourse.vtk.org/t/split-volume-by-label-values/5629/5 "2021-04-25T20:25:31Z")

</div>

Thanks @lassoan for these suggestions. I am working in VTK.js, so I’m restricted to what’s available there. One VTK.js limitation I’ve run up against is the lack of picking for Volumes - which is why I need to convert the volumes to surfaces.

I have tried setting to the `contourValue` parameter for the MarchingCubes to the value of the respective labels, but it seems to have no effect - the whole volume is rendered instead of just the parts of it with the correct label values.

```
    let marchingCube = vtkImageMarchingCubes.newInstance({
      contourValue: myLabelValue,
      computeNormals: true,
      mergePoints: true,
    });

```

I may not be grasping the differences between the ISO values and the label values and how they relate to each other - I don’t have much of a background w/ medical imaging data.
