Resampling Volume to Target Shape

Hello Neurostars,

My goal is to use nilearn’s resample_img function to resample all my volumes to the same shape. The function takes the arguments target_affine and target_shape. By setting the diagonal of the target_affine matrix to a voxel size of 0.5 mm (and leaving the target_shape undefined), I expected the image dimensions of the volume to double from (150 x 256 x 256) to (300 x 512 x 512). Unfortunately, the resultant dimensions turned out to be (359 x 535 x 535). This is demonstrated in the upsampling portion of this notebook .

To bring the image to the standard size I desire (300 x 512 x 512), it appears that target_shape must be defined to the desired shape to perform a subsequent crop/pad operation.

Is this logic correct and the best route to meet my goal? If possible, I would like to avoid cropping so there is no information loss. Also, can someone shed some light as to why the volume dimensions aren’t exactly doubled?


The reason that the shape is not exactly the double of the original shape is probably that there is a bit of rotation in the transformation, and as a result the transformation of the original bounding box must fit in a large axis-aligned bounding box.

To achieve your goals, you will indeed need to set target_shape.