Segmentation fault displaying float image

Hello there! I get a segmentation fault when trying to display or apply any filter to an ikt::Image with float pixel types. I can use this same code (swapping the pixel types) to open an image with unsigned char pixel type and it works just fine. Both images I mentioned (the float pixel ones and the unsigned char one) are of type Nifti.

I also have opened the image with ImageIOBase to check the pixel type and its indeed float.

Here is the code:

#include “itkImage.h”
#include “itkImageFileReader.h”
#include “itkExtractImageFilter.h”
#include “itkImageToVTKImageFilter.h”
#include “vtkImageViewer.h”
#include “vtkImageMapper3D.h”
#include “vtkRenderWindowInteractor.h”
#include “vtkSmartPointer.h”
#include “vtkImageActor.h”
#include “vtkInteractorStyleImage.h”
#include “vtkRenderer.h”
#include “itkRGBPixel.h”
#include <unistd.h>
#include “QuickView.h”
#include
#include “itkBinaryThresholdImageFilter.h”
#include “itkMedianImageFilter.h”

typedef float FloatPixelType;
typedef itk::Image<FloatPixelType, 3> Input3DFloatImageType;
typedef itk::Image<FloatPixelType, 2> FloatImageType;
typedef itk::ImageFileReader< Input3DFloatImageType> Float3DReaderType;
typedef itk::ImageToVTKImageFilter FloatConnectorType;
typedef itk::ExtractImageFilter< Input3DFloatImageType, FloatImageType > FloatFilterType;

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

const char * grayMatterFile = “…/…/I3T/I3TGM.img”;
Float3DReaderType::Pointer floatReader = Float3DReaderType::New();
floatReader->SetFileName( grayMatterFile );

FloatFilterType::Pointer floatFilter = FloatFilterType::New();
floatFilter->SetDirectionCollapseToSubmatrix();
floatFilter->InPlaceOn();
floatReader->UpdateOutputInformation();
Input3DFloatImageType::RegionType floatRegion = floatReader->GetOutput()->GetLargestPossibleRegion();

Input3DFloatImageType::SizeType floatSize = floatRegion.GetSize();
floatSize[1] = 0;
Input3DFloatImageType::IndexType floatIndexStart = floatRegion.GetIndex();

for(int i = 0; floatRegion.GetSize()[1]; i++){

    floatIndexStart[1] = i;
    Input3DFloatImageType::RegionType floatDesiredRegion;

    floatDesiredRegion.SetSize( floatSize);
    floatDesiredRegion.SetIndex( floatIndexStart);

    // Float image
    floatFilter->SetInput( floatReader->GetOutput() );
    floatFilter->SetExtractionRegion( floatDesiredRegion ); 
    floatFilter->Update();
    ShowImage<FloatImageType>(floatFilter->GetOutput());


}

}

template
void ShowImage(typename TImageType::Pointer image){

typedef itk::ImageToVTKImageFilter<TImageType> ThisConnectorType;
typename ThisConnectorType::Pointer connector = ThisConnectorType::New();
connector->SetInput(image);

try{
    connector->Update();
 } catch( itk::ExceptionObject & error ) {
    std::cerr << "Error: " << error << std::endl;
    return;
 }


vtkSmartPointer<vtkImageActor> actor =  vtkSmartPointer<vtkImageActor>::New();

actor->GetMapper()->SetInputData(connector->GetOutput());

vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
renderer->ResetCamera();

vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);

vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style = vtkSmartPointer<vtkInteractorStyleImage>::New();

renderWindowInteractor->SetInteractorStyle(style);

renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();

renderWindowInteractor->Start();

}

Thank you very much in advanced!

Can you share I3TGM.img? If you have more than one image, you could share the smallest one which triggers the crash.

1 Like

I tried with three images which they all trigger such segmentation fault. Here a link of on of them (wouldnt fit in this fileupload):
https://we.tl/C1aUJ5N9d4

Here is a slightly reduced code:

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkExtractImageFilter.h"
#include "itkImageToVTKImageFilter.h"
#include "vtkImageViewer.h"
#include "vtkImageMapper3D.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"
#include "vtkImageActor.h"
#include "vtkInteractorStyleImage.h"
#include "vtkRenderer.h"
#include "itkRGBPixel.h"
//#include <unistd.h>
#include "QuickView.h"
//#include <armadillo>
#include "itkBinaryThresholdImageFilter.h"
#include "itkMedianImageFilter.h"


template<typename TImageType>
void ShowImage(typename TImageType::Pointer image) {

    typedef itk::ImageToVTKImageFilter<TImageType> ThisConnectorType;
    typename ThisConnectorType::Pointer connector = ThisConnectorType::New();
    connector->SetInput(image);

    try {
        connector->Update();
    }
    catch (itk::ExceptionObject & error) {
        std::cerr << "Error: " << error << std::endl;
        return;
    }


    vtkSmartPointer<vtkImageActor> actor = vtkSmartPointer<vtkImageActor>::New();

    actor->GetMapper()->SetInputData(connector->GetOutput());

    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(actor);
    renderer->ResetCamera();

    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    vtkSmartPointer<vtkInteractorStyleImage> style = vtkSmartPointer<vtkInteractorStyleImage>::New();

    renderWindowInteractor->SetInteractorStyle(style);

    renderWindowInteractor->SetRenderWindow(renderWindow);
    renderWindowInteractor->Initialize();

    renderWindowInteractor->Start();
}

typedef float FloatPixelType;
typedef itk::Image<FloatPixelType, 3> Input3DFloatImageType;
typedef itk::Image<FloatPixelType, 2> FloatImageType;
typedef itk::ImageFileReader< Input3DFloatImageType> Float3DReaderType;
typedef itk::ImageToVTKImageFilter<FloatImageType> FloatConnectorType;
typedef itk::ExtractImageFilter< Input3DFloatImageType, FloatImageType > FloatFilterType;

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

    const char * grayMatterFile = "I3TGM.img";
    Float3DReaderType::Pointer floatReader = Float3DReaderType::New();
    floatReader->SetFileName(grayMatterFile);
    //floatReader->Update(); //uncomment to prevent crash

    FloatFilterType::Pointer floatFilter = FloatFilterType::New();
    floatFilter->SetDirectionCollapseToSubmatrix();
    floatFilter->InPlaceOn();
    floatReader->UpdateOutputInformation();
    Input3DFloatImageType::RegionType floatRegion = floatReader->GetOutput()->GetLargestPossibleRegion();

    Input3DFloatImageType::SizeType floatSize = floatRegion.GetSize();
    floatSize[1] = 0;
    Input3DFloatImageType::IndexType floatIndexStart = floatRegion.GetIndex();

    floatIndexStart[1] = floatRegion.GetSize()[1] / 2;
    Input3DFloatImageType::RegionType floatDesiredRegion;

    floatDesiredRegion.SetSize(floatSize);
    floatDesiredRegion.SetIndex(floatIndexStart);

    // Float image
    floatFilter->SetInput(floatReader->GetOutput());
    floatFilter->SetExtractionRegion(floatDesiredRegion);
    floatFilter->Update();
    ShowImage<FloatImageType>(floatFilter->GetOutput());
    return 0;
}

Uncommenting the update line avoids the crash with your image (with a side-effect of image being all-black). But even with the current code it works for this image:
test.hdr (352 Bytes)
test.img (1 MB)

The crash is occurring in c:\Libs\ITK-4.12.0\Modules\IO\NIFTI\src\itkNiftiImageIO.cxx, line 470, function CastCopy, for i==87680. But m_OnDiskComponentType is UCHAR, and that’s why the cast is being done.

I will need to investigate more.

1 Like

I understand. Let me know if you find anything out. If you need anything do not hesitate in contacting me. Thank you again!

It was a bug in NIFTI Image IO. A proposed fix is here. Further reduced example is below.

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkExtractImageFilter.h"

typedef float FloatPixelType;
typedef itk::Image<FloatPixelType, 3> Input3DFloatImageType;
typedef itk::Image<FloatPixelType, 2> FloatSliceType;
typedef itk::ImageFileReader< Input3DFloatImageType> Float3DReaderType;
typedef itk::ExtractImageFilter< Input3DFloatImageType, FloatSliceType > FloatFilterType;

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

    const char * grayMatterFile = "I3TGM.img";
    Float3DReaderType::Pointer floatReader = Float3DReaderType::New();
    floatReader->SetFileName(grayMatterFile);

    FloatFilterType::Pointer floatFilter = FloatFilterType::New();
    floatFilter->SetDirectionCollapseToSubmatrix();
    floatFilter->InPlaceOn();
    floatReader->UpdateOutputInformation();
    Input3DFloatImageType::RegionType floatRegion = floatReader->GetOutput()->GetLargestPossibleRegion();

    Input3DFloatImageType::SizeType floatSize = floatRegion.GetSize();
    floatSize[1] = 0;
    Input3DFloatImageType::IndexType floatIndexStart = floatRegion.GetIndex();
    floatIndexStart[1] = floatRegion.GetSize()[1] / 2;

    Input3DFloatImageType::RegionType floatDesiredRegion;
    floatDesiredRegion.SetSize(floatSize);
    floatDesiredRegion.SetIndex(floatIndexStart);

    floatFilter->SetInput(floatReader->GetOutput());
    floatFilter->SetExtractionRegion(floatDesiredRegion);
    floatFilter->Update();

    typedef itk::ImageFileWriter<FloatSliceType> WriterType;
    WriterType::Pointer writer = WriterType::New();
    writer->SetInput(floatFilter->GetOutput());
    writer->SetFileName("I3TGM-slice.img"); //image correctly written
    writer->Update();
    return 0;
}

Can you crop this image, or in some other way generate a small NIFTI/Analyze image with m_OnDiskComponentType of UCHAR which could be used for testing?

I have created a small test image which reproduces the problem and used it in a new test. The fix should be ready now.

1 Like

That fix has been merged to master, and will be part of ITK 4.13 release.

Sorry for the late response. Thank you so much for everything Dzenanz! I will work from the github repo till then!

Hello! I have spotted a another error with floating pixel type images. Once the float type image is opened correctly I can now work with its slices. However when trying to access to a pixel from the 3D image I also get a segmentation fault. So something like the following will cause a segmentation fault:

typedef float FloatPixelType;
typedef itk::Image<FloatPixelType,  3> Input3DFloatImageType;
Input3DFloatImageType::IndexType testIndex;
testIndex[0] = 1;
testIndex[1] = 1;
testIndex[2] = 1;
int pixelValue = CSFImg->GetPixel(testIndex);
std::cout << "Pixel: "<< pixelValue << std::endl;

However I can access correctly to the 2D pixels of every slice.

After further testing I am getting the same error for different pixel types as well (unsigned char).

Is the crash still present if you invoke floatReader->Update(); before int pixelValue = CSFImg->GetPixel(testIndex);?

To access a pixel, it has to be loaded into memory. And your pipeline only request one slice to be loaded at a time. If you try to access pixels from that slice, it should succeed.

You are right! my bad. It works fine now. Thank you again for all the help!