The problem of itk.ImageFileReader

Hello,

I have encountered some problems recently. I want to rebulid the volume image from a “.tre” file to show the refine image. I use itk to complete this work, and it works perfect in the conda env. However, when I run it in my Slicer extension, it shows:

TemplateImageType: <class 'itk.itkImagePython.itkImageF3'>
terminate called after throwing an instance of 'H5::DataSpaceIException'
error: [/mnt2/homes/xingyul/xinyul/Slicer/bin/SlicerApp-real] exit abnormally - Report the problem.

I tried to debug, found that this line of code caused the program to crash:

    TemplateImageType = itk.Image[itk.F, 3]
    print('TemplateImageType:',TemplateImageType)
    TemplateImageReaderType = itk.ImageFileReader[TemplateImageType]

I think the function ‘itk.ImageFileReader’ caused the conflict, how to fix it?

Thanks

The entire code is as follows:

import itk
from itk import TubeTK as ttk
def treTomask(rootpath, ctpath):
    ctpath=''
    print('start treTomask')
    PixelType = itk.F
    Dimension = 3
    ImageType = itk.Image[PixelType, Dimension]
        
    # # Read tre file
    TubeFileReaderType = itk.SpatialObjectReader[Dimension]
        
    tubeFileReader = TubeFileReaderType.New()
    tubeFileReader.SetFileName(rootpath)
    tubeFileReader.Update()

    print('tubeFileReader complete')

    tubes = tubeFileReader.GetGroup()

    print('tubes = tubeFileReader.GetGroup()')

    # Read template image
    TemplateImageType = itk.Image[itk.F, 3]
    print('TemplateImageType:',TemplateImageType)
    TemplateImageReaderType = itk.ImageFileReader[TemplateImageType]
    print('TemplateImageReaderType')
    print(ctpath)
        
    templateImageReader = TemplateImageReaderType.New()
    templateImageReader.SetFileName(ctpath)
    templateImageReader.Update()
    print('templateImageReader')

    templateImage = templateImageReader.GetOutput()
    TubesToImageFilterType = ttk.ConvertTubesToImage[TemplateImageType]
    tubesToImageFilter = TubesToImageFilterType.New()
    tubesToImageFilter.SetUseRadius(True)
    tubesToImageFilter.SetTemplateImage(templateImageReader.GetOutput())
    tubesToImageFilter.SetInput(tubes)
    tubesToImageFilter.Update()
    outputImage = tubesToImageFilter.GetOutput()

    print('tubesToImageFilter  complete')

    TTKImageMathType = ttk.ImageMath[ImageType,ImageType]
    imMath = TTKImageMathType.New(Input = outputImage)
    img = imMath.GetOutput()
    # os.remove(rootpath)
    print('Tre convert is DONE')
    return img

By the way, I try subprocess, like this:

subprocess.run('/mnt2/homes/xingyul/anaconda3/envs/labelsys/bin/python /mnt2/homes/xingyul/xinyul/Slicer/vessel_connect/infer_tools_tree/result/test.py', shell=True)

but it seems doesn’t work :

Traceback (most recent call last):
  File "/mnt2/homes/xingyul/xinyul/Slicer/vessel_connect/infer_tools_tree/result/test.py", line 1, in <module>
    from TreToImg import treTomask
  File "/mnt2/homes/xingyul/xinyul/Slicer/vessel_connect/infer_tools_tree/result/TreToImg.py", line 1, in <module>
    import itk
  File "/mnt2/homes/xingyul/xinyul/Slicer/lib/Python/lib/python3.6/site-packages/itk/__init__.py", line 28, in <module>
    from itk.support.extras import *
  File "/mnt2/homes/xingyul/xinyul/Slicer/lib/Python/lib/python3.6/site-packages/itk/support/extras.py", line 23, in <module>
    import numpy as np
  File "/mnt2/homes/xingyul/xinyul/Slicer/lib/Python/lib/python3.6/site-packages/numpy/__init__.py", line 140, in <module>
    from . import core
  File "/mnt2/homes/xingyul/xinyul/Slicer/lib/Python/lib/python3.6/site-packages/numpy/core/__init__.py", line 22, in <module>
    from . import multiarray
  File "/mnt2/homes/xingyul/xinyul/Slicer/lib/Python/lib/python3.6/site-packages/numpy/core/multiarray.py", line 12, in <module>

This thread should help: Slicer crashes when creating h5 file - #3 by szymswiat

1 Like

Thanks for reply!I have successfully fixed it by subprocess, but I’d like to try your suggestion.