which function can get the slice size extract by vtkImageReslice?

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();

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.

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.

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.

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

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?

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).

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).

2 Likes