MultiScaleHessianBasedMeasureImageFilter GetScalesOutput() member function doesn't work in python?

Getting an error when I try write to file or get the numpy array associated with the GetScalesOutput() function for MultiScaleHessianBasedMeasureImageFilter in python.

        # Load itk_image from file
        ImageType = type(itk_image)
        HessianPixelType = itk.SymmetricSecondRankTensor[itk.D, DIM]
        HessianImageType = itk.Image[HessianPixelType, DIM]

        objectness_filter = itk.HessianToObjectnessMeasureImageFilter[
            HessianImageType, ImageType
        ].New()
        objectness_filter.SetBrightObject(True)
        objectness_filter.SetScaleObjectnessMeasure(True)
        objectness_filter.SetAlpha(0.5)
        objectness_filter.SetBeta(1.0)
        objectness_filter.SetGamma(5.0)

        multi_scale_filter = itk.MultiScaleHessianBasedMeasureImageFilter[
            ImageType, HessianImageType, ImageType
        ].New()
        multi_scale_filter.SetInput(itk_image)
        multi_scale_filter.SetHessianToMeasureFilter(objectness_filter)
        multi_scale_filter.SetSigmaStepMethodToLogarithmic()
        multi_scale_filter.SetSigmaMinimum(0.5)
        multi_scale_filter.SetSigmaMaximum(5.5)
        multi_scale_filter.SetNumberOfSigmaSteps(5)
        multi_scale_filter.Update()
        
        output_image = multi_scale_filter.GetOutput()
        scales_image = multi_scale_filter.GetScalesOutput()
        itk.imwrite(scales_image, "scales.nii.gz")

Results in:

...
    itk.imwrite(scales_image, "scales.nii.gz")
  File "/usr/local/lib/python3.9/site-packages/itk/support/extras.py", line 1176, in imwrite
    writer.Update()
RuntimeError: /work/ITK-source/ITK/Modules/IO/ImageBase/include/itkImageFileWriter.hxx:366:
Did not get requested region!
Requested:
ImageRegion (0x7ffc5ce5b380)
  Dimension: 3
  Index: [0, 0, 0]
  Size: [188, 217, 259]
Actual:
ImageRegion (0x7ffc5ce5b340)
  Dimension: 3
  Index: [0, 0, 0]
  Size: [0, 0, 0]

Running the same code but trying to access the numpy array does the following

itk.GetArrayFromImage(scales_image, keep_axes=True)

gives this error:

*** ValueError: PyMemoryView_FromBuffer(): info->buf must not be NULL

None of these errors occur when using the GetOutput() method.

This is ITK 5.3.0 and python 3.9

You seem to be missing multi_scale_filter.SetGenerateScalesOutput( True ).

Thank you, that was my problem. Adding that fixed the issue.

1 Like