Nifty script to crop an image by supplying a binary mask. Let's assume we have a brain scan and an associated binary mask like

We can crop the original image using


In [1]:
medpy_extract_sub_volume_by_example.py resources/flair.nii.gz output/cropped.nii.gz resources/brainmask.nii.gz


Traceback (most recent call last):
  File "/home/loli/.local/bin/medpy_extract_sub_volume_by_example.py", line 6, in <module>
    exec(compile(open(__file__).read(), __file__, 'exec'))
  File "/home/loli/Workspace/python/medpy/bin/medpy_extract_sub_volume_by_example.py", line 163, in <module>
    main()    
  File "/home/loli/Workspace/python/medpy/bin/medpy_extract_sub_volume_by_example.py", line 88, in main
    (max(0, mask[2].min() - args.offset), mask[2].max() + 1 + args.offset)) # minx, maxx / miny, maxy / minz, maxz
IndexError: tuple index out of range


In [ ]: