Package: radifox-utils
Installationpipinstallradifox-utilsThis will install the utils package with it'sscipydependancy chain.To install pytorch dependencies to use these functions with pytorch Tensors:pipinstallradifox-utils[pytorch]DegradeThis is a small library to degrade (blur and downsample) a signal. Here, we
aim to properly model the forward process in signal acquisition.ResizeSampling step when resizing an imageWhen resizing a digital image with interpolation, we usually need to sample values at non-integer coordinates. The sampling step, i.e., the separation between two adjacent pixels/voxels to be sampled, then determines the digital resolution in the resulting image. When given a target digital resolution, we usually want our image resizing routines respect the corresponding sampling step. For example, when visualizing a zoomed-in medical image, if the digital resolution calculated from the image header mismatches the sampling step, we will see either a squeezed or stretched image.There are many Python libraries available to resize an image. However, functions such asscipy.ndimage.zoomandskimage.transform.rescaledo not respect the sampling step. Here we use a very simple experiment to demonstrate this.importnumpyasnpfromscipy.ndimageimportzoomsampling_step=0.7x=np.arange(6).astype(float)# Perform a linear interpolation with replication paddingy=zoom(x,1/sampling_step,order=1,mode='nearest')With Python version 3.7.6 and Scipy version 1.6.3, we will have y equal to[0, 0.625, 1.25, 1.875, 2.5, 3.125, 3.75, 4.375, 5.]Since our sampling step is chosen as 0.7, we would expect the resulting array to have approximately 0.7 different between two nearby values. However, we get 0.625 in this case. For scikit-image,importnumpyasnpfromskimage.transformimportrescalesampling_step=0.7x=np.arange(6).astype(float)# Perform a linear interpolation with replication paddingy=rescale(x,1/sampling_step,order=1,mode='edge')With Python version 3.7.6 and sciki-image version 1.18.1, we have y equal to[0, 0.5, 1.16666667, 1.83333333, 2.5, 3.16666667, 3.83333333, 4.5, 5]Here the nearby difference is 0.6666667 (ignore the first and last since they are affected by padding). It is not 0.7 either.What happens here is that since the resulting image should have an integer number of values, these functions choose to change the sampling step (or the scale, the inverse of the sampling) to accommodate that.However, if we use the PyTorch functiontorch.nn.functional.interpolate:importnumpyasnpimporttorchfromtorch.nn.functionalimportinterpolatesampling_step=0.7x=torch.arange(6).float()x=x[None,None,...]# add batch and channel dim# Perform a linear interpolation with replication paddingy=interpolate(x,scale_factor=1/sampling_step,mode='linear')With Python version 3.7.6 and PyTorch version 1.8.1, we have[0.0000, 0.5500, 1.2500, 1.9500, 2.6500, 3.3500, 4.0500, 4.7500]Here we have our 0.7 sampling step back (ignore the values that are affected by padding).The shift of the resized imageEven iftorch.nn.functional.interpolatepreserves the sampling step, the output field of view (FOV) does not align with the original image, as we can see from the above example. It is usually preferred to have the FOVs center around the same position before and after the interpolation to avoid shifting the contents of the image.Our implementationHere we provide an implementation to both preserve the sampling step and to align up the FOV.fromradifox.utils.resize.scipyimportresizesampling_step=0.7x=np.arange(6).astype(float)ya=resize(x,(sampling_step,),order=1)print(ya)# [0.0 0.4 1.1 1.8 2.5 3.2 3.9 4.6 5.0]Update the Affine MatrixSome images, such as medical images, come with position and orientation information in addition to the image
array, usually as an affine matrix. We need to update that with a new origin and scale, based on our resized
voxels. Ourupdate_affinefunction assumes a homogeneous affine matrix as a numpy array.importnumpyasnpfromradifox.utils.resize.affineimportupdate_affineoriginal_affine=np.array([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1],])new_affine=update_affine(original_affine,[1,1,4])This function will update the affine based on the new voxel size, taking into account any existing rotation,
scale, translation or shear.np.array([[1.,0.,0.,0.],[0.,1.,0.,0.],[0.,0.,4.,1.5],[0.,0.,0.,1.]])For a more complex example, we can create a different example matrix with existing values.fromtransforms3d.affinesimportcomposefromtransforms3d.eulerimporteuler2matT=np.array([14.0,3.8,-39.1])R=euler2mat(np.pi/16,-np.pi/16,np.pi/16)Z=np.array([0.8,0.8,1.0])S=np.array([0.0,0.0,0.0])original_affine=compose(T,R,Z,S)new_affine=update_affine(original_affine,[1,1,3])We can see that our affine has now adjusted not just the 2 values as before, but has adjusted all of the origin
values and the axis 2 column to reflect existing rotations and scales.new_affine-original_affine# array([[ 0. ,  0. , -0.29921,  0.14960],#        [ 0. ,  0. , -0.45734,  0.22867],#        [ 0. ,  0. ,  1.92388,  0.96194],#        [ 0. ,  0. ,  0.     ,  0.     ]])