# How to extract a volume around a voxel in a 3D image?

**URL:** <https://discourse.itk.org/t/how-to-extract-a-volume-around-a-voxel-in-a-3d-image/2768>\
**Category:** Algorithms\
**Created:** [February 27, 2020, 1:06pm UTC](https://discourse.itk.org/t/how-to-extract-a-volume-around-a-voxel-in-a-3d-image/2768 "2020-02-27T13:06:23Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![EE18D504\_SINDHURA\_C](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/b3f665/32.png) [@EE18D504\_SINDHURA\_C](https://discourse.itk.org/u/EE18D504_SINDHURA_C)\
**Post date:** [February 27, 2020, 1:06pm UTC](https://discourse.itk.org/t/how-to-extract-a-volume-around-a-voxel-in-a-3d-image/2768/1 "2020-02-27T13:06:23Z")

</div>

Suppose I have a 3D image of `250*250*250` size.  
I want to extract a volume of `25*25*25` around a particular voxel that could be defined by me.  
Is there any example that I could look into ??

---

<div class="post-metadata">

**Author:** ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)\
**Post date:** [February 27, 2020, 2:00pm UTC](https://discourse.itk.org/t/how-to-extract-a-volume-around-a-voxel-in-a-3d-image/2768/2 "2020-02-27T14:00:56Z")

</div>

In ITK you’d use the ExtractImageFilter. Here’s the documentation for it:

[https://itk.org/Doxygen/html/classitk\_1\_1ExtractImageFilter.html](https://itk.org/Doxygen/html/classitk_1_1ExtractImageFilter.html)

and here’s an example program that uses it:

[https://itk.org/Doxygen/html/SphinxExamples\_2src\_2Core\_2Common\_2CropImageBySpecifyingRegion\_2Code\_8cxx-example.html#\_a6](https://itk.org/Doxygen/html/SphinxExamples_2src_2Core_2Common_2CropImageBySpecifyingRegion_2Code_8cxx-example.html#_a6)

In SimpleITK python you can use do something spiffy like:

```
img2 = img1[cx-25:cx+25, cy-25:cy+25, cz-25:cz+25]

```

(except you’d probably want to do some bounds checking)

---

<div class="post-metadata">

**Author:** ![flaviu2](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.itk.org/u/flaviu2)\
**Post date:** [March 2, 2020, 9:01am UTC](https://discourse.itk.org/t/how-to-extract-a-volume-around-a-voxel-in-a-3d-image/2768/3 "2020-03-02T09:01:30Z")

</div>

> [@dchen](#):
>
> In SimpleITK python you can use do something spiffy like:
> 
> ```auto
> img2 = img1[cx-25:cx+25, cy-25:cy+25, cz-25:cz+25]
> 
> ```

Dave, how would you implement this in C++/SimpleITK ? I am asking this because seem more easily than ITK implementation …

---

<div class="post-metadata">

**Author:** ![Jonas](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@Jonas](https://discourse.itk.org/u/Jonas)\
**Post date:** [March 2, 2020, 11:14am UTC](https://discourse.itk.org/t/how-to-extract-a-volume-around-a-voxel-in-a-3d-image/2768/4 "2020-03-02T11:14:35Z")

</div>

Hi,

I’ve been using the CropImageFilter to do something similar.

[https://itk.org/Doxygen/html/classitk\_1\_1CropImageFilter.html](https://itk.org/Doxygen/html/classitk_1_1CropImageFilter.html)

You need to specify the opposites corners of you area.

> cropmageFilter-\>SetInput(imgVessels);  
> cropImageFilter-\>SetLowerBoundaryCropSize( lowerSize);  
> cropImageFilter-\>SetUpperBoundaryCropSize( upperSize);  
> cropImageFilter-\>Update();

---

<div class="post-metadata">

**Author:** ![flaviu2](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.itk.org/u/flaviu2)\
**Post date:** [March 2, 2020, 11:41am UTC](https://discourse.itk.org/t/how-to-extract-a-volume-around-a-voxel-in-a-3d-image/2768/5 "2020-03-02T11:41:25Z")

</div>

Jonas, thank you for your idea. In my case, I have a 3d point (taken with vtkCellPicker), and with this 3d point I want to take only a limited volume around this point …

A little detail: how can you achieved imgVessels ? Sorry for this simple questions, I am a beginner in ITK/SimpleITK.

---

<div class="post-metadata">

**Author:** ![Jonas](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@Jonas](https://discourse.itk.org/u/Jonas)\
**Post date:** [March 2, 2020, 1:33pm UTC](https://discourse.itk.org/t/how-to-extract-a-volume-around-a-voxel-in-a-3d-image/2768/6 "2020-03-02T13:33:23Z")

</div>

Sorry about imgVessels. That was my input image at the time, but replace it with your input Image.

lowerSize and upperSize are itk::Size\<3,int\> Objects.

> lowerSize[0] = PointIndexX - offsetX;  
> lowerSize[1] = PointIndexY - offsetY;  
> lowerSize[2] = PointIndexZ - offsetZ;

> upperSize[0] = PointIndexX + offsetX;  
> upperSize[1] = PointIndexY + offsetY;  
> upperSize[2] = PointIndexZ + offsetZ;

this should work as long as you check that you are not out of bond.

---

<div class="post-metadata">

**Author:** ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)\
**Post date:** [March 2, 2020, 2:03pm UTC](https://discourse.itk.org/t/how-to-extract-a-volume-around-a-voxel-in-a-3d-image/2768/7 "2020-03-02T14:03:22Z")

</div>

Unfortunately, this is a SimpleITK/Python only feature. To do extract a sub-image in C++, you have to use the CropImageFilter (as @Jonas demonstrates).

---

<div class="post-metadata">

**Author:** ![flaviu2](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.itk.org/u/flaviu2)\
**Post date:** [March 2, 2020, 2:10pm UTC](https://discourse.itk.org/t/how-to-extract-a-volume-around-a-voxel-in-a-3d-image/2768/8 "2020-03-02T14:10:00Z")

</div>

I will try your idea. If I don’t push my luck, can you tell me how to check the bound limits ? (I am trying to learn ITK/SimpleITK from this forum too).

---

<div class="post-metadata">

**Author:** ![Jonas](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@Jonas](https://discourse.itk.org/u/Jonas)\
**Post date:** [March 2, 2020, 2:16pm UTC](https://discourse.itk.org/t/how-to-extract-a-volume-around-a-voxel-in-a-3d-image/2768/9 "2020-03-02T14:16:40Z")

</div>

The best way to learn is to read the documentation and look at examples.

here is the documentation on regions.  
[https://itk.org/Doxygen/html/classitk\_1\_1ImageRegion.html](https://itk.org/Doxygen/html/classitk_1_1ImageRegion.html)

You also have a lot of code example such as:  
[https://itk.org/ITKExamples/search.html?q=region](https://itk.org/ITKExamples/search.html?q=region)

The documentation is well made in that regard. You should abuse it as much as possible.

---

<div class="post-metadata">

**Author:** ![flaviu2](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.itk.org/u/flaviu2)\
**Post date:** [March 3, 2020, 3:43pm UTC](https://discourse.itk.org/t/how-to-extract-a-volume-around-a-voxel-in-a-3d-image/2768/10 "2020-03-03T15:43:04Z")

</div>

Based on your guiding lines, I wrote:

```
	typedef float InternalPixelType;
	const unsigned int nDimension = 3;
	typedef itk::Image<InternalPixelType, nDimension> InputImageType;
	typedef itk::Image<InternalPixelType, nDimension> OutputImageType;
	typedef itk::CropImageFilter<InputImageType, OutputImageType> CropImageFilterType;
	typedef itk::Image<InternalPixelType, nDimension> InternalImageType;
	InternalImageType::Pointer itkImage = dynamic_cast <InternalImageType*>(img.GetITKBase());
	CropImageFilterType::Pointer cropFilter	= CropImageFilterType::New();
	cropFilter->SetInput(itkImage);
	// do nothing with cropFilter for now
	cropFilter->Update();

```

where **img** is **sitk::Image** type. But I got an error at **cropFilter-\>Update()**; line:

```
itkExceptionMacro(<< "Input " << requiredInputName << " is required but not set.");

```

on **ProcessObject ::VerifyPreconditions** method … obviously I’ve done something wrong … but what ?

I have to mention that I seek on [https://itk.org/ITKExamples/search.html?q=region](https://itk.org/ITKExamples/search.html?q=region) and in ITK examples to figure out what happen here, but I didn’ catch the issue … can you lead guide me a little bit ?

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [March 3, 2020, 5:02pm UTC](https://discourse.itk.org/t/how-to-extract-a-volume-around-a-voxel-in-a-3d-image/2768/11 "2020-03-03T17:02:00Z")

</div>

Hello @flaviu2,  
It looks like the dynamic cast failed ([the example you a likely based your code on](https://itk.org/SimpleITKDoxygen/html/ITKIntegration_2ITKIntegration_8cxx-example.html) shows that you need to check for this).

Are you only converting to an ITK image in order to crop the image? If yes, then you could use the [SimpleITK crop filter](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1CropImageFilter.html).

---

<div class="post-metadata">

**Author:** ![flaviu2](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.itk.org/u/flaviu2)\
**Post date:** [March 3, 2020, 7:45pm UTC](https://discourse.itk.org/t/how-to-extract-a-volume-around-a-voxel-in-a-3d-image/2768/12 "2020-03-03T19:45:44Z")

</div>

Yes you had right … the **itkImage** object has been NULL, I don’t understand why …

---

<div class="post-metadata">

**Author:** ![flaviu2](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.itk.org/u/flaviu2)\
**Post date:** [March 3, 2020, 8:04pm UTC](https://discourse.itk.org/t/how-to-extract-a-volume-around-a-voxel-in-a-3d-image/2768/13 "2020-03-03T20:04:34Z")

</div>

Now I understand the difference between SimpleITK and ITK. With SimpleITK I have:

```
	sitk::CropImageFilter crop;
	crop.SetLowerBoundaryCropSize(lower);
	crop.SetUpperBoundaryCropSize(upper);
	crop.Execute(img);

```

much more simpler 🙂

---

<div class="post-metadata">

**Author:** ![flaviu2](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.itk.org/u/flaviu2)\
**Post date:** [March 3, 2020, 8:11pm UTC](https://discourse.itk.org/t/how-to-extract-a-volume-around-a-voxel-in-a-3d-image/2768/14 "2020-03-03T20:11:46Z")

</div>

Please tell me why I got out of bounds on this filter:

```
sitk::Image img;

int cx = img.GetWidth(); // 512
int cy = img.GetHeight(); // 512
int cz = img.GetDepth(); // 44

int x = 326;
int y = 351;
int z = 26;

std::vector<unsigned int> lower;
lower.push_back(x + 2);
lower.push_back(y + 2);
lower.push_back(z + 2);
std::vector<unsigned int> upper;
upper.push_back(x - 2);
upper.push_back(y - 2);
upper.push_back(z - 2);

sitk::CropImageFilter crop;
crop.SetLowerBoundaryCropSize(lower);
crop.SetUpperBoundaryCropSize(upper);
crop.Execute(img);

```

But I got this error:

```
itkExceptionMacro("The input image's size " << input_sz << " is less than the total of the crop size!");

```

That point, _x,y,z_ seem to be within **img** bounds … or not ? Can you help me ?

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [March 3, 2020, 8:22pm UTC](https://discourse.itk.org/t/how-to-extract-a-volume-around-a-voxel-in-a-3d-image/2768/15 "2020-03-03T20:22:33Z")

</div>

The documentation for `SetLowerBondaryCropSize` says:

```auto
Set/Get the cropping sizes for the upper and lower boundaries.

```

So it is the size to reduce the volume by. For you case it would be:

`std::vector<unsigned int> crop_size={2,2,2}`

---

<div class="post-metadata">

**Author:** ![flaviu2](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.itk.org/u/flaviu2)\
**Post date:** [March 4, 2020, 11:07am UTC](https://discourse.itk.org/t/how-to-extract-a-volume-around-a-voxel-in-a-3d-image/2768/16 "2020-03-04T11:07:09Z")

</div>

Yes, it goes now. The sitk::Image is cropped:

```
	sitk::Image imgTemp(img);
	sitk::CropImageFilter crop;
	crop.SetLowerBoundaryCropSize(lower);
	crop.SetUpperBoundaryCropSize(upper);
	img = crop.Execute(imgTemp);

```

I wonder if there is some simpleitk method to crop only a small part of volume, let say a bone or a tooth …

I have tried to save the copped image into a jpg/bmp/etc image but the sitk::writer told me that “_JPEG Writer can only write 2-dimensional images_”
