siolmsstate / brain_mri

An open brain MRI dataset and baseline evaluations for tumor recurrence prediction
MIT License
10 stars 3 forks source link

Affine transformation #3

Open stiven-hidri opened 4 months ago

stiven-hidri commented 4 months ago

Hi, I'm trying to apply the affine transormation described in the paper but without success. I was hoping that you could help me out since i cannot find that part in the provided code.

Right now i'm stuck here:

mr_file_paths = [os.path.join(path_MR, f) for f in os.listdir(path_MR) if f.endswith('.dcm')]        
mr_data = [pydicom.dcmread(p) for p in mr_file_paths] #dicom readings of mri
mr_dtype = mr_data[0].pixel_array.dtype
mr = np.array([p.pixel_array for p in mr_data], dtype=mr_dtype) #numpys of each slice of the mri

rtd_file_path  = [os.path.join(path_RTD, f) for f in os.listdir(path_RTD) if f.endswith('.dcm')][0]
rtd_data = pydicom.dcmread(rtd_file_path) #rtdose dicom reading
rtd = np.float64(rtd_data.pixel_array) #rtdose data

mr_ippf, mr_ippl = np.array(mr_data[0].ImagePositionPatient), np.array(mr_data[-1].ImagePositionPatient) #first and last imagePositionPatient arrays of mri
mr_iop = np.array(mr_data[0].ImageOrientationPatient) #mri imageOrientationPatient array
mr_spacing = np.array(mr_data[0].PixelSpacing) # mri PixelSpacing

Δr, Δc = mr_spacing  #row and column spacing
F11, F21, F31 = mr_iop[3:6] #cosines
F12, F22, F32 = mr_iop[0:3] #cosines

k1, k2, k3 = (mr_ippl - mr_ippf) / (len(mr)-1)
Dx, Dy, Dz = mr_ippf

affine = np.array([
    [F11*Δr,    F12*Δc, k1, Dx  ],
    [F21*Δr,    F22*Δc, k2, Dy  ],
    [F31*Δr,    F32*Δc, k3, Dz  ],
    [0,             0,          0,   1   ],
], dtype=np.float64)

rtd = ndimage.affine_transform(rtd, affine, output_shape=mr.shape)

I used as reference the following page: https://nipy.org/nibabel/dicom/dicom_orientation.html#getting-a-3d-affine-from-a-dicom-slice-or-list-of-slices

Do you have any suggestion? Thanks in advance