Skip to content

Instantly share code, notes, and snippets.

@stowler
Last active June 20, 2017 19:00
Show Gist options
  • Save stowler/5493255 to your computer and use it in GitHub Desktop.
Save stowler/5493255 to your computer and use it in GitHub Desktop.
lut_convertToFslview.sh : an unsafe proof-of-concept bash script for converting LUTs to fslview LUTs
#!/bin/bash
#
# CALL AS: lut_convertToFslview.sh
# (no arguments: input and output files defined below)
#
# CREATED: 20130430 by [email protected]
# LAST UPDATED: same
#
#
#
# DESCRIPTION: an unsafe proof-of-concept bash script for converting LUTs to fslview LUTs
#
# See screenshot and original google plus post: http://goo.gl/WxmsS
#
# fslview can use lookup tables (LUTs) from mricron and imagej if you convert
# them to fslview's LUT format. This seems to work, but I haven't tested it
# thoroughly:
#
# 1) Identify the imagej or mricron lookup table you would like to use in
# fslview. It is stored in a file ending in ".lut". For example:
# /Applications/mricron.app/Contents/MacOS/lut/actc.lut
#
# 2) Open the LUT (ImageJ: File->Open…)
#
# 3) Save the LUT as columns of text.
# (ImageJ: Image->Color->ShowLUT->List->File->SaveAs->
# something like /tmp/LUT_exportedFromImageJ.txt )
#
# 4) Convert the columns of text to a fslview *.lut file using a script like this one
#
# 5) Open an image in fslview and load the new LUT to confirm success
# (see http://fsl.fmrib.ox.ac.uk/fsl/fslview/imageinfo.html )
#
# STYSTEM REQUIREMENTS:
# - mricron and imagej to supply the original input LUT
# - imagej to convert the LUT
# - fslview to load the new LUT
#
# INPUT FILES AND PERMISSIONS:
# - need read permissions for the input LUT
# - need write permisisons for the output LUT (can be written anywhere)
#
# OTHER ASSUMPTIONS:
# - as described above, user manually exports LUT via ImageJ and then this
# script converts that LUT to fslview's LUT format
#
# !!!!!!!!!!!!!!!!!!!!!! DANGER: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# Tested (just barely) with one mricron LUT on two systems:
#
# mac os x: /Applications/mricron.app/Contents/MacOS/lut/actc.lut
# neurodebian: /usr/share/mricron/lut/actc.lut
# a test image: http://www.fmrib.ox.ac.uk/~steve/ftp/colourbar.nii.gz
# (linked from https://www.jiscmail.ac.uk/cgi-bin/webadmin?A2=FSL;d533faef.0701 )
#
##############################################################
# set input and output files:
##############################################################
lutInFromImageJ=/tmp/LUT_exportedFromImageJ.txt
lutOutForFslview=/tmp/LUT_outForFslview.lut
##############################################################
# give the user some information about the input LUT:
##############################################################
echo ""
echo "Your input LUT from ImageJ:"
ls -l ${lutInFromImageJ}
echo ""
head ${lutInFromImageJ}
echo "...snip..."
qtyInputLines="`wc -l ${lutInFromImageJ} | awk '{print $1}' `"
echo "(found a total of ${qtyInputLines} lines in the input LUT. Should be one more than the number of mapped intensities.)"
echo ""
##############################################################
# reformat input LUT's RGB values for fslview, store in variable:
##############################################################
echo ""
echo -n "Converting to fslview's LUT format..."
# Create one line per mapped intensity in the format matching fslview LUT.
# These lines should contain RGB values between 0 and 1 inclusive (I'm guessing).
# First line maps color corresponding to lowest visible intensity.
# Last line maps color corresponding to highest visible intensity.
#
# e.g.,
# <-color{0.000000,0.000000,0.000000}->
# <-color{0.010000,0.010000,0.010000}->
# <-color{0.020000,0.020000,0.020000}->
# ...and so on...
#
colorLines=''
while read line; do
[[ $line =~ .*lue.* ]] && continue # skip the input header line. the lazy way.
grayscaleValue="`echo ${line} | cut -d ' ' -f 1`"
redInput="`echo ${line} | cut -d ' ' -f 2`"
greenInput="`echo ${line} | cut -d ' ' -f 3`"
blueInput="`echo ${line} | cut -d ' ' -f 4`"
# my imagej/mricron input LUT is expressed as RGB with values between 0 and
# 255, so now I'm dividing by 255 for the [0,1] output I believe is needed by
# fslview:
redOutput=`echo "scale=6;${redInput}/255" | bc `
greenOutput=`echo "scale=6;${greenInput}/255" | bc `
blueOutput=`echo "scale=6;${blueInput}/255" | bc `
colorLines="${colorLines}<-color{${redOutput},${greenOutput},${blueOutput}}->\n"
done < ${lutInFromImageJ}
echo "done."
##############################################################
# create wrapper from existing fslview lut:
##############################################################
read -d '' lutHeaderFromFslview <<-EndOfHeader
%!VEST-LUT
%%BeginInstance
<<
/SavedInstanceClassName /ClassLUT
/PseudoColorMinimum 0.00
/PseudoColorMaximum 1.00
/PseudoColorMinControl /Low
/PseudoColorMaxControl /High
/PseudoColormap [
EndOfHeader
read -d '' lutFooterFromFslview <<-EndOfFooter
]
>>
%%EndInstance
%%EOF
EndOfFooter
##############################################################
# write the new fslview LUT to a file:
##############################################################
# make room for the new LUT:
rm -f ${lutOutForFslview}
# create the new LUT by writing its header, colorLines, and footer:
echo "${lutHeaderFromFslview}" >> ${lutOutForFslview}
echo -e "${colorLines}" >> ${lutOutForFslview}
echo "${lutFooterFromFslview}" >> ${lutOutForFslview}
##############################################################
# tell the user what we did:
##############################################################
echo ""
echo "This is your new LUT for importing into fslview:"
ls -l ${lutOutForFslview}
qtyOutputLines="`wc -l ${lutOutForFslview} | awk '{print $1}'`"
echo ""
echo "It contains ${qtyOutputLines} lines. The number of lines in the fslview"
echo "LUT should be about 15 more than the number of mapped intensities in the input"
echo "LUT (e.g., 256 intensities plus 15 lines of LUT wrapper = 271 lines)."
echo ""
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment