itk.GetArrayFromImage fail after source region size change.

I got an error after doing the following, it seems that after changing the source, itk.GetArrayFromImage no longer works, however .GetOutput() still works.

reader = itk.ImageSeriesReader[ItkTypes.IF3].New()
reader.SetFileNames(files)
reader.Update()

itk.GetArrayFromImage(reader.GetOutput())

reader.SetFileNames(newFiles)
reader.UpdateLargestPossibleRegion()

# Some Filter Down the Line
itk.GetArrayFromImage(someFilter.GetOutput()) # This Fails

This is the error I get:

File "/usr/local/Cellar/insighttoolkit/4.12.1/lib/python3.6/site-packages/itk/Configuration/../itkPyBufferPython.py", line 3120, in GetArrayViewFromImage
ndarrview  = numpy.asarray(memview).view(dtype = numpydatatype).reshape(shape).view(numpy.ndarray)
ValueError: cannot reshape array of size 262144 into shape (23,512,512)

Anyone can help?

hmm looks like I managed to solve this by doing reader.Modified after setting FileNames. Am I doing this the right way?

hmm it seems that calling UpdateLargestPossibleRegion() at the end of the pipeline doesn’t propagate to all filters up. Do I need to call UpdateLargestPossibleRegion() in every single filter I’m my pipe? It seems like it only works if it do.

Hi @benjaminhon,

Does this issue still occur with the latest version of ITK, 4.13?

Hi matt, I’m currently on 4.12.1 installed using home-brew.

when I call UpdateLargestPossibleRegion() at the end of the pipeline, it seems that I can get itk.GetArrayFromImage from the end, but when I try to get some outputs from the middle of the pipeline, they give me that error.

I just tried it on 4.13, seems like I still get the same error

Yes, calling UpdateLargestPossibleRegion() is necessary to ensure that the image passed into GetArrayFromImage() has been populated appropriately. I will look into calling this automatically to avoid the need to call it manually.

Hi matt, I was further debugging and found out that it was an intermediate ExtractSlice filter that was changing all my RequestedRegion to 512 by 512 by 1. Such that when I call itk.GetArrayFromImage, I have to call UpdateLargestPossibleRegion() again.

My assumption was that ExtractSlice which I used as a split off filter off my main pipeline would not affect the upstream but it did.

I think I understand more now, will update if I find out anything again :slight_smile:

2 Likes

:+1: @benjaminhon yes, you got it.

To help other folks from being caught by this gotcha, this proposed patch will call UpdateLargestPossibleRegion before converting the image to a NumPy array.

1 Like