Search code examples
pythonarraysnumpyscipyitk

Python ITK Rehsape error with median Filter


I have the follwoing question. I have a numpy array with size 91x40x21 full of floats and I want to use the median filter. Unfortunatly when i trying to cast it back to an array i get this error:

"cannot reshape array of size 76440 into shape (0,0,0)"

This is my code:

image_view = itk.GetImageViewFromArray(matrix.astype('float32'))
medianImage = itk.MedianImageFilter.New(image_view, Radius = 3)
matrix = itk.GetArrayViewFromImage(medianImage)

Solution

  • If you explicitely update and get output from the image, then everythink will work:

    matrix = np.random.random_integers(0,100,(10,6,8))
    image_view = itk.GetImageViewFromArray(matrix.astype('Float32'))
    medianImageFilter = itk.MedianImageFilter.New(image_view, Radius = 3)
    medianImageFilter.Update() 
    medianImage = medianImageFilter.GetOutput()
    newMatrix = itk.GetArrayViewFromImage(medianImage)