Package: imvis
imvisInteractive visualization of 3D medical images in pythonInstallationThe package can be installed from PyPI using pip:pipinstallimvisFeatures3D image visualizationimagesc3sis a function that allows to visualize 3D images in a 2D slice-by-slice fashion. It is based on thematplotliblibrary and allows to interactively scroll through the slices of a 3D image. It also allows to change the color map and the windowing of the image.importimvisasivimportpydicomds=pydicom.dcmread('path/to/dicom/file')img=ds.pixel_arrayiv.imagesc3s(img)In cases where scrolling is not possible (e.g. in a Jupyter notebook), the alternative versionimagesc3slidercan be used. It allows to scroll through the slices of a 3D image using a slider.iv.imagesc3slider(img)When using Jupyter notebook, the matplotlib backend can be changed totkorqtto enable scrolling. This can be done using the following magic command:%matplotlibtkiv.imagesc3slider(img)MIP with rotation anglesmipzallows the user to obtain a maximum intensity projection (MIP) of a 3D image along the z-axis. The user can also specify the rotation angles of the MIP.importSimpleITKassitkimportnumpyasnpimg=sitk.ReadImage("/path/to/nifti")imarray=sitk.GetArrayFromImage(img)mip_array=np.zeros((36,imarray.shape[0],imarray.shape[1]))foriinrange(0,360,10):mip_array[int(i/10),:,:]=iv.mipz(imarray,i)iv.imagesc3s(mip_array,[0,10])NIFTI image resampling in reference to another imageresample_nifti_toallows to resample a NIFTI image in reference to another image. This is useful when you want to resample a NIFTI image to the same resolution as a DICOM image.defresample_nifti_to(nifti_in,nifti_ref,fname_out,img_type='intensity'):"""Resample a nifti image to the same space as another nifti image.Parameters----------nifti_in : stringPath to the nifti image to be resampled.nifti_ref : stringPath to the nifti image to be used as reference.fname_out : stringPath to the resampled nifti image.img_type : string, optionalType of the image. Default is 'intensity'.'intensity': general type, no conversion.'BQML': PET or quantitative SPECT image, total counts are preserved.'mask': interger mask, interpolation will not change the value."""Convert PET DICOM to NIFTI with SUVdicom2niftiSUVallows to convert a PET DICOM image to a NIFTI image with SUV values. The SUV values are computed using the corresponding DICOM tags.defdicom2niftiSUV(dicomdir,niftiname):"""Convert a folder of dicom files to nifti files and apply SUV conversion.Parameters----------dicomdir : stringPath to the folder containing dicom files.niftiname : stringPath and filename to the output nifti file."""Sort files in the DICOMDIR file into hierarchical foldersdicomdir_splitallows to sort the files in a DICOMDIR file into hierarchical folders in thePatient/Study/Seriesfashion. This might be useful when extracting the desired DICOM series from a DICOMDIR file.defdicomdir_split(dicomdir_path,output_folder):''' Split DICOM files in the DICOMDIR into different folders based according to patient, studies, and series.Parameters----------dicomdir_path : stringPath to the DICOMDIR file.output_folder : stringPath to the output folder.'''Important notesStandard orientationThe matrix indices of the 3D images can be confusing. In this project, the author always assumes the following standard orientation, as shown in the figure below.