Hi,
Using fslhd and sed, making your own header changes is relatively easy and robust:
The logic
> fslhd -x file.nii > tmp1.fslhd
> sed -e :a -e "s/cal_max[ \t]*=[ \t]*'[.0123456789]*e*[+-]*[0123456789]*'/cal_max = 1024'/g" tmp1.fslhd > tmp2.fslhd;
> fslcreatehd tmp2.fslhd file.nii
(which sets the cal_max field of file.nii to 1024) can be used in a script, such
as a bash script, in a more general way. (see the attached example)
fslhd does not always give all the fields' values as some depend on others, so
an extra line for that is required, other than that it's straightforward.
bw
Alle Meije
############################################################
#!/bin/bash
# is the syntax correct?
if [[ $3 == "" ]]; then
echo ""
echo "mod_hdr <nifti_file> <nifti_field> <value>"
echo ""
echo "<nifti_file>: a NifTI image"
echo "<nifti_field>: the name of a NifTI header field"
echo "<value>: the updated value for that field"
echo ""
exit
fi
# put the header of ${1} in a temporary file
fslhd -x ${1} > tmp1.fslhd
# if no slice_code -> put it in and set it to 4
# if slice_code in -> change whatever it was to 4
#grep ${2} tmp1.fslhd
if [[ `grep ${2} tmp1.fslhd` == "" ]]; then
echo "field not found. adding to record."
#sed -e :a -e "s/nifti_image/nifti_image\n ${2} = '${3}'/g" tmp1.fslhd
sed -e :a -e "s/nifti_image/nifti_image\n ${2} = '${3}'/g" tmp1.fslhd >
tmp2.fslhd;
else
echo "field found. modifying record."
#sed -e :a -e "s/${2}[ \t]*=[ \t]*'[.0123456789]*e*[+-]*[0123456789]*'/${2}
= '${3}'/g" tmp1.fslhd
sed -e :a -e "s/${2}[ \t]*=[ \t]*'[.0123456789]*e*[+-]*[0123456789]*'/${2}
= '${3}'/g" tmp1.fslhd > tmp2.fslhd;
fi
# put the new header in
fslcreatehd tmp2.fslhd ${1}
############################################################
Matthew Webster wrote:
> Hello!
> For header editing like this the program nifti_tool is really
> good..
>
> Many Regards
> Matthew
>
>> Hi,
>>
>> For quite some time I have had a little script that uses FSL tools to
>> automatically set the cal_max and cal_min values in the NIFTI header
>> to improve default display of certain images in FSLView. Now it seems
>> like this script is not successfully inserting these fields into the
>> header. I use fslhd –x to pull the original header out, then I append
>> the desired fields to it and create a new blank volume with
>> fslcreatehd. This blank volume has the desired fields. When I then
>> try to copy the header from this volume to the original data file, the
>> values get stripped back out. I have tried using the blank volume as
>> the first volume in an fslmerge command with the original data (and
>> then an fslroi to reextract the original data with the new header),
>> which is what worked before, and I have tried using fslcpgeom to copy
>> the header from the blank volume to the new volume. I would like to
>> continue to be able to set these fields as desired in an automated
>> fashion, so is there some tool that will allow me to copy the header
>> without losing the fields of interest?
>>
>> Thanks,
>>
>> Matt.
>
|