# Updating a sub region in ImageToVTKImageFilter

**URL:** https://discourse.itk.org/t/updating-a-sub-region-in-imagetovtkimagefilter/702
**Category:** Beginner Questions
**Created:** [February 23, 2018, 6:17am UTC](https://discourse.itk.org/t/updating-a-sub-region-in-imagetovtkimagefilter/702 "2018-02-23T06:17:22Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![benjaminhon](https://discourse.itk.org/user_avatar/discourse.itk.org/benjaminhon/32/83_2.png) [@benjaminhon](https://discourse.itk.org/u/benjaminhon)
#### Post date: [February 23, 2018, 6:17am UTC](https://discourse.itk.org/t/updating-a-sub-region-in-imagetovtkimagefilter/702/1 "2018-02-23T06:17:23Z")

</div>

Hi all,

I am performing some annotation functions which which modifies the ITK image -\> ImageToVTKImageFilter -\> vtkImageViewer2.

However, this is quite slow for large images because I need to call ImageToVTKImageFilter.Update() whenever then user mouse drags.

I was looking at the SetRequestedRegion() to see if I can just update the region that was drawn, however ImageToVTKImageFilter does not have SetRequestedRegion.

Is there any way I can update only the drawn region to speed things up?

Thanks

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [February 23, 2018, 2:33pm UTC](https://discourse.itk.org/t/updating-a-sub-region-in-imagetovtkimagefilter/702/2 "2018-02-23T14:33:05Z")

</div>

You might only need to call `Modified()` on the resulting VTK image.

If that does not solve your problem, you could try using [vtkImageImport](https://www.vtk.org/doc/nightly/html/classvtkImageImport.html) directly: `SetImportVoidPointer` to ITK image’s `GetBufferPointer()`, and call `Modified()` on the resulting VTK image when needed.

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [February 23, 2018, 2:53pm UTC](https://discourse.itk.org/t/updating-a-sub-region-in-imagetovtkimagefilter/702/3 "2018-02-23T14:53:41Z")

</div>

Another option, which works from Python, is to add an [itk.RegionOfInterestImageFilter](https://itk.org/Doxygen/html/classitk_1_1RegionOfInterestImageFilter.html)

> ITK image → RegionOfInterestImageFilter → ImageToVTKImageFilter → vtkImageViewer2.

or, probably best, call [`SetDisplayExtent`](https://www.vtk.org/doc/nightly/html/classvtkImageActor.html#adcad5e6d638a0e32caa7b191e8b476e8) on the `vtkImageViewer2`’s `vtkImageActor` (untested).

---

<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: [February 17, 2020, 12:39pm UTC](https://discourse.itk.org/t/updating-a-sub-region-in-imagetovtkimagefilter/702/4 "2020-02-17T12:39:48Z")

</div>

Hi Dzenan. Can you detail a little bit your idea from here ? I am trying to import data from SimpleITK to VTK. And I have tried this:

```
    sitk::ImportImageFilter importer;
....
	sitk::Image img = importer.Execute();
	img = sitk::GrayscaleErode(img, 3);
	sitk::CurvatureFlowImageFilter blurFilter;
	blurFilter.SetNumberOfIterations(5);
	blurFilter.SetTimeStep(0.125);
	img = blurFilter.Execute(img);
...
	vtkImageImport* pImageImport = vtkImageImport::New();
	pImageImport->SetImportVoidPointer(&img);
	m_pDICOMReader->SetOutput(pImageImport->GetOutput());
	m_pDICOMReader->Update();
	pImageImport->Delete();

```

but I guess is not ok, obviously because the code is not correct. How can I migrate SimpleITK image data to vtkImageData.  
Sorry for this (maybe) easy question, for me is not 🙂

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [February 17, 2020, 3:03pm UTC](https://discourse.itk.org/t/updating-a-sub-region-in-imagetovtkimagefilter/702/5 "2020-02-17T15:03:43Z")

</div>

`pImageImport->SetImportVoidPointer(&img);` this needs to be replaced by `pImageImport->SetImportVoidPointer(img.GetBufferAsVoidPointer());` or something similar. Then you need a series of `pImageImport->SetSpacing(...);` calls for spacing, size/extent, origin. Finally `pImageImport->Update();`. Why are you trying to mix in DICOM reader?

---

<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: [February 18, 2020, 7:45am UTC](https://discourse.itk.org/t/updating-a-sub-region-in-imagetovtkimagefilter/702/6 "2020-02-18T07:45:06Z")

</div>

Kindly thank you Dzenan ! I have just a little until I solve my task 🙂

I have just one more question: how to convert from _sitk::Image::GetSize_ into _vtkImageImport::SetDataExtent_, because there is no _SetSize_ inside of _vtkImageImport_.

Here is the code source:

```
	sitk::ImportImageFilter importer;
	....
	importer.SetBufferAsUInt8(in);
	.....
	sitk::Image img = importer.Execute();
	sitk::ConnectedThresholdImageFilter segmentationFilter;
	segmentationFilter.SetLower(20.0);
	segmentationFilter.SetUpper(1000);
	segmentationFilter.SetReplaceValue(255);
	img = segmentationFilter.Execute(img);
	....
	pImageImport->SetImportVoidPointer((void*)img.GetBufferAsUInt8());
	std::vector<double> spacing = img.GetSpacing();
	std::vector<double> origin = img.GetOrigin();
	std::vector<unsigned int> size = img.GetSize();
	pImageImport->SetDataSpacing(spacing[0], spacing[1], spacing[2]);
	pImageImport->SetDataOrigin(origin[0], origin[1], origin[2]);
	pImageImport->SetDataExtent(); // <-- this is the question, what should I put here ? because pImageImport doesn't have SetDataSize(), just SetDataExtent()
	pImageImport->Update();

```

Here is the documentation for vtkImageImport::SetDataExtent:

- Get/Set the extent of the data buffer. The dimensions of your data
- must be equal to (extent[1]-extent[0]+1) \* (extent[3]-extent[2]+1) \*
- (extent[5]-DataExtent[4]+1). For example, for a 2D image use
- (0,width-1, 0,height-1, 0,0).  
\*/  
vtkSetVector6Macro(DataExtent,int);  
vtkGetVector6Macro(DataExtent,int);  
void SetDataExtentToWholeExtent()  
{this-\>SetDataExtent(this-\>GetWholeExtent());}  
//@}

What should I put into _pImageImport-\>SetDataExtent_ ?

P.S. Here is the list of _sitk::Image::GetBufferAs_…

```
int8_t *GetBufferAsInt8( );
uint8_t *GetBufferAsUInt8( );
int16_t *GetBufferAsInt16( );
uint16_t *GetBufferAsUInt16( );
int32_t *GetBufferAsInt32( );
uint32_t *GetBufferAsUInt32( );
int64_t *GetBufferAsInt64( );
uint64_t *GetBufferAsUInt64( );
float *GetBufferAsFloat( );
double *GetBufferAsDouble( );
```

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [February 18, 2020, 4:23pm UTC](https://discourse.itk.org/t/updating-a-sub-region-in-imagetovtkimagefilter/702/7 "2020-02-18T16:23:19Z")

</div>

Computing the extent should be something like this:

```auto
auto index=img->GetLargestPossibleRegion().GetIndex(); // this might be different in SimpleITK
for (unsigned d=0; d<Dimension; d++)
{
  extent[d]=index[d];
  extent[d+1]=index[d]+size[d]-1;
}

```

---

<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: [February 18, 2020, 4:32pm UTC](https://discourse.itk.org/t/updating-a-sub-region-in-imagetovtkimagefilter/702/8 "2020-02-18T16:32:14Z")

</div>

For SimpleITK the starting index is always 0, then you need to call [GetSize](https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1Image.html#a257e30186da62fd5765ac4ef630fc86a) for the image size.

---

<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: [February 18, 2020, 7:39pm UTC](https://discourse.itk.org/t/updating-a-sub-region-in-imagetovtkimagefilter/702/9 "2020-02-18T19:39:26Z")

</div>

> [@dzenanz](#):
>
> auto index=img-\>GetLargestPossibleRegion().GetIndex();

Is there any similar way to find this index with SimpleITK only ? I have struggled days to insert SimpleITK in my project, I don’t want the same experience with ITK 🙂

Moreover, my image object with what I have worked is **sitk::Image**.

---

<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: [February 18, 2020, 7:44pm UTC](https://discourse.itk.org/t/updating-a-sub-region-in-imagetovtkimagefilter/702/10 "2020-02-18T19:44:37Z")

</div>

> [@flaviu2](#):
>
> Is there any similar way to find this index with SimpleITK only ?

No, it’s always zero.

---

<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: [February 18, 2020, 8:22pm UTC](https://discourse.itk.org/t/updating-a-sub-region-in-imagetovtkimagefilter/702/11 "2020-02-18T20:22:45Z")

</div>

I am about to finish my task. But I guess I didn’t done something right:

```
    sitk::Image img = importer.Execute();
	spacing = img.GetSpacing();
	origin = img.GetOrigin();
	size = img.GetSize();
	pImageImport->SetDataSpacing(spacing[0], spacing[1], spacing[2]);
	pImageImport->SetDataOrigin(origin[0], origin[1], origin[2]);
	int extent[6];
	int index[3];
	index[0] = 0; index[1] = 0; index[2] = 0;
	for (unsigned int d = 0; d < img.GetDimension(); ++d)
	{
		extent[d] = index[d];
		extent[d + 1] = index[d] + size[d] - 1;
	}
	pImageImport->SetDataExtent(extent);
	pImageImport->SetImportVoidPointer((void*)img.GetBufferAsUInt8());
	pImageImport->Update(); <-- Warning: In D:\VTK\VTK-8.2.0\IO\Image\vtkImageImport.cxx, line 507
								vtkImageImport (000001AA5E16E6D0): 
								There is a distinction between the whole extent and the buffered
								extent of an imported image. Use SetWholeExtent to set the extent
								of the entire image. Use SetDataExtent to set the extent of the
								portion of the image that is in the buffer set with
								SetImportVoidPointer. Both should be called even if the extents are
								the same.

```

Obviously, I have done extent in the right way …

---

<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: [February 18, 2020, 9:00pm UTC](https://discourse.itk.org/t/updating-a-sub-region-in-imagetovtkimagefilter/702/12 "2020-02-18T21:00:47Z")

</div>

That is really a great warning message coming from VTK! It tells you exactly what needs to be done.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [February 18, 2020, 9:43pm UTC](https://discourse.itk.org/t/updating-a-sub-region-in-imagetovtkimagefilter/702/13 "2020-02-18T21:43:36Z")

</div>

You also need `pImageImport->SetWholeExtent(extent);`

---

<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: [February 19, 2020, 10:54am UTC](https://discourse.itk.org/t/updating-a-sub-region-in-imagetovtkimagefilter/702/14 "2020-02-19T10:54:35Z")

</div>

Here is transfer from **vtkImageData** to **sitk** :

```
int* nDim = m_pDICOMReader->GetOutput()->GetDimensions();
uint8_t* in = new uint8_t[nDim[0] * nDim[1] * nDim[2]];
double* dSpacing = m_pDICOMReader->GetOutput()->GetSpacing();
double* dSpacing = m_pDICOMReader->GetOutput()->GetSpacing();
std::vector<double> spacing;
spacing.push_back(dSpacing[0]);
spacing.push_back(dSpacing[1]);
spacing.push_back(dSpacing[2]);
importer.SetSpacing(spacing);
double* dOrigin = m_pDICOMReader->GetOutput()->GetOrigin();
std::vector<double> origin;
origin.push_back(dOrigin[0]);
origin.push_back(dOrigin[1]);
origin.push_back(dOrigin[2]);
importer.SetOrigin(origin);
std::vector<unsigned int> size;
size.push_back((unsigned int)nDim[0]);
size.push_back((unsigned int)nDim[1]);
size.push_back((unsigned int)nDim[2]);
importer.SetSize(size);
importer.SetBufferAsUInt8(in);
TRACE(">>>>%d|%d|%d\n", nDim[0], nDim[1], nDim[2]);

```

TRACE result: \>\>\>\>512|512|44 (my CT data)

Here is the transfer from **sitk** to **vtkImageImport** :

```
    sitk::Image img = importer.Execute();
	spacing = img.GetSpacing();
	origin = img.GetOrigin();
	size = img.GetSize();
	pImageImport->SetDataSpacing(spacing[0], spacing[1], spacing[2]);
	pImageImport->SetDataOrigin(origin[0], origin[1], origin[2]);
	int extent[6];
	int index[3];
	index[0] = 0; index[1] = 0; index[2] = 0;
	for (unsigned int d = 0; d < img.GetDimension(); ++d)
	{
		extent[d] = index[d];
		extent[d + 1] = index[d] + size[d] - 1;
	}
	pImageImport->SetWholeExtent(extent);
	pImageImport->SetDataExtentToWholeExtent();
	pImageImport->SetNumberOfScalarComponents(1);
	pImageImport->SetImportVoidPointer((void*)img.GetBufferAsUInt8());
	pImageImport->Update();
	nDim = pImageImport->GetOutput()->GetDimensions();
	TRACE(">>>>%d|%d|%d\n", nDim[0], nDim[1], nDim[2]);

```

TRACE result: \>\>\>\>1|44|1

Where I lost those two dimensions ? And is not the right order …

---

<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: [February 20, 2020, 2:21pm UTC](https://discourse.itk.org/t/updating-a-sub-region-in-imagetovtkimagefilter/702/16 "2020-02-20T14:21:00Z")

</div>

I think I succeeded … and I post here the code for the case who someone will have the same issue:

```
	spacing = img.GetSpacing();
	img = sitk::Cast(img, sitk::PixelIDValueEnum::sitkUInt8);
	pImageImport->SetImportVoidPointer((void*)img.GetBufferAsUInt8());
	pImageImport->SetDataScalarTypeToUnsignedChar();
	pImageImport->SetDataExtent(0, (int)size[0] - 1, 0, (int)size[1] - 1, 0, size[2] - 1);
	pImageImport->SetWholeExtent(0, (int)size[0] - 1, 0, (int)size[1] - 1, 0, size[2] - 1);
	pImageImport->SetDataSpacing(spacing[0], spacing[1], spacing[2]);
	pImageImport->Update();
	nDim = pImageImport->GetOutput()->GetDimensions();
	TRACE(">>>>%d|%d|%d\n", nDim[0], nDim[1], nDim[2]);

```

Result: \>\>\>\>512|512|44, just the input size. Please correct me if something is wrong in my code.

Thank you !

---

<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: [February 20, 2020, 2:33pm UTC](https://discourse.itk.org/t/updating-a-sub-region-in-imagetovtkimagefilter/702/17 "2020-02-20T14:33:37Z")

</div>

> [@dzenanz](#):
>
> Computing the extent should be something like this:

@dzenanz It looks like you had a bug in the indexing of extent in your proposed code. Should have been something like this:

```auto
for (unsigned d=0; d<Dimension; d++)
{
  extent[2*d]=0;
  extent[2*d+1]=size[d]-1;
}

```

I also removed the always 0 index;

@flaviu2 Thank you for sharing the solution. I didn’t see the problem with the above code before you posted your solution. I like the way you directly set the extents:

```auto
pImageImport->SetDataExtent(0, (int)size[0] - 1, 0, (int)size[1] - 1, 0, size[2] - 1);

```

👍  
Compared to your other code you forgot to set the Origin and the NumberOfComponents.

---

<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: [February 20, 2020, 3:29pm UTC](https://discourse.itk.org/t/updating-a-sub-region-in-imagetovtkimagefilter/702/18 "2020-02-20T15:29:45Z")

</div>

I’am glad to be useful here !!
