Crop volume outside the boundary of an NIFTI image

I am trying to crop a VOI of an NIFTI image. In some cases, part of the VOI is outside the image and I would like to keep the VOI size unchanged. And set the pixel value of the part outside the image to 0. 3D slicer can do exactly this, but does anyone know how to write it in vtk? I have used vtkExtractVOI, but the VOI size will be restricted within the boundary of the image. Thank you!

3D Slicer is open-source software with permissive license: you can have a look at any of its features and copy everything without any restrictions. Slicer is mostly VTK based, so it should serve as a good example how the feature can be implemented using VTK. I cannot provide any more specific pointers, because “VOI” has many meanings.

I would just add that while 3D Slicer can do a lot, it is actually very small. 3D Slicer core is about 10x smaller than VTK+ITK. It is even smaller than the DCMTK DICOM toolkit. Therefore, if you are building medical imaging related software and you are using VTK then you could save lots of time by using 3D Slicer classes (which are VTK classes wrapped and extended for medical applications) than just the general-purpose VTK classes.

Hi Andras, thanks for the reply. The VOI refers to the volume below. I am trying to only use few of VTK classes instead of using codes in Slicer.