Extraction the vector components using python

Operating system:Linux
Slicer version:4.11.0
Hi 3D Slicer experts and all
I have a BSplin Transform which contains displacement fields. I want to extract its vector components. for this purpose I converted it to a volume node using Transforms module.[ my reference volume is a CT image with image dimensions of 512 *512 *173]. Therefore, the result was a volume node (renamed to Voluememasked) with image dimensions 512 *512 *173.
for extraction I used the following commands in Slicer python interactor :

import scipy.io as sio
nodeName= ‘Volumemasked’
voxelArray = array(nodeName)
Traceback (most recent call last):
File “”, line 1, in
File “/home/aseman/Downloads/Slicer-4.11.0-2020-02-19-linux-amd64/bin/Python/slicer/util.py”, line 935, in array
return arrayFromVolume(node)
File “/home/aseman/Downloads/Slicer-4.11.0-2020-02-19-linux-amd64/bin/Python/slicer/util.py”, line 971, in arrayFromVolume
narray = vtk.util.numpy_support.vtk_to_numpy(vimage.GetPointData().GetScalars()).reshape(nshape)
ValueError: cannot reshape array of size 136052736 into shape (173,512,512)

how can I reshape it? can you help me?
Thanks a lot

this is a numpy stuf
136052736 = 3 * 173 * 512 * 512
therefore you have 3 components in each voxel, redefine nshape like
nshape = [3, 73, 512, 512]
then your reshape will work, to extract the x, y, z components do:
x, y, z = narray[0], narray[1], narray[2]
if you want the vectors
vec = zip(x, y, z)

1 Like

Thanks for reporting this, it seems that Slicer’s array helper function cannot get vector field from scalar volume. We should be able to fix it within a few days. In the meantime, you can change arrayFromVolume function in your util.py file as @Alex_Vergara suggested above.

I have updated arrayFromVolume to work with any number of scalar components, regardless of using a scalar or vector volume. I’ve also updated Transforms module to save displacement vectors if a vector volume is selected as output, and save displacement magnitude values if a scalar volume is selected as output. Changes are available in latest Slicer Preview Release.