Last active
April 7, 2022 16:54
-
-
Save jcreinhold/587487a103c1e873077d1a2ef98190a1 to your computer and use it in GitHub Desktop.
normalize one image by a rough estimate of tissue class mean
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
#!/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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you use this script in an academic paper, please cite the paper: