Converting Between Image Formats

To convert between the various image formats used in the lab, we use a program called imconvert. This program has an intimidating number of options, but is usually very simple to use. In general the command line is:

> imconvert -i InputFileName.extension -o OutputFileName.extension

The file types for the input and output files are detected automatically based on the file names, and the contents of the files. Please be aware that not all file formats are complete, in the sense that not all of the patient or scan data are contained in them. For example, when files are converted to any of the MGH-style formats (file.bshort, file.bfloat, file.buchar) all information on the subject and on the scan parameters are ignored. For more information on file types, see Image File Formats.

In addition to imconvert, there are a variety of tools to convert to and from special formats. Please see the Links below.

News Flash: As a result of numerous requests, I have posted the source for imconvert [HERE].

Note that the file names for the data and header files generally must match. If the data are found without a corresponding header, imconvert (and other programs) will generate an error.
Recognized
Input Formats
Name style Available
Output formats
Name style
.bshort filename.bshort
filename.hdr
.bshort filename.bshort
filename.hdr
.bfloat anything.bfloat
anything.hdr
.bfloat filename.bfloat
filename.hdr
.buchar filename.buchar
filename.hdr
.buchar filename.buchar
filename.hdr
Analyze filename.img
filename.hdr
Analyze (4D) filename.img
filename.hdr
ANMR APD2 fname_xxxxx_xxx_xxx_xxx_001.img
fname_xxxxx_xxx_xxx_xxx.irp
Analyze (3D) filename.txxx.img
filename.txxx.hdr
Signa genesis ExxxxSxxxIxxx.MR
DICOM filename.dcm (note: Siemens uses a modified DICOM "mosaic" format similar to mdisplay below. Our convertor, dcm2img, recognizes and handles this.)
mdisplay Any of the output formats with multiple slices in each image. The image at right is an example of an mdisplay format. Typically, bshort, buchar or bfloat are used, but Analyze 3 and 4D are valid. mdisplay format:

While imconvert generally produces good results, there are cases in which you will want to over-ride its default behaviors. Therefore, imconvert supports a number of optional arguments

Option

Meaning

-M followed by a scalar multiplier. The pixel intensities in the input images are multiplied by this value before being saved. This is useful if the input values are either very small or very large compared to the intensity resolution of the output type.
-T followed by the slice thickness. With this option, you can specify the slice thickness to be used in Analyze format outputs
-A followed by the slice thickness. This sets up Analyze 3D format, the file type most often used for input to AIR and to SPM. Given an input file containing many slices and N points, imconvert -A thickness will produce N output files, each of which contains all slices at a time point with a file name that reflects the time point. The thickness parameter will be set to the number (in mm) following -A.

Each time point file is given a unique name, based on the time point. Thus, if the output name myFile.img was specified for an input containing four time points, imconvert will create four files (and four headers):

myFile.001.img myFile.002.img myFile.003.img myFile.004.img

NOTE: Analyze 4D files are saved by default, when an output file name of filename.img is specified.

-# followed by the number of slices in the input file. When images are saved in multiple display (mdisp) format, as by CC_gr with the -b option, they can be hard to handle with some traditional image processing tools. This option reformats the images such that the slices are readily separated. The output style is sometimes called a stack or brik format.
-t save each time point as a separate file. This is automatically engaged when the -A option is used
-s save a separate file containing all time points at each slice location. This is automatically engaged when the -e option is used.

Each time point file is given a unique name, based on the time point. Thus, if the output name myFile.img was specified for an input containing four time slices, imconvert will create four files (and four headers):

myFile.s001.img myFile.s002.img myFile.s003.img myFile.s004.img

-r followed by a number. Enter the tr (in msec) as expected by some programs (fsl, spm)
-m followed by the first through last slices to be used in the input file or the letter A. Create mdisplay output (multiple slice locations per image). The following argument is the first and last slice location to use in the output. For example,

imconvert -i infile -o outfile -m 4-6

creates a multiple display output having 3 slice locations (numbers 4 through 6) for each time point. Note that the slice locations start from 1.

imconvert -i infile -o outfile -m A

creates a multiple display output using all slices in the input file.

-O followed by L or B. Force the output byte order to be either big endian (B) or little endian (L). Macintosh, Sun and SGI computers are big endian, pc's and alphastations are little endian. For example,

imconvert -i infile -o outfile -O L

creates a pc or alpha compatible output file.

-N Expand or shrink the intensity range of the input images to fit the full output range of the output data type. This option is essentially meaningless with floating point outputs (.bfloat, or -f). When used with integer formats, it preserves the best possible intensity resolution.
-R AutoRange. The output images cover the full intensity range of the output type, but imconvert first calculates a noise floor and peak threshold. This is the best option to use if you are converted to a limited type (e.g., buchar) for display purposes (e.g, with Photoshop).
-e Emulates the program epi2bshort (which should never be used!), but without the problems of image slice shifts that occur with the older program. At this time, it is hard to imagine why you should need this function, as imconvert -m probably fills your need. A separate file is created for each slice, that contains all time points at that location. Output file names are generated automatically. For a four slice input file, for example, imconvert -e will create the data files:

A1.s1.img A1.s2.img A1.s3.img A1.s4.img

-f Force the output type to float, especially useful when statistical images are saved in Analyze format. Note that few programs can take advantage of this data type (scanSTAT is an exception).
-X -Y Specify the field of view (in mm) to be used in the header for the output images. Very useful if the input type is bshort, bfloat or buchar. If X is entered, but not Y, the field of view is assumed to be square.
-V Verbose. Print out diagnostic information while running.
-F The Flip option is very important: some programs (e.g., fsl) expect the images to have the posterior at the top on axial slices. You should carefully verify the orientation of your converted images.
-l followed by a number representing the number of lines that need to be moved from the bottom of the first slice to the top.
original dataset Picture A shows an original dataset where, in the first slice (of each time point) eyes appear to be 'in the back of the head'. Picture B shows a fixed dataset, after using imconvert with option '-l'. Unfortunately, there is no rule as to how many lines need to be moved from the bottom of the first slice, to the top (I have had a few of these and for each of them I need to move 18 lines up, but this is in no way a rule!), so you should inspect your dataset first. It would be helpful to view the dataset in a viewer that allows sagittal views, count the number of lines and then use imconvert. Good tool for this purpose is afni.
A
fixed dataset This option can be used at the same time as converting images, so for example if you had an analyze image that you need to convert to bshort (in mdisplay layout) and fix the first slice at the same time you would use:

imconvert -i input.img -o output.bshort -m -l 18

But, you could also this option option to just fix an image that you have without changing the format:

imconvert -i input.img -o output.img -l 18

B
This option can also be used in combination with any of the other imconvert options.

While imconvert works well with most file types, there are special cases for which better tools exist.


More Help...

Like many programs used in the Brain Mapping MRI Lab, if you enter the program name (imconvert) without arguments, it will print out a short help message. The arguments may be entered in any order. Any arguments in square brackets are optional.


USAGE: imconvert -i infile.ext1 -o outfile.ext2 [ M, T, A, #, t, s, m, O, X, Y, R, e, f, F, c, V]

General purpose file format conversion. File types are interpreted from the names
The first two arguments are required

        Don't be put off, ONLY the first two are required, the rest are special purpose
options
        -M scalar intensity multiplier
        -T slice thickness (for Analyze output)
        -A Analyze 3D format. Follow by slice thickness
        -# number of slices in 'mdisplay' format for infile
        -t separate file for each time point
        -s separate file for each slice
        -N Normalize full range of input intensities to full range of output type
        -m F-L save output files in mdisplay format (multiple slices/image)
               F and L are the first and last slice for mdisplay
               The option '-m A' will use all slices
        -O output byte order.
                Enter -O L for pc/dec (little endian) or
                -O B for mac/Sun/SGI (big endian)
        -X field of view (mm) in X
        -Y field of view (mm) in Y - enter only if different from field of view in X!
        -R Auto window input intensities to full range of output type
        -e emulate epi2bshort (but without offset artifacts!)
        -f force output type to float for Analyze files
        -F flip image top and bottom
        -c suppress cropping off the edges of images
        -V verbose operation
saves a processing file "outFile.imconvert.proc"

Input files may be:
        .bfloat
        .bshort
        .buchar
        .img (Analyze)
        .img (APD)
        .MR (Signa Genesis)
Output files may be:
        .bfloat
        .bshort
        .buchar
        .img (Analyze)
Output byte order is the same as that of the computer on which imconvert is run

EXAMPLES:
Convert the file myIn.bshort to float
> imconvert -i myIn.bshort -o myOut.bfloat

        ------------------

Convert the mdisplay file myIn.bfloat, containing fifteen 5 mm thick slices
to Analyze 3D format. Multiply all values by 100.
> imconvert -i myIn.bfloat -o myOut.img -A 5 -# 15 -M 100
Output files are named myOut.tXXX.img, where XXX is the time point

        ------------------

Emulate epi2bshort, converting APD files to all time points at each slice
> imconvert -i myIn_0123_001_003.img -e
Output files are named A1.sXX.bshort, where XX is the slice number

        ------------------

Convert a Signa genesis file to an Analyze file
> imconvert -i E02334S5I11.MR -o Signa1.img

        ------------------

Rescale data for use with 8-bit display programs (e.g., Photoshop)
> imconvert -i myIn.bfloat -o EightBit.buchar -R

        ------------------

Convert an input file into an mdisplay format using slices 3, 4, 5 and 6
> imconvert -i myIn_0123_001_003.img -o myMDisplay.bshort -m 3-6

        ------------------

Convert an input file into an mdisplay format using ALL slices
> imconvert -i myIn_0123_001_003.img -o myMDisplay.bshort -m A

        ------------------

More information can be found on the Brain Mapping lab HowTo pages

Back to HowTo pages
This page is maintained by Mark Cohen [updated 3-6-03]