My code here is trying to read in an image as an itk image, turn it into a vtk image, flip it across the y axis, and show it. However, the display only shows black. (pixel values of 0). Why could this be? Here is the code:
#include <vtkSmartPointer.h>
#include <vtkImageFlip.h>
#include <vtkImageViewer2.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <itkImage.h>
#include <itkImageFileReader.h>
#include <itkImageToVTKImageFilter.h>
#include
int main() {
// Define the image type
typedef itk::Image<unsigned int, 3> ImageType;
// Read the ITK image
typedef itk::ImageFileReader<ImageType> ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName("C:\\Users\\satya\\Documents\\CLionProjects\\TreeTrack\\805338_TOF_B_exp.nii"); // Update the path to your image file
try {
reader->Update();
} catch (itk::ExceptionObject & error) {
std::cerr << "Error reading the image: " << error << std::endl;
return EXIT_FAILURE;
}
// Convert ITK image to VTK image
typedef itk::ImageToVTKImageFilter<ImageType> ITKToVTKFilterType;
ITKToVTKFilterType::Pointer itkToVtkFilter = ITKToVTKFilterType::New();
itkToVtkFilter->SetInput(reader->GetOutput());
try {
itkToVtkFilter->Update();
} catch (itk::ExceptionObject & error) {
std::cerr << "Error converting ITK image to VTK image: " << error << std::endl;
return EXIT_FAILURE;
}
// Flip the image along the y-axis
vtkSmartPointer<vtkImageFlip> imageFlip = vtkSmartPointer<vtkImageFlip>::New();
imageFlip->SetInputData(itkToVtkFilter->GetOutput());
imageFlip->SetFilteredAxis(1); // Flip along the y-axis
imageFlip->SetFlipAboutOrigin(true);
imageFlip->Update();
// Visualize the flipped image
vtkSmartPointer<vtkImageViewer2> viewer = vtkSmartPointer<vtkImageViewer2>::New();
viewer->SetInputData(imageFlip->GetOutput());
viewer->Render();
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
viewer->GetRenderWindow()->SetInteractor(renderWindowInteractor);
viewer->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
Additionally, the whole thing works when I manually convert it into vtk instead of using the filter using this code:
#include <vtkSmartPointer.h>
#include <vtkImageFlip.h>
#include <vtkImageViewer2.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <itkImageRegionConstIterator.h>
#include <itkImage.h>
#include <itkImageFileReader.h>
#include <itkImageToVTKImageFilter.h>
#include
int main() {
// Define the image type
typedef itk::Image<unsigned int, 3> ImageType;
// Read the ITK image
typedef itk::ImageFileReader<ImageType> ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName("C:\\Users\\satya\\Documents\\CLionProjects\\TreeTrack\\805338_TOF_B_exp.nii"); // Update the path to your image file
try {
reader->Update();
} catch (itk::ExceptionObject & error) {
std::cerr << "Error reading the image: " << error << std::endl;
return EXIT_FAILURE;
}
// Create an ITK iterator for the image
typedef itk::ImageRegionConstIterator<ImageType> IteratorType;
IteratorType it(reader->GetOutput(), reader->GetOutput()->GetLargestPossibleRegion());
// Manual conversion from ITK image to VTK image
vtkSmartPointer<vtkImageData> vtkImage = vtkSmartPointer<vtkImageData>::New();
// Set dimensions and spacing
vtkImage->SetDimensions(reader->GetOutput()->GetLargestPossibleRegion().GetSize()[0],
reader->GetOutput()->GetLargestPossibleRegion().GetSize()[1],
reader->GetOutput()->GetLargestPossibleRegion().GetSize()[2]);
// Allocate memory
vtkImage->AllocateScalars(VTK_UNSIGNED_INT, 1);
// Copy pixel values from ITK image to VTK image
for (it.GoToBegin(); !it.IsAtEnd(); ++it) {
itk::Index<3> itkIndex = it.GetIndex();
double value = it.Get(); // Get pixel value from ITK image
//std::cout << "value: " << value;
vtkImage->SetScalarComponentFromDouble(itkIndex[0], itkIndex[1], itkIndex[2], 0, value);
//std::cout << "vtkvalue: " << vtkImage->GetScalarComponentAsFloat(itkIndex[0], itkIndex[1], itkIndex[2], 0) << std::endl;
}
// Flip the image along the y-axis
vtkSmartPointer<vtkImageFlip> imageFlip = vtkSmartPointer<vtkImageFlip>::New();
imageFlip->SetInputData(vtkImage);
imageFlip->SetFilteredAxis(1); // Flip along the y-axis
imageFlip->SetFlipAboutOrigin(true);
imageFlip->Update();
// Visualize the flipped image
vtkSmartPointer<vtkImageViewer2> viewer = vtkSmartPointer<vtkImageViewer2>::New();
viewer->SetInputData(imageFlip->GetOutput());
viewer->Render();
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
viewer->GetRenderWindow()->SetInteractor(renderWindowInteractor);
viewer->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}