Created
          April 30, 2021 18:52 
        
      - 
      
- 
        Save sarthakpati/e3d38c5a2572b60b78cb7c21be8bceb0 to your computer and use it in GitHub Desktop. 
    Get bounding box of a label image
  
        
  
    
      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
    
  
  
    
  | import SimpleITK as sitk | |
| image_file = '/path/to/image.nii.gz' | |
| mask_file = '/path/to/image.nii.gz' | |
| image = sitk.ReadImage(image_file) | |
| mask = sitk.ReadImage(mask_file) | |
| bb_filter = sitk.LabelStatisticsImageFilter() | |
| bb_filter.Execute(image, mask) | |
| current_bb = bb_filter.GetBoundingBox(1) | |
| x_size = current_bb[1] - current_bb[0] | |
| y_size = current_bb[3] - current_bb[2] | |
| z_size = current_bb[5] - current_bb[4] | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment