Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import SimpleITK as sitk
import numpy as np
import pandas as pd
# downloaded from
filename = 'annotation_25.nrrd'
itkimage = sitk.ReadImage(filename)
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(
df['count'] = df['count'].astype(
filename = 'value_count.csv'
df.to_csv( filename, index=False )
Copy link

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