Hello,
I was trying to use the vtkImageViewer2 class to display the slice of functional MRI, but it doesn’t display the image texture same as in 3D Slicer.
if (fileName.endsWith("nii"))
{
m_pvtkNIIReader->SetFileName(fileName.toLocal8Bit().constData());
m_pvtkNIIReader->Update();
m_pvtkImageViewerSeg->SetInputData(m_pvtkNIIReader->GetOutput());
m_pvtkImageViewerSeg->SetRenderWindow(ui.qvtkWidget_1->GetRenderWindow());
m_pvtkImageViewerSeg->SetSliceOrientationToXY();
m_pvtkImageViewerSeg->SetSlice((int)m_pvtkImageViewerSeg->GetSliceMax() * 0.5);
m_pvtkInteractorStyleSeg->SetImageViewer(m_pvtkImageViewerSeg);
m_pvtkImageViewerSeg->SetupInteractor(m_pvtkRenWinInteractorSeg);
m_pvtkImageViewerSeg->GetRenderer()->SetBackground(0.0, 0.0, 0.0);
m_pvtkImageViewerSeg->GetRenderer()->ResetCamera();
m_pvtkImageViewerSeg->Render();
}
The left image shows the situation where the vtkNIFTIImageReader object of vtk is opened and displayed using vtkImageViewer2; The image on the right shows the same NII file opened and displayed using 3D Slicer.
I also tried to display it with vtkImageReslice and vtkLookupTable, the code and result are below, still couldn’t show the texture correctly.
int main() {
vtkNew<vtkRenderer> renderer;
renderer->SetBackground(0.1804, 0.5451, 0.3412);
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetSize(1000, 1000);
renderWindow->AddRenderer(renderer);
vtkNew<vtkInteractorStyleTrackballCamera> style;
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
vtkNew <vtkNIFTIReader> reader;
reader->SetFileName("snffM00587_0016.img");
reader->Update();
int extent[6];
double spacing[3];
double origin[3];
double center[3];
reader->GetOutput()->GetExtent(extent);
reader->GetOutput()->GetSpacing(spacing);
reader->GetOutput()->GetOrigin(origin);
center[0] = origin[0] + spacing[0] * 0.5 * (extent[0] + extent[1]);
center[1] = origin[1] + spacing[1] * 0.5 * (extent[2] + extent[3]);
center[2] = origin[2] + spacing[2] * 0.5 * (extent[4] + extent[5]);
double axialElements[16] = {
1, 0, 0, center[0],
0, 1, 0, center[1],
0, 0, 1, center[2],
0, 0, 0, 1 };
auto resliceAxesZ = vtkSmartPointer<vtkMatrix4x4>::New();
resliceAxesZ->DeepCopy(axialElements);
auto ImageResliceZ = vtkSmartPointer<vtkImageReslice>::New();
ImageResliceZ->SetInputConnection(reader->GetOutputPort());
ImageResliceZ->SetOutputDimensionality(2);
ImageResliceZ->SetResliceAxes(resliceAxesZ);
ImageResliceZ->SetInterpolationModeToLinear();
auto lookupTableZ = vtkSmartPointer<vtkLookupTable>::New();
lookupTableZ->SetRange(0, 500);
lookupTableZ->SetValueRange(0.0, 1.0);
lookupTableZ->SetSaturationRange(0.0, 0.0);
lookupTableZ->SetRampToLinear();
lookupTableZ->Build();
auto mapToColorsZ = vtkSmartPointer<vtkImageMapToColors>::New();
mapToColorsZ->SetLookupTable(lookupTableZ);
mapToColorsZ->SetInputConnection(ImageResliceZ->GetOutputPort());
mapToColorsZ->Update();
auto imageActorZ = vtkSmartPointer<vtkImageActor>::New();
imageActorZ->GetMapper()->SetInputConnection(mapToColorsZ->GetOutputPort());
renderer->AddActor(imageActorZ);
renderWindow->Render();
renderWindowInteractor->Start();
}
Could someone help me to figure out what I need to do to display the fMRI texture properly?
@dgobbi