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.

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

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.

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