Extract the 3 componenets x,y,z of gradiant filter

Hi;

I have applied two filters on 3d vtkImagedata (binary volume) in order to compute the gradient at each (i,j,k) voxel like:

vtkImageGaussianSmooth *gaussianFilter = vtkImageGaussianSmooth::New();
gaussianFilter->SetInputData(ContourImg);
gaussianFilter->SetDimensionality(3);
gaussianFilter->SetStandardDeviations (2, 2, 2);
gaussianFilter->Update();

vtkImageGradient *gradientFilter = vtkImageGradient::New();
gradientFilter->SetInputData(gaussianFilter->GetOutput());
gradientFilter->SetDimensionality(3);
gradientFilter->HandleBoundariesOn();
gradientFilter->Update();

Now I want recover the x ,y and z component of each voxel. I have see some example on net but i am confused about the right solution so here is my solution:

vtkDataArray* grad = gradImg->GetPointData()->GetScalars();
int coords[3]= {i, j, k};
vtkIdType tupleId = gradImg->ComputePointId(coords);

  g[0]= grad->GetComponent(tupleId, 0);
  g[1]= grad->GetComponent(tupleId, 1);
  g[2]= grad->GetComponent(tupleId, 2);

But in other examples, they use the “vtkImageExtractComponents” to extract x and y components of the gradient like this:

vtkSmartPointer extractXFilter =
vtkSmartPointer::New();
extractXFilter->SetComponents(0);
extractXFilter->SetInputConnection(gradientFilter->GetOutputPort());

double xRange[2];

extractXFilter->Update();
extractXFilter->GetOutput()->GetPointData()->GetScalars()->GetRange(xRange);

I Dont understand how can I use this filter to extract the gradiant components at each i,j,k voxel

thank’s in advance

There are many ways to access voxel data. Simple methods, such as GetScalarComponentAsDouble are extremely slow, raw buffer access, such as GetScalarPointer only work well for known memory layout (Slicer uses only the default memory layout, so it should be fine), fast and flexible methods use multithreading and array dispatching, which are extremely complicated.

In your case, I would recommend using GetScalarPointer.

1 Like