Display 3D image using ITK and VTK

Hi,
I am using simple code which I found in ITK Example.
I need to display nifti image, but all I got is black box. And when I hover the mouse over the window, the color changes from black to white, and I can’t see the image.

I think I am doing something wrong, because the code is very trivial. Any help is much appreciated. Thank you!

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "QuickView.h"
int main(int argc, char *argv[])
{
  typedef itk::Image<unsigned char, 2>  ImageType;
  typedef itk::ImageFileReader<ImageType> ReaderType;
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName("OT.nii");
  reader->Update();
  QuickView viewer;
  viewer.AddImage(reader->GetOutput());
  viewer.Visualize(true);
}

Because you declare your image as 2D, only the first slice is read. And if it only contains zero-values (black) pixels, you can’t see anything else when you visualize it.

@mabou This patch by @phcerdan may be of interest:

http://review.source.kitware.com/#/c/23387/

itkViewImage has been recently pushed to master!

@mabou give it a try if you use ITK from master.
If not, you can still try it copying the files itkViewImage.h and itkViewImage.hxx from here
to your include directory.

You can see an example of usage here, but basically would be:

// Change QuickView.h for itkViewImage.h
#include "itkViewImage.h"
...
reader->Update();
itk::ViewImage<ImageType>::View(reader->GetOutput());

And let us know! And as @dzenanz said, if your image is 3D, change the dimension of ImageType.

3 Likes

Thanks all for your help.
I have copied itkViewImage.h and itkViewImage.hxx and used the code of runViewImage.cxx and I think it works better now, because when moving the mouse cursor, I can see the coordinates changes, but the third coordinate is always zero, which means it is always the first slice (which is black of course).
Now my question: how I can move between slices? It should be done from the code or by moving the cursor?

Thanks for testing it @mabou.

You don’t need to change the code. From the top of my head (not in front of computer right now) try to press the right click button with the cursor in the slice you want to move, then move the mouse. You might need to first reorient the camera a little to do this in the Z slice. To orient the camera do the same (hold right-click) but with the cursor outside the image. Easier to play with it than to explain! :smiley:

Okay, I could play with the camera orientation as you said (hold right-click outside the cursor). But, still I only see black frame, and the coordinates changed from (0,0,0) to (230,230,0), which means the Z slice is always zero!

Great! Now you are only missing moving the Z slice to see other values. Hold right-click with the cursor over the image, and move the mouse.

1 Like

By the way, did you change the dimension to 3 in your code? If the dimension is two, there is only one slice with z=0, so nothing to move.

1 Like

For 3D images you should see something like:

screen_viewImage3D

For 2D images:

screen_viewImage2D

3 Likes

Thank you a lot! It works perfectly now (although I haven’t change something from my last reply, but it happens :slight_smile: )

4 Likes

Beautiful! :smiley:

1 Like

Thank you for sharing your knowledge

I have images that I have already normalized the values between [0,1] and saved them in nii.gz files
I created the itk::Image with the followings:

typedef float PixelType;
constexpr unsigned int Dimension=3;
using ImageType=itk::Image<PixelType ,Dimension>;

When I am moving the cursor, I can see the values of voxels in the image is changing, but it shows a black box. What could be the reason.

Thanks

The image values are probably being cast to unsigned char, which has a range 0-255. So all your voxels get converted to 0 (black) or 1 (less deep black). To have non-black visualization, try rescaling your image to pixel type unsigned char using RescaleIntensity filter.

1 Like

Hi Pablo,
I am new to ITK/VTK. I tried exactly the same code and CMakeList. It compiles fine. But I get seg fault whenever I ran the code. I have included my code below. I really want to get it working. Any help is much appreciated. Thank you so much!
Chang

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkViewImage.h"
#include <iostream>

int main(int argc, char *argv[]) {

using ImageType = itk::Image<unsigned char, 3>;
using ReaderType = itk::ImageFileReader<ImageType>;

ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName("/home/chang2/c2dev/Data/ZZAILabUCSD_HN01/ct3dhn.nii");
reader->Update();

typedef itk::RescaleIntensityImageFilter< ImageType, ImageType > RescaleFilterType;
RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
rescaleFilter->SetInput(reader->GetOutput());
rescaleFilter->SetOutputMinimum(0);
rescaleFilter->SetOutputMaximum(255);

itk::ViewImage<ImageType>::View(rescaleFilter->GetOutput());


return EXIT_SUCCESS;
}
cmake_minimum_required(VERSION 3.10)

project(HelloITK)

set(ITK_DIR "/home/chang2/c2dev/ITK-Build")
set(VTK_DIR "/home/chang2/c2dev/VTK-Build")
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})

if (ITKVtkGlue_LOADED)
  find_package(VTK REQUIRED)
  include(${VTK_USE_FILE})
else()
  find_package(ItkVtkGlue REQUIRED)
  include(${ItkVtkGlue_USE_FILE})
  set(Glue ItkVtkGlue)
endif()

# Add executable called "HelloITK" that is built from the source file "HelloITKWorld.cxx".
# Any number of source files can be listed here.  
add_executable(HelloITK HelloITK.cxx)

# Link the executable "HelloITK" to the libraries. 
target_link_libraries(HelloITK ${Glue} ${VTK_LIBRARIES} ${ITK_LIBRARIES})

Hi @Chang2

My first guess is that you are missing the Update step for the rescaleFilter. Add it here:

rescaleFilter->SetOutputMaximum(255);
rescaleFilter->Update();

itk::ViewImage::View(rescaleFilter->GetOutput());
2 Likes