Skip to content

Instantly share code, notes, and snippets.

@jcreinhold
Last active April 7, 2022 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcreinhold/587487a103c1e873077d1a2ef98190a1 to your computer and use it in GitHub Desktop.
Save jcreinhold/587487a103c1e873077d1a2ef98190a1 to your computer and use it in GitHub Desktop.
normalize one image by a rough estimate of tissue class mean
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Normalize the intensity of an image by
finding a tissue mean in the foreground and
voxel-wise dividing the image by that value
Author: Jacob Reinhold
"""
import sys
from argparse import ArgumentParser
from pathlib import Path
import numpy as np
import pymedio.image as mioi
from intensity_normalization.util.io import split_filename
from intensity_normalization.util.tissue_membership import find_tissue_memberships
def main() -> int:
parser = ArgumentParser(description="Normalize image for image processing")
parser.add_argument("path", type=Path)
parser.add_argument("-o", "--output-path", type=Path, default=None)
parser.add_argument("-ot", "--output-type", type=str, default="nii")
parser.add_argument("-t", "--tissue", type=int, default=1, choices=(0, 1, 2))
args = parser.parse_args()
if args.output_path is None:
root, base, _ = split_filename(args.path)
args.output_path = root / f"{base}_norm.{args.output_type.lstrip('.')}"
image = mioi.Image.from_path(args.path)
mask = image > image.mean()
tissue_memberships = find_tissue_memberships(image, mask)
tissue_mem = tissue_memberships[..., args.tissue]
mean = np.average(image, weights=tissue_mem)
normalized = image / mean
normalized.save(args.output_path)
return 0
if __name__ == "__main__":
sys.exit(main())
@jcreinhold
Copy link
Author

If you use this script in an academic paper, please cite the paper:

    @inproceedings{reinhold2019evaluating,
      title={Evaluating the impact of intensity normalization on {MR} image synthesis},
      author={Reinhold, Jacob C and Dewey, Blake E and Carass, Aaron and Prince, Jerry L},
      booktitle={Medical Imaging 2019: Image Processing},
      volume={10949},
      pages={109493H},
      year={2019},
      organization={International Society for Optics and Photonics}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment