-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathants_label_qc_image.sh
executable file
·60 lines (46 loc) · 10.6 KB
/
ants_label_qc_image.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env bash
#Calculator for maths
calc() { awk "BEGIN{ print $* }"; }
imagefile=$1
labelfile=$2
output=$3
tmpdir=$(mktemp -d)
LUT=$(cat <<EOF
0 0.3203125 0.98828125 0.9921875 0.44921875 0.6484375 0.3203125 0.15234375 0.9921875 0.41796875 0.0546875 0.890625 0.59765625 0.8984375 0.9765625 0.41015625 0.67578125 0.6171875 0.83984375 0.0234375 0.9609375 0.99609375 0.34375 0.19921875 0.9375 0.23828125 0.23046875 0.94921875 0.31640625 0.6171875 0.6015625 0.6015625 0.4296875 0.82421875 0.96484375 0.73828125 0.99609375 0.703125 0.1328125 0.9765625 0.078125 0.6171875 0.7578125 0.76953125 0.4375 0.671875 0.11328125 0.82421875 0.48828125 0.5703125 0.78515625 0.73828125 0.77734375 0.171875 0.85546875 0.390625 0.54296875 0.78515625 0.97265625 0.1640625 0.65234375 0.21484375 0.27734375 0.99609375 0.60546875 0.640625 0.0625 0.76171875 0.84765625 0.609375 0.54296875 0.76953125 0.9609375 0.44140625 0.56640625 0.80859375 0.3984375 0.6953125 0.97265625 0.53515625 0.28515625 0.1484375 0.98046875 0.7890625 0.37109375 0.8984375 0.29296875 0.54296875 0.75390625 0.57421875 0.93359375 0.9296875 0.9609375 0.5078125 0.97265625 0.05859375 0.8984375 0.69140625 0.66015625 0.1171875 0.6328125 0.67578125 0.81640625 0.42578125 0.99609375 0.6015625 0.6640625 0.7890625 0.41015625 0.4453125 0.234375 0.4609375 0.9765625 0.51171875 0.69140625 0.65234375 0.1953125 0.15625 0.859375 0.1328125 0.18359375 0.9375 0.45703125 0.8984375 0.96875 0.8515625 0.30859375 0.203125 0.70703125 0.21484375 0.984375 0.4765625 0.26171875 0.1953125 0.69140625 0.9375 0.671875 0.7890625 0.44140625 0.97265625 0.68359375 0.234375 0.8828125 0.8828125 0.6484375 0.1875 0.46875 0.50390625 0.04296875 0.62109375 0.8046875 0.3671875 0.15625 0.55859375 0.19921875 0.4609375 0.046875 0.9296875 0.60546875 0.99609375 0.515625 0.94140625 0.91796875 0.15625 0.859375 0.99609375 0.16796875 0.9921875 0.97265625 0.62890625 0.75 0.18359375 0.78125 0.20703125 0.26953125 0.69921875 0.88671875 0.2421875 0.6875 0.51171875 0.48046875 0 0.9375 0.7265625 0.1953125 0.28515625 0.69921875 0.40625 0.60546875 0.8125 0.23046875 0.14453125 0.12890625 0.83203125 0.08984375 0.59375 0.87890625 0.921875 0.79296875 0.66015625 0.6953125 0.65625 0.28515625 0.59765625 0.6640625 0.234375 0.62109375 0.40625 0.48828125 0.48046875 0.06640625 0.546875 0.26171875 0.72265625 0.140625 0.31640625 0.47265625 0.828125 0.7734375 0.9765625 0.96875 0.9921875 0.4453125 0.8125 0.82421875 0.81640625 0.71484375 0.8828125 0.6328125 0.9921875 0.3046875 0.89453125 0.6640625 0.26171875 0.2890625 0.8203125 0.4765625 0.6796875 0.6796875 0.91015625 0.921875 0.17578125 0.3125 0.57421875 0.890625 0.91015625 0.8359375 0.6484375 0.95703125 0.43359375 0.46875 0.57421875 0.9375 0.13671875 0.234375
0 0.48828125 0.203125 0.06640625 0.734375 0.25390625 0.9921875 0.953125 0.61328125 0.40625 0.47265625 0.98828125 0.3359375 0.63671875 0.34765625 0.39453125 0.9765625 0.21875 0.71875 0.6796875 0.1953125 0.45703125 0.8828125 0.68359375 0.546875 0.55078125 0.515625 0.1796875 0.43359375 0.27734375 0.7421875 0.453125 0.3828125 0.4765625 0.828125 0.3828125 0.640625 0.2265625 0.84765625 0.44140625 0.69921875 0.9375 0.7421875 0.0390625 0.9921875 0.75390625 0.41015625 0.28515625 0.41796875 0.3671875 0.09765625 0.50390625 0.265625 0.51953125 0.94921875 0.609375 0.5703125 0 0.51953125 0.65234375 0.33984375 0.8828125 0.8203125 0.1484375 0.2421875 0.5234375 0.37109375 0.5859375 0.671875 0.34765625 0.54296875 0.74609375 0.2578125 0.765625 0.8984375 0.4296875 0.45703125 0.20703125 0.4765625 0.65625 0.57421875 0.671875 0.3046875 0.2109375 0.62109375 0.51171875 0.65625 0.63671875 0.07421875 0.28125 0.61328125 0.8671875 0.375 0.9765625 0.7265625 0.578125 0.421875 0.3671875 0.93359375 0.5625 0.51171875 0.21875 0.16015625 0.890625 0.33984375 0.42578125 0.30078125 0.57421875 0.35546875 0.96484375 0.3828125 0.7421875 0.19140625 0.2734375 0.16796875 0.27734375 0.4296875 0.48046875 0.296875 0.9609375 0.44921875 0.25 0.953125 0.99609375 0.453125 0.40625 0.9765625 0.50390625 0.44140625 0.43359375 0.44140625 0.3671875 0.890625 0.6015625 0.40625 0.65234375 0.87890625 0.98828125 0.35546875 0.57421875 0.19921875 0.4296875 0.5703125 0.8359375 0.5 0.484375 0.4921875 0.48828125 0.75390625 0.30859375 0.06640625 0.83203125 0.5546875 0.9453125 0.640625 0.671875 0.74609375 0.4453125 0.80078125 0.328125 0.359375 0.5625 0.26953125 0.78125 0.72265625 0.4140625 0.578125 0.5546875 0.4453125 0.4375 0.14453125 0.9765625 0.89453125 0.54296875 0.43359375 0.65625 0.15625 0.66796875 0.25 0.46484375 0.81640625 0.8828125 0.79296875 0.97265625 0.48046875 0.62109375 0.359375 0.77734375 0.56640625 0.953125 0.81640625 0.40625 0.734375 0.57421875 0.8359375 0.3046875 0.60546875 0.4453125 0.6875 0.28125 0.90234375 0.265625 0.40234375 0.31640625 0.4375 0.484375 0.97265625 0.81640625 0.375 0.95703125 0.87890625 0.6015625 0.3984375 0.58203125 0.453125 0.734375 0.93359375 0.15234375 0.15625 0.26953125 0.59375 0.30859375 0.33984375 0.50390625 0.62890625 0.0703125 0.71484375 0.15625 0.2734375 0.42578125 0.87109375 0.97265625 0.22265625 0.59765625 0.44921875 0.328125 0.35546875 0.28515625 0.35546875 0.41796875 0.7734375 0.47265625 0.3984375 0.28515625 0.07421875 0.71484375 0.04296875 0.33203125 0.3203125 0.91015625 0.4296875 0.41796875 0.13671875 0.85546875 0.5546875
0 0.04296875 0.98828125 0.18359375 0.94921875 0.48046875 0.765625 0.2265625 0.125 0.94140625 0.41015625 0.25 0.17578125 0.95703125 0.4453125 0.578125 0.53515625 0.6796875 0.3203125 0.171875 0.58203125 0.2890625 0.9375 0.4609375 0.65234375 0.96875 0.61328125 0.76953125 0.23046875 0.30859375 0.171875 0.03125 0.74609375 0.98046875 0.1953125 0.02734375 0.359375 0.1640625 0.49609375 0.7578125 0.625 0.23828125 0.98828125 0.3671875 0.46875 0.36328125 0.671875 0.03125 0.16015625 0.62109375 0.2421875 0.66015625 0.85546875 0.1953125 0.46484375 0.35546875 0.10546875 0.5625 0.41796875 0.9921875 0.9453125 0.8125 0.17578125 0.42578125 0.58203125 0.99609375 0.77734375 0.0546875 0.87890625 0.42578125 0.921875 0.16015625 0.265625 0.140625 0.57421875 0.44140625 0.12890625 0.33203125 0.171875 0.80859375 0.42578125 0.2890625 0.921875 0.53125 0.12890625 0.828125 0.78125 0.93359375 0.69140625 0.796875 0.78125 0.3515625 0.6328125 0.58203125 0.33203125 0.55859375 0.89453125 0.30859375 0.36328125 0.04296875 0.7109375 0.25 0.0859375 0.66015625 0.1484375 0.21484375 0.13671875 0.98828125 0.8125 0.1875 0.7109375 0.45703125 0.50390625 0.69921875 0.546875 0.39453125 0.58203125 0.4921875 0.97265625 0.35546875 0.19921875 0.671875 0.31640625 0.41015625 0.85546875 0.99609375 0.625 0.7265625 0.046875 0.09375 0.44921875 0.00390625 0.7421875 0.875 0.5390625 0.16796875 0.453125 0.29296875 0.59375 0.94921875 0.421875 0.27734375 0.80859375 0.1328125 0.21875 0.94921875 0.96484375 0.0859375 0.49609375 0.54296875 0.1875 0.94140625 0.89453125 0.39453125 0.01953125 0.078125 0.61328125 0.6640625 0.97265625 0.578125 0.8984375 0.40234375 0.4453125 0.27734375 0.0859375 0.52734375 0.16796875 0.72265625 0.3828125 0.921875 0.33203125 0.69921875 0.19921875 0.58984375 0.1640625 0.9375 0.8515625 0.33984375 0.765625 0.6171875 0.0703125 0 0.34375 0.18359375 0.34765625 0.796875 0.3984375 0.953125 0.0625 0.10546875 0.421875 0.859375 0.703125 0.06640625 0.94140625 0.6328125 0.98828125 0.4609375 0.9375 0.453125 0.19921875 0.28125 0.55859375 0.26953125 0.10546875 0.8828125 0.578125 0.1484375 0.70703125 0.6171875 0.56640625 0.9609375 0.640625 0.96875 0.421875 0.8828125 0.65625 0.51953125 0.4140625 0.23046875 0.29296875 0.41015625 0.6171875 0.96484375 0.95703125 0.359375 0.92578125 0.12890625 0.77734375 0.81640625 0.6328125 0.34375 0.6640625 0.9609375 0.21484375 0.01953125 0.8359375 0.10546875 0.01953125 0.9296875 0.12890625 0.671875 0.703125 0.70703125 0.71484375 0.9296875 0.60546875 0.26171875 0.8984375 0.703125 0.984375 0.01953125 0.48828125 0.53125 0.40625
EOF
)
# Fix any orientation issues
ConvertImage 3 ${imagefile} ${tmpdir}/image.nii.gz
# Mask needed so that only the ROI of the labels gets the overlay
ThresholdImage 3 ${labelfile} ${tmpdir}/mask.nii.gz 0.5 inf 1 0
# Make an RGB NIFTI file
ConvertScalarImageToRGB 3 ${labelfile} ${tmpdir}/label.nii.gz none custom <(echo "$LUT") 0 255 0 255
# Make a label file with no values so we can make an unlabelled image
ThresholdImage 3 ${labelfile} ${tmpdir}/mask2.nii.gz 0 inf 0 0
ConvertScalarImageToRGB 3 ${tmpdir}/mask2.nii.gz ${tmpdir}/label2.nii.gz none custom <(echo "$LUT") 0 255 0 255
# Compute the bounding box of the labels from the mask
LabelGeometryMeasures 3 ${tmpdir}/mask.nii.gz na ${tmpdir}/geometry.csv
# Extract geometry measures for calculation
BoundingBoxLower_x=$(tail -1 ${tmpdir}/geometry.csv | cut -d, -f 13)
BoundingBoxUpper_x=$(tail -1 ${tmpdir}/geometry.csv | cut -d, -f 14)
BoundingBoxLower_y=$(tail -1 ${tmpdir}/geometry.csv | cut -d, -f 15)
BoundingBoxUpper_y=$(tail -1 ${tmpdir}/geometry.csv | cut -d, -f 16)
BoundingBoxLower_z=$(tail -1 ${tmpdir}/geometry.csv | cut -d, -f 17)
BoundingBoxUpper_z=$(tail -1 ${tmpdir}/geometry.csv | cut -d, -f 18)
# Calculate a nicely spaced sequence of slices along the three dimensions
x_slices=$(for item in $(seq ${BoundingBoxLower_x} $(calc "(${BoundingBoxUpper_x}-${BoundingBoxLower_x})/10") ${BoundingBoxUpper_x}); do printf "$(calc "int(${item})")\n"; done | head -10 | paste -s -dx)
y_slices=$(for item in $(seq ${BoundingBoxLower_y} $(calc "(${BoundingBoxUpper_y}-${BoundingBoxLower_y})/10") ${BoundingBoxUpper_y}); do printf "$(calc "int(${item})")\n"; done | head -10 | paste -s -dx)
z_slices=$(for item in $(seq ${BoundingBoxLower_z} $(calc "(${BoundingBoxUpper_z}-${BoundingBoxLower_z})/10") ${BoundingBoxUpper_z}); do printf "$(calc "int(${item})")\n"; done | head -10 | paste -s -dx)
# Create unlabelled images
CreateTiledMosaic -i ${tmpdir}/image.nii.gz -x ${tmpdir}/mask2.nii.gz -r ${tmpdir}/label2.nii.gz -a 0 -o ${tmpdir}/s2.jpg -f 0x1 -d x -s ${x_slices} -t 1x10
CreateTiledMosaic -i ${tmpdir}/image.nii.gz -x ${tmpdir}/mask2.nii.gz -r ${tmpdir}/label2.nii.gz -a 0 -o ${tmpdir}/c2.jpg -f 0x1 -d y -s ${y_slices} -t 1x10
CreateTiledMosaic -i ${tmpdir}/image.nii.gz -x ${tmpdir}/mask2.nii.gz -r ${tmpdir}/label2.nii.gz -a 0 -o ${tmpdir}/t2.jpg -f 0x1 -d z -s ${z_slices} -t 1x10
# Create labelled images
CreateTiledMosaic -i ${tmpdir}/image.nii.gz -x ${tmpdir}/mask.nii.gz -r ${tmpdir}/label.nii.gz -a 0.5 -o ${tmpdir}/s.jpg -f 0x1 -d x -s ${x_slices} -t 1x10
CreateTiledMosaic -i ${tmpdir}/image.nii.gz -x ${tmpdir}/mask.nii.gz -r ${tmpdir}/label.nii.gz -a 0.5 -o ${tmpdir}/c.jpg -f 0x1 -d y -s ${y_slices} -t 1x10
CreateTiledMosaic -i ${tmpdir}/image.nii.gz -x ${tmpdir}/mask.nii.gz -r ${tmpdir}/label.nii.gz -a 0.5 -o ${tmpdir}/t.jpg -f 0x1 -d z -s ${z_slices} -t 1x10
# Stack them
convert -background black ${tmpdir}/*jpg -trim -adaptive-resize 3840x -gravity center -append ${output}
rm -r ${tmpdir}