Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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