Created
November 22, 2023 20:58
-
-
Save sarthakpati/a5337aff797ffd2290c85f7706f9624a to your computer and use it in GitHub Desktop.
Get basic image properties of ITK 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 os | |
import SimpleITK as sitk | |
input_directory = os.path.join(os.getcwd(), "data") | |
for filename in os.listdir(input_directory): | |
if filename.endswith(".nii.gz"): | |
image = sitk.ReadImage(os.path.join(input_directory, filename)) | |
# properties | |
dims = image.GetDimension() | |
origin = image.GetOrigin() | |
spacing = image.GetSpacing() | |
size = image.GetSize() | |
# stats using itk filters | |
stats = sitk.StatisticsImageFilter() | |
stats.Execute(image) | |
min_val = stats.GetMinimum() | |
max_val = stats.GetMaximum() | |
mean = stats.GetMean() | |
std = stats.GetSigma() | |
print(f"Image: {filename}") | |
print(f"Dimensions: {dims}") | |
print(f"Origin: {origin}") | |
print(f"Spacing: {spacing}") | |
print(f"Size: {size}") | |
print(f"Min: {min_val}") | |
print(f"Max: {max_val}") | |
print(f"Mean: {mean}") | |
print(f"Std: {std}") | |
print("") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment