Last active
January 19, 2024 13:15
-
-
Save rmukh/a655b5ac16a37d72cb6f20747664e4af to your computer and use it in GitHub Desktop.
Script to read FreeSurfer Color LUT table in Python 3
This file contains 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 numpy as np | |
import re | |
""" | |
The link to the table: https://surfer.nmr.mgh.harvard.edu/fswiki/FsTutorial/AnatomicalROI/FreeSurferColorLUT | |
It is also available locally if FreeSurfer is installed: $FREESURFER_HOME/FreeSurferColorLUT.txt | |
Read and store FreeSufer LUT color table | |
The result is two variables: | |
rgb - numpy array where 0th dimention is the region code (first column) and 3 other | |
dimensions are for RBG values. Final fimension: (number of regions x 3) | |
label_names - dictinory where key = region code, and value = label name | |
""" | |
rgb = np.empty((0, 4), dtype=np.int64) | |
label_names = {} | |
# replace the path to the FreeSurferColorLUT.txt with your own | |
# in linux you can do: echo $FREESURFER_HOME/FreeSurferColorLUT.txt | |
with open('/usr/local/freesurfer/FreeSurferColorLUT.txt', 'r') as f: | |
raw_lut = f.readlines() | |
# read and process line by line | |
pattern = re.compile(r'\d{1,5}[ ]+[a-zA-Z-_0-9*.]+[ ]+\d{1,3}[ ]+\d{1,3}[ ]+\d{1,3}[ ]+\d{1,3}') | |
for line in raw_lut: | |
if pattern.match(line): | |
s = line.rstrip().split(' ') | |
s = list(filter(None, s)) | |
rgb = np.append(rgb, np.array([[int(s[0]), int(s[2]), int(s[3]), int(s[4])]]), axis=0) | |
label_names[int(s[0])] = s[1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment