# how to clip vtkImageData with the specified mask?

**URL:** https://discourse.vtk.org/t/how-to-clip-vtkimagedata-with-the-specified-mask/8352
**Category:** Support
**Created:** [April 22, 2022, 2:57pm UTC](https://discourse.vtk.org/t/how-to-clip-vtkimagedata-with-the-specified-mask/8352 "2022-04-22T14:57:29Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [April 22, 2022, 2:57pm UTC](https://discourse.vtk.org/t/how-to-clip-vtkimagedata-with-the-specified-mask/8352/1 "2022-04-22T14:57:29Z")

</div>

In my project, I have segmented the heart, which means that I have a mask. The mask means the pixel inside heart is one, while the pixel outside heart is zero.

Now, I want to display only this heart using `vtkFixedPointVolumeRayCastMapper` without other tissue.

How can I display only this heart with the mask information?

I find `vtkClipVolume` may be helpful for my requirement. But it accept `vtkImplicitFunction` rather than a mask.

How can I clip `vtkImageData` with the specified mask? Any suggestion is appreciated~~~

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [April 23, 2022, 11:55am UTC](https://discourse.vtk.org/t/how-to-clip-vtkimagedata-with-the-specified-mask/8352/2 "2022-04-23T11:55:18Z")

</div>

I find `vtkClipVolume` will output a `vtkUnstructedGrid`. Is it possible to directly convert a `vtkImageData` to `vtkUnstructedGrid`?

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [April 23, 2022, 1:25pm UTC](https://discourse.vtk.org/t/how-to-clip-vtkimagedata-with-the-specified-mask/8352/3 "2022-04-23T13:25:43Z")

</div>

The `vtkFixedPointVolumeRayCastMapper` only works with `vtkImageData`, so `vtkUnstructedGrid` is not useful for this situation.

One simple way to apply a mask is by multiplying with `vtkImageMathematics`. This might require changing the mask data type to the image data type, with `vtkImageCast`.

Or, another way to apply a mask is `vtkImageStencil`:

```python
imageToStencil = vtk.vtkImageToImageStencil()
imageToStencil.SetInputData(maskImage)

stencilFilter = vtk.vtkImageStencil()
stencilFilter.SetStencilConnection(imageToStencil.GetOutputPort())
stencilFilter.SetInputData(heartImage)
# set "background" to a value that will be transparent for volume rendering,
# for example 0 for MRI, or -1000 for CT
stencilFilter.SetBackgroundValue(0)

```

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [April 24, 2022, 1:50am UTC](https://discourse.vtk.org/t/how-to-clip-vtkimagedata-with-the-specified-mask/8352/4 "2022-04-24T01:50:23Z")

</div>

@dgobbi Thank you for the kindly reply.

---

<div class="post-metadata">

### Author: ![phong](https://discourse.vtk.org/user_avatar/discourse.vtk.org/phong/32/6887_2.png) [@phong](https://discourse.vtk.org/u/phong)
#### Post date: [May 29, 2023, 10:03am UTC](https://discourse.vtk.org/t/how-to-clip-vtkimagedata-with-the-specified-mask/8352/5 "2023-05-29T10:03:23Z")

</div>

Hi Zhang,  
I guess this question related to this topic created by you : [How to crop 3D image from a 2D free-hand ROI? - #5 by Linfeng\_Song](https://discourse.vtk.org/t/how-to-crop-3d-image-from-a-2d-free-hand-roi/2468/5) . Have you manged to implement the free-form cut function ? I am implementing this function by just only using VTK code. Somehow it works with data with slicing (1, 1, 1), but for volume with different slicing the cut position is always not correct. Can you please help to share your algorithm of implementing free-form cut ? Thank in advance. BTW, is there any opportunity that I can know your contact and have a conversation about this topic.

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [May 30, 2023, 1:40am UTC](https://discourse.vtk.org/t/how-to-clip-vtkimagedata-with-the-specified-mask/8352/6 "2023-05-30T01:40:36Z")

</div>

> [@phong](#):
>
> Have you manged to implement the free-form cut function ? I am implementing this function by just only using VTK code.

I have implemented an algorithm. First, I can obtain the 2D contour, then expanding it to 3D surface. Then, I turn the 3D surface into to mask.

> [@phong](#):
>
> BTW, is there any opportunity that I can know your contact and have a conversation about this topic.

I am a Chinese, I have QQ/Wechat. Or you can seed me message in this forum.
