[SOLVED] Undefined reference to QuickView even with ITKVtkGlue added in target_link_libraries

Hi,
I am trying to open and display an image with QuickView, however the linker is unable to find QuickView::AddImage.

CMakeFiles/viewer.dir/viewer.cc.o: In function `main':
viewer.cc:(.text+0x12d): undefined reference to `void QuickView::AddImage<itk::Image<short, 3u> >(itk::Image<short, 3u>*, bool, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: error: ld returned 1 exit status

In the cmake file I have:

cmake_minimum_required(VERSION 3.11)
project(itkTest)

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(itkTest itkTest.cc)
target_link_libraries(itkTest ${ITK_LIBRARIES})

add_executable(vtkTest vtkTest.cc)
target_link_libraries(vtkTest ${VTK_LIBRARIES})

add_executable(viewer viewer.cc)
target_link_libraries(viewer ${Glue} ${ITK_LIBRARIES} ${VTK_LIBRARIES})

message(STATUS "${ITK_LIBRARIES}")

I have tried ITK, opening an image, and VTK, displaying a cylinder (VTKExamples/Cxx/Rendering/CylinderRenderingProperties), independently and both compile and run fine.

In ITK_LIBRARIES is included the ITKVtkGlue module (I have compiled the lastest ITK with the Module_ITKVtkGlue option ON) and according to this is there where QuickView should be.

I am working on Archlinux with ITK 5.0.0 installed from git (with make install) and VTK 8.1.0 from the repositories. Cmake 3.11.4.

The code:
#include <QuickView.h>
#include <itkImageFileReader.h>

int main(int argc, char* argv[])
{
  const std::string inputFilepath("../resources/training_001/mr_PD/training_001_mr_PD.mhd");

  static constexpr unsigned int ImageDimensions = 3;
  using PixelType = short;

  using ImageType = itk::Image<PixelType, ImageDimensions>;
  using ReaderType = itk::ImageFileReader<ImageType>;
    
  ReaderType::Pointer imageReader = ReaderType::New();
  imageReader->SetFileName(inputFilepath);
  imageReader->Update();

  QuickView viewer;
  viewer.AddImage(imageReader->GetOutput());
  viewer.Visualize();

  return 0;
}

Thanks!

PD: The else part in the cmake file is no longer necessary because ITKVtkGlue is (from now on) included in ITK, right?

EDIT: Maybe if I install vtk from github also? I would like to avoid that, because I prefer to pacman to admin my packages. Maybe a cmake variable about VTK, which ITKVtkGlue needs, is not defined correctly?

2 Likes

Hi @federico, you are good with VTK 8 from pacman, you just need to build ITKVtkGlue, as you did.

Exactly, however a better approach would be to use only the components of ITK that your are using:
find_package(ITK REQUIRED COMPONENTS ITKImageIO ITKVtkGlue)

Try without finding VTK:

cmake_minimum_required(VERSION 3.11)
project(itkTest)

find_package(ITK REQUIRED COMPONENTS ITKImageIO ITKVtkGlue)
include(${ITK_USE_FILE})

add_executable(viewer viewer.cc)
target_link_libraries(viewer ${ITK_LIBRARIES})

message(STATUS "${ITK_LIBRARIES}")

Another thing to try for debug purposes is to use the ITK directly from the build folder, not the one you installed in the system.

3 Likes

Thanks @phcerdan for your fast answer.

I figure out the problem, (apparently) QuickView does not support 3D images. If I change the dimensions and image filepath to a 2D image, then everything works perfectly.

After little more research, I found this which includes a 3d visualizer. I don’t really understand it, however is a start.

PD: For completeness, and because I didn’t know how to specify a custom directory:

I tried both approaches with a final CMakeLists.txt:
cmake_minimum_required(VERSION 3.11)
project(itkCode)

    message(STATUS "CMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}")

    find_package(ITK REQUIRED COMPONENTS ITKImageIO ITKVtkGlue)
    message(STATUS "ITK_CONFIG=${ITK_CONFIG}")
    include(${ITK_USE_FILE})
    message(STATUS "ITK_USE_FILE: ${ITK_USE_FILE}")

    add_executable(viewer viewer.cc)
    target_link_libraries(viewer ${ITK_LIBRARIES})

    message(STATUS "${ITK_LIBRARIES}")

and cmake -DCMAKE_PREFIX_PATH="/home/federico/ITK-withglue" .. (where ITKConfig.cmake is) (checked with make VERBOSE=1).

1 Like

Good spotting! You may use ViewImage for 3D images, an example here. ViewImage.h can be found in the ITKVtkGlue module as well.

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

@federico I still get the same error after enabling ITKVtkGlue flag and adding it to cmakeList.

@phcerdan Thanks for useful comments here,
I have ground truth files in *mhd and *raw files for each patient and the label files has the values between 0-4. However, after running the following code, it is showing a black window:
itk::ViewImage<ImageType>::View(reader->GetOutput());

What could be the reason?

  • Can we read mhd/raw files by using itk::Image, or I should use different data type in ITK?

  • I have used thetypedef short PixelType; data type and constexpr unsigned int Dimension=3;, should I change the PixelType here? I am a beginner and a little bit confused. ImageType is defined differently in the codes that I am referring to:

typedef itk::Image<unsigned char, Dimension> ImageType;

or

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

Hi @Sara_Caffe,
For itk::ViewImage use the same ImageType you used for reading the image (defined for your reader). You don’t have to cast it to float or any other pixel type for the visualization.

This particular viewer wasn’t designed for label images, so maybe you are seeing it black because the label has the value 1, which is too close to 0 in the float range you are using. If you click or hover the mouse in your black image you should see a value in the corner. Check if the values are different.

It should work straight away if the PixelType you use for the reader is appropriate for your data.

Hope it helps.

2 Likes

Hi again,

I could view the label files (*.mhd) by using itk::ViewImage, however, it is graylevel images, is there any way to see different values with colors?

Thanks in advance

You could use LabelToRGB filter just before display step.

1 Like

Thank you very much