# which function can get the slice size extract by vtkImageReslice?

**URL:** https://discourse.vtk.org/t/which-function-can-get-the-slice-size-extract-by-vtkimagereslice/1584
**Category:** Support
**Created:** [August 15, 2019, 5:31am UTC](https://discourse.vtk.org/t/which-function-can-get-the-slice-size-extract-by-vtkimagereslice/1584 "2019-08-15T05:31:27Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![pingdan](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pingdan](https://discourse.vtk.org/u/pingdan)
#### Post date: [August 15, 2019, 5:31am UTC](https://discourse.vtk.org/t/which-function-can-get-the-slice-size-extract-by-vtkimagereslice/1584/1 "2019-08-15T05:31:27Z")

</div>

```
I want to get a slice by codes as below, and I can get the image.but I want to know how to get the 

```

slice image’s size, its width and height.  
static double obliqueX[3] = { cos(angle), 0, -sin(angle) };  
static double obliqueY[3] = { 0, 1, 0 };  
static double obliqueZ[3] = { sin(angle), 0, cos(angle) };

```
vtkSmartPointer<vtkMatrix4x4> resliceAxes =
	vtkSmartPointer<vtkMatrix4x4>::New();

vtkSmartPointer<vtkImageReslice> reslice =
	vtkSmartPointer<vtkImageReslice>::New();

reslice->SetInputData(m_ImageData);
reslice->SetOutputDimensionality(2);
reslice->SetResliceAxesDirectionCosines(obliqueX, obliqueY, obliqueZ);
reslice->SetResliceAxesOrigin(center);
reslice->SetInterpolationModeToLinear();

vtkSmartPointer<vtkLookupTable> colorTable =
	vtkSmartPointer<vtkLookupTable>::New();
colorTable->SetRange(0, 1000);
colorTable->SetValueRange(0.0, 1.0);
colorTable->SetSaturationRange(0.0, 0.0);
colorTable->SetRampToLinear();
colorTable->Build();

vtkSmartPointer<vtkImageMapToColors> colorMap =
	vtkSmartPointer<vtkImageMapToColors>::New();
colorMap->SetLookupTable(colorTable);
colorMap->SetInputConnection(reslice->GetOutputPort());
colorMap->Update();

```

 ![a](https://discourse.vtk.org/uploads/default/original/1X/e15a4ef638cb229f7387228d07bee52f54425fc2.jpeg)

---

<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: [August 15, 2019, 6:22am UTC](https://discourse.vtk.org/t/which-function-can-get-the-slice-size-extract-by-vtkimagereslice/1584/2 "2019-08-15T06:22:07Z")

</div>

Please edit the first line in your post, its formatting is incorrect because it was indented too far.

There is no function in VTK to compute the oblique slice size, but it is possible to do it with VTK’s geometry pipeline:

Step 1: use vtkOutlineFilter to create a geometrical box the size of the input image volume.

```
vtkSmartPointer<vtkOutlineFilter> outline =
    vtkSmartPointer<vtkOutlineFilter>::New();
outline->SetInputData(m_ImageData);
outline->Update();

```

Step 2: Put the box into the reslice output coordinate system:

```
// create transform that is inverse of ResliceAxes
vtkSmartPointer<vtkTransform> transform =
    vtkSmartPointer<vtkTransform>::New();
transform->Identity();
transform->Concatenate(reslice->GetResliceAxes());
transform->Inverse();

// apply the transformation to the box
vtkSmartPointer<vtkTransformPolyDataFilter> transpoly =
    vtkSmartPointer<vtkTransformPolyDataFilter>::New();
transpoly->SetInputData(outline->GetOutput());
transpoly->SetTransform(transform);
transpoly->Update();

```

Step 3: slice the transformed box with a plane.

```
// the plane is just a Z-plane in the new coordinate system
vtkSmartPointer<vtkPlane> plane = vtkSmartPointer<Plane>::New();
plane->SetOrigin(0.0, 0.0, 0.0);
plane->SetNormal(0.0, 0.0, 1.0);

vtkSmartPointer<vtkCutter> cutter = vtkSmartPointer<vtkCutter>::New();
cutter->SetInputData(transpoly->GetOutput());
cutter->SetCutFunction(plane);
cutter->Update();

```

Step 4: get the bounds for the slice, and compute the size:

```
double bounds[6];
cutter->GetOutput()->GetBounds(bounds);
double size[2] = { bounds[1] - bounds[0], bounds[3] - bounds[2] };

```

I have not tested this code, since I just typed it off the top of my head. It might contain mistakes. But you can try it to see if it works.

If you put the output of the vtkCutter into a vtkActor, you will be able to visualize the outline of the slice.

---

<div class="post-metadata">

### Author: ![pingdan](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pingdan](https://discourse.vtk.org/u/pingdan)
#### Post date: [August 15, 2019, 6:54am UTC](https://discourse.vtk.org/t/which-function-can-get-the-slice-size-extract-by-vtkimagereslice/1584/3 "2019-08-15T06:54:39Z")

</div>

thank you.  
in fact , I can compute the width and height according to math. but I am not sure if it is correct by computing. as we known, the calculating value is a double value, and the image size must be a int value.

---

<div class="post-metadata">

### Author: ![pingdan](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pingdan](https://discourse.vtk.org/u/pingdan)
#### Post date: [August 15, 2019, 7:03am UTC](https://discourse.vtk.org/t/which-function-can-get-the-slice-size-extract-by-vtkimagereslice/1584/4 "2019-08-15T07:03:35Z")

</div>

the slice extract will be save to other format image, I must write the image information. so I need to know the right image size.

---

<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: [August 15, 2019, 7:04am UTC](https://discourse.vtk.org/t/which-function-can-get-the-slice-size-extract-by-vtkimagereslice/1584/5 "2019-08-15T07:04:09Z")

</div>

To get the integer value, divide by the pixel spacing.

---

<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: [August 15, 2019, 7:14am UTC](https://discourse.vtk.org/t/which-function-can-get-the-slice-size-extract-by-vtkimagereslice/1584/6 "2019-08-15T07:14:07Z")

</div>

Or maybe you only need something like this:

```
int dims[3];
reslice->Update();
reslice->GetOutput()->GetDimensions(dims);
std::cout << dims[0] << ", " << dims[1] << ", " << dims[2] << std::endl;

```

I’m not sure what you are asking for. Do you want to know how vtkImageReslice computes the size of its output?

Or do you want to compute the size yourself, and use `reslice->SetOutputExtent(,,,,,)` to set the size?

---

<div class="post-metadata">

### Author: ![pingdan](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pingdan](https://discourse.vtk.org/u/pingdan)
#### Post date: [August 15, 2019, 8:08am UTC](https://discourse.vtk.org/t/which-function-can-get-the-slice-size-extract-by-vtkimagereslice/1584/7 "2019-08-15T08:08:49Z")

</div>

> [@dgobbi](#):
>
> reslice-\>GetOutput()-\>GetDimensions(dims);

yes, this just what I need.but I confused, why the size is smaller than the source size.from math, the slice is longer than the source size according to 512/cos(angle).

---

<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: [August 15, 2019, 8:32am UTC](https://discourse.vtk.org/t/which-function-can-get-the-slice-size-extract-by-vtkimagereslice/1584/8 "2019-08-15T08:32:36Z")

</div>

By default, vtkImageReslice uses the size of the input as the size of the output. So if the input is 512x512x512, the output slice is 512x512. That’s just how it is.

You can use `reslice->AutoCropOutputOn()` to make it larger, but sometimes it will overestimate.

If you need a specific size, then you must call `reslice->SetOutputExtent()` to set the size (and probably `reslice->SetOutputOrigin()` to set the corner of the slice).
