Skip to content

Instantly share code, notes, and snippets.

@lydiang
Last active September 22, 2022 04:23
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 lydiang/a1c403fd5ad3f6bc687d0031cd24d86a to your computer and use it in GitHub Desktop.
Save lydiang/a1c403fd5ad3f6bc687d0031cd24d86a to your computer and use it in GitHub Desktop.
import SimpleITK as sitk
import numpy as np
import pandas as pd
# downloaded from http://download.alleninstitute.org/informatics-archive/current-release/mouse_ccf/annotation/ccf_2017/
filename = 'annotation_25.nrrd'
itkimage = sitk.ReadImage(filename)
print(itkimage)
numpyImage = sitk.GetArrayFromImage(itkimage)
values, counts = np.unique(numpyImage[:], return_counts=True)
df = pd.DataFrame()
for x in range(len(values)) :
rec = {}
rec['count'] = counts[x]
rec['value'] = values[x]
df = df.append(rec, ignore_index= True )
df['value'] = df['value'].astype(np.int)
df['count'] = df['count'].astype(np.int)
filename = 'value_count.csv'
df.to_csv( filename, index=False )
@lydiang
Copy link
Author

lydiang commented Sep 22, 2022

Open annotation_25.nrrd file using SimpleITK and count the number of unique values

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