Last active
          October 17, 2022 09:36 
        
      - 
      
 - 
        
Save nanaHa1003/e9f37c888337aa6ddc3a75f8e2a2bcab to your computer and use it in GitHub Desktop.  
    An example script to convert Nifit segmentation from value range (0, 1, 2) to (0, 2) by dropping label 1.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | from argparse import ArgumentParser | |
| import numpy as np | |
| import nibabel as nib | |
| def convert(src, dst): | |
| seg = nib.load(src) | |
| seg_data = np.asanyarray(seg.dataobj) | |
| seg_data = np.where(seg_data == 2, 1, 0).astype(np.uint8) | |
| out = nib.Nifti1Image(seg_data, seg.affine) | |
| nib.save(out, dst) | |
| if __name__ == "__main__": | |
| parser = ArgumentParser() | |
| parser.add_argument("--src", "-s", type=str, help="Source Nifti segmentation file.") | |
| parser.add_argument("--dst", "-d", type=str, help="Output Nifti file.") | |
| args = parser.parse_args() | |
| convert(args.src, args.dst) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment