Page MenuHomePhabricator

Level Window contrast effect in SAM tool
Closed, ResolvedPublic

Description

Add Render view Level Window contrast change effect in images for SAM tool.

Related Objects

Event Timeline

a178n triaged this task as Normal priority.Jul 10 2023, 5:18 PM
a178n created this task.

I tried to implement this feature using the below snippet in DoUpdatePreview method.
The image get written successfully however there is no level window effect in the output image. Looks normal. What's going on?

mitk::LevelWindow levelWindow;
this->GetToolManager()->GetReferenceData(0)->GetLevelWindow(levelWindow);
MITK_INFO << "levelWindow.GetLowerWindowBound(): " << levelWindow.GetLowerWindowBound();
MITK_INFO << "levelWindow.GetUpperWindowBound(): " << levelWindow.GetUpperWindowBound();
MITK_INFO << "levelWindow.GetWindow(): " << levelWindow.GetWindow();
MITK_INFO << "levelWindow.GetLevel(): " << levelWindow.GetLevel();

Image *input = const_cast<Image *>(inputAtTimeStep);
vtkImageData *imageData = input->GetVtkImageData();
vtkSmartPointer<vtkLookupTable> lookupTable = vtkSmartPointer<vtkLookupTable>::New();
lookupTable->SetRange(levelWindow.GetLowerWindowBound(), levelWindow.GetUpperWindowBound());
lookupTable->SetSaturationRange(0.0, 0.0);
lookupTable->SetValueRange(0.0, 1.0);
lookupTable->SetHueRange(0.0, 0.0);
lookupTable->SetRampToLinear();

auto levelWindowFilter = vtkSmartPointer<vtkMitkLevelWindowFilter>::New();
levelWindowFilter->SetLookupTable(lookupTable);
levelWindowFilter->SetInputData(imageData);
levelWindowFilter->SetMinOpacity(0.0);
levelWindowFilter->SetMaxOpacity(1.0);
levelWindowFilter->Update();

vtkImageData *vtkOut = levelWindowFilter->GetOutput();
MITK_INFO << "vtkOut->GetDataDimension(): " << vtkOut->GetDataDimension();

auto mitkImage = mitk::Image::New();
mitkImage->Initialize(vtkOut, 1, -1, -1, 1);
mitkImage->SetVolume(vtkOut->GetScalarPointer());

std::string imagePath = "C://Users//a178n.AD//Desktop//test.nrrd";
IOUtil::Save(inputAtTimeStep, imagePath);

Hi,
I finally could apply levelwindow Filter on image successfully for SAM tool.
But that seems to opened up another can of worms 😅 (?)
The vtkMitkLevelWindowFilter gives me 2D 4 channel image. But the channels are duplicate. I verified using numpy/python. Presumably. I can just take out one channel slice and that should be it.
But I observe somethings not correct:

  1. I convert vtk image to mitk image using the following syntax:
auto mitkImage = mitk::Image::New();
mitkImage->Initialize(vtkOut, 1, -1, -1, -1);
mitkImage->SetVolume(vtkOut->GetScalarPointer());
mitkImage->Update();

Problem is: Despite explicitly stating image channel dim as 1, the volume is actually 4D 3D (4 channel RGBA) and not a 2D slice. No exceptions thrown while setting the volume.
mitkImage->GetDimension() gives 2.

  1. Probably, more serious:

The levelwindow filter applied images are unsigned char images. So my int image with value range (-1028 to 1027) becomes new image with 0-255.
This datatype change seems to happen at MITK-level since levelwindow filtered is used while rendering.
Happens here: https://github.com/MITK/MITK/blob/a754b053db1214637403308f9fd210a4eef6df13/Modules/Core/src/Rendering/vtkMitkLevelWindowFilter.cpp#L284

You seem to mix a few things here.

  1. Why do you think the image is 4-d? Look at the implementation of mitk::Image::Initialize(vtkImageData*, ...) and you will see that internally the dimensions array always has at least 4 entries but if the actual image has less dimensions the extra dimensions are set to 1 which is just fine. So for example a 32x32 2-d image has the dimensions array [32, 32, 1, 1], since it represents a single slice of size 32x32 in a single time step.
  2. Why do you use the vtkMitkLevelWindowFilter? If you just want to apply a level window without* all the extra bells and whistles use vtkImageMapToColors for example: https://gitlab.kitware.com/vtk/vtk/-/blob/v9.2.0/Examples/ImageProcessing/Cxx/ImageSlicing.cxx#L211

edit: with -> without

  1. I am seeing the array in python after writing it out IOUtil::Save(mitkImage, imagePath);. That's 4 channel image. ie. 32x32x4.
  2. I use vtkMitkLevelWindowFilter because you recommended(?) and also its used by MITK for its levelwindow processing and at few other occasions. So this data type change is widely affecting MITK. But whether its compensated somewhere else or not is not clear for me.

But for SAM tool- Thanks for the tip, I will try out vtkImageMapToColors and see.

Anyway after discussing with Ralf, the problem at hand - implementing levelwindow effect of input images to SAM, is solved using itk::IntensityWindowingImageFilter.