# Segmentation fault displaying float image

**URL:** <https://discourse.itk.org/t/segmentation-fault-displaying-float-image/486>\
**Category:** Beginner Questions\
**Created:** [December 5, 2017, 4:24pm UTC](https://discourse.itk.org/t/segmentation-fault-displaying-float-image/486 "2017-12-05T16:24:04Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bonisthegreat](https://discourse.itk.org/user_avatar/discourse.itk.org/bonisthegreat/32/196_2.png) [@bonisthegreat](https://discourse.itk.org/u/bonisthegreat)\
**Post date:** [December 5, 2017, 4:24pm UTC](https://discourse.itk.org/t/segmentation-fault-displaying-float-image/486/1 "2017-12-05T16:24:04Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [December 5, 2017, 4:27pm UTC](https://discourse.itk.org/t/segmentation-fault-displaying-float-image/486/2 "2017-12-05T16:27:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bonisthegreat](https://discourse.itk.org/user_avatar/discourse.itk.org/bonisthegreat/32/196_2.png) [@bonisthegreat](https://discourse.itk.org/u/bonisthegreat)\
**Post date:** [December 5, 2017, 4:58pm UTC](https://discourse.itk.org/t/segmentation-fault-displaying-float-image/486/3 "2017-12-05T16:58:05Z")

</div>

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](https://we.tl/C1aUJ5N9d4)

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [December 5, 2017, 5:44pm UTC](https://discourse.itk.org/t/segmentation-fault-displaying-float-image/486/4 "2017-12-05T17:44:25Z")

</div>

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](https://discourse.itk.org/uploads/default/original/1X/efde91d1b6be9fef86484103ebddf1b4c4b74420.hdr) (352 Bytes)  
[test.img](https://discourse.itk.org/uploads/default/original/1X/19801eec7e04a8933aae7aaeda6d6234e2783521.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.

---

<div class="post-metadata">

**Author:** ![bonisthegreat](https://discourse.itk.org/user_avatar/discourse.itk.org/bonisthegreat/32/196_2.png) [@bonisthegreat](https://discourse.itk.org/u/bonisthegreat)\
**Post date:** [December 5, 2017, 6:53pm UTC](https://discourse.itk.org/t/segmentation-fault-displaying-float-image/486/5 "2017-12-05T18:53:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [December 6, 2017, 12:01am UTC](https://discourse.itk.org/t/segmentation-fault-displaying-float-image/486/6 "2017-12-06T00:01:06Z")

</div>

It was a bug in NIFTI Image IO. A proposed fix is [here](http://review.source.kitware.com/#/c/22864/). 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?

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [December 6, 2017, 9:55pm UTC](https://discourse.itk.org/t/segmentation-fault-displaying-float-image/486/7 "2017-12-06T21:55:03Z")

</div>

I have created a small test image which reproduces the problem and used it in a new test. The [fix](http://review.source.kitware.com/#/c/22864/) should be ready now.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [December 7, 2017, 2:58pm UTC](https://discourse.itk.org/t/segmentation-fault-displaying-float-image/486/8 "2017-12-07T14:58:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bonisthegreat](https://discourse.itk.org/user_avatar/discourse.itk.org/bonisthegreat/32/196_2.png) [@bonisthegreat](https://discourse.itk.org/u/bonisthegreat)\
**Post date:** [December 8, 2017, 11:25pm UTC](https://discourse.itk.org/t/segmentation-fault-displaying-float-image/486/9 "2017-12-08T23:25:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bonisthegreat](https://discourse.itk.org/user_avatar/discourse.itk.org/bonisthegreat/32/196_2.png) [@bonisthegreat](https://discourse.itk.org/u/bonisthegreat)\
**Post date:** [December 9, 2017, 10:41am UTC](https://discourse.itk.org/t/segmentation-fault-displaying-float-image/486/10 "2017-12-09T10:41:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [December 9, 2017, 3:32pm UTC](https://discourse.itk.org/t/segmentation-fault-displaying-float-image/486/11 "2017-12-09T15:32:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bonisthegreat](https://discourse.itk.org/user_avatar/discourse.itk.org/bonisthegreat/32/196_2.png) [@bonisthegreat](https://discourse.itk.org/u/bonisthegreat)\
**Post date:** [December 9, 2017, 3:37pm UTC](https://discourse.itk.org/t/segmentation-fault-displaying-float-image/486/12 "2017-12-09T15:37:31Z")

</div>

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