# [Convert3D][1] Documentation [TOC] ### What's New? * **-fast-marching** and **-fm-dilate** commands * **-composite** command * New label set specification ### About convert3d **Convert3d** is a command-line tool for converting 3D images between common file formats. The tool also includes a growing list of commands for image manipulation, such as thresholding and resampling. The tool can also be used to obtain information about image files. The simplest way to use **convert3d** is to convert images between formats. Here are some examples. Note that you must use the `-o` command to specify the output of the conversion. c3d input.img -o output.vtk c3d hello.cub -o hello.img c3d float.img -type short -o short.img **Convert3d** works in a way like a [Revese Polish notation][2] calculator. This means that actions must be preceeded by their inputs: instead of writing 'a + b', we must write 'a b +'. In our case, the inputs are image files, and the actions are specified on the command line. For example, to add a pair of images, we would issue the following command: c3d input1.img input2.img -add -o output.img Here, `input1.img` and `input2.img` are a pair of image files (in Analyze format), while `-add` and `-o` are commands. The command `-add` takes two input images, adds them, and generates a single output image. The command `-o` (short for output) takes one input image and writes it to a filename specified after the command (in this case, `output.img`). The behavior of **convert3d** commands can be affected by options. For example, the `-resample` command is affected by the `-interpolation` option. In order for the option to affect a command, *the option must precede the command on the command line*. For example: c3d input.img -interpolation Cubic -resample 50x30x40vox -o output.img All image processing is done in double-precision reals. However you can use the **-type** option to save as any data type. By default, image intensities are rounded to the nearest integer (not rounded down) when saving as a integral data type. ### Command autocompletion for Bash users Users of the bash shell can take advantage of its command autocompletion features. When this is enabled, you can type on the command line c3d input.img -re[TAB] and as you hit the TAB key, the list of possible completions will be presented to you. This way you will have to go back to this reference less often. To enable this feature, download the script [bashcomp.sh][3] and add the following line to your **.profile** script; source SOMEDIR/bashcomp.sh where SOMEDIR should be replaced by the directory where you saved the script. ### Coordinate Frames and NIFTI **Convert3d** is NIFTI-compatible. The transformation between the image coordinates and physical coordinates is read from the NIFTI header and maintained through the pipeline. The physical coordinates are in the **RAS** frame: *x* increases from left to right, *y* from posterior to anterior, *z* from inferior to superior. You can display the coordinate transformation using the **-info-full** command. $ c3d.exe input.nii -info-full Image #1: Image Dimensions : [176, 255, 216] Bounding Box : {[87.2353 103.015 -108.064], [263.235 358.015 107.936]} Voxel Spacing : [1, 1, 1] Intensity Range : [5, 1775] Mean Intensity : 103.175 Voxel->RAS x-form : 1.00000 -0.00000 -0.00000 -87.23527 -0.00000 1.00000 -0.00000 -103.01535 0.00000 0.00000 1.00000 -108.06398 0.00000 0.00000 0.00000 1.00000 Image Metadata: ITK_InputFilterName = NiftiImageIO ITK_OnDiskStorageTypeName = short ### Usage Examples To convert an image from Metaimage format to short-valued Analyze format use c3d input.mha -type short -o output.img.gz To apply some arithmetic to the image, do the following c3d input.mha -scale 4096 -shift 2048 -type short -o output.img.gz To resample and Gaussian blur an image the following command is used c3d input.hdr -interpolation Linear -resample 256x256x192 -smooth 3vox -o output.img.gz To compute **a+2b** with a pair of images, do c3d a.img b.img -scale 2 -add -o c.img ### Multilabel Images Multilabel (ML) images are images where each voxel is assigned a label, chosen from a relatively small set of labels. Labels are typically integers, although they can be floating point numbers. For example, labels can be used to identify anatomical structures in an image: label 1 may correspond to the white matter, 2 to the gray matter, 3 to CSF, and 0 to the background. When processing ML images, one must take certain care to make sure that numerical operations do not blend labels into meaningless values. For instance, smoothing a ML image will create intermediate values at places where two labels are near each other. A similar issue comes up when resampling or reslicing these images. However, **convert3d** provides commands that split the ML image into a set of binary images (one for each label), allowing you to perform operations on each label separately. Subsequently, the processed label-wise images can be combined into a new ML image. This is accomplished using commands **-split** and **-merge**. In addition, the **-foreach** ... **-endfor** command structure allows operations to be applied sequentially to all images on the stack. For example, to smooth the components of a ML image separately, we can use the following command c3d ml.img -split -foreach -smooth 3mm -endfor -merge -o mlsmooth.img The **-merge** command implements voting between individual label images. A label assigned to a voxel during merge is that of the label image with highest intensity at that voxel. The values of the labels assigned during the **-merge** command are remembered from the time the **-split** command is run. So if the image *ml.img* has labels *1, 4, 10* then the output image will also have the same labels. When applying spatial transformations to ML images, one may choose to use nearest neighbor interpolation: c3d ref.img ml.img -interpolation NearestNeighbor -reslice-matrix affine.mat -o mltran.img However, this causes some aliasing of the results. It is sometimes useful to assume that labels are *fuzzy*, and to apply linear or cubic interpolation to the fuzzy labels. Here is how we can apply an affine transformation to a ML image this way: c3d ref.img -popas ref ml.img -split -foreach -smooth 3mm -insert ref 1 -reslice-matrix affine.mat -endfor -merge-o mltran.img This command is somewhat complex, mainly because the **-reslice-matrix** command requires a reference image as the first operand, and we have to use named images to insert it on the stack in the right place. ### Commands: Image Input/Output and Information Passing an image on the command line (without any switches) results in that image being read and placed at the end of the stack. For example: c3d myimage.nii -info another.nii -info will result in information printed for both images. At the end, `myimage.nii` will be in the first position on the stack and `another.nii` will be at the end of the stack. #### -dicom-series-list: List image series in a DICOM directory Syntax: `-dicom-series-list Prints out a table of DICOM series ids and corresponding image information to standard output. #### -dicom-series-read: Read a DICOM image series Syntax: `-dicom-series-read ` Imports a specific DICOM image series from a directory containing DICOM files. The **directory** parameter may also point to one of the DICOM files in the directory. The **seried_id** is a string identifier for the series that can be obtained by calling **-dicom-series-list** #### -info: Display brief image information Syntax: `-info` Prints brief information about the last image on the stack. Does not affect the stack. c3d image.hdr -info Use with the **-foreach** command to get information on multiple images c3d images*.nii -foreach -info -endfor #### -info-full: Display verbose image information Syntax: `-info-full` Prints extended information about the last image on the stack, such as the metadata dictionary. For example, c3d image.hdr -info-full #### -mcs, -multicomponent-split: Enable splitting of multi-component images on read Syntax: `-mcs` Enable reading of multi-component images. By default, when a multi-component image is encountered, the components are combined into a single image. Setting the **-mcs** flag changes this behavior, and each of the components is loaded sequentially. See the section below on multi-component image support. $ c3d -mcs rgb.mha -foreach -probe 110x110x80mm -endfor Interpolated image value at 110 110 80 is 1 Interpolated image value at 110 110 80 is 66 Interpolated image value at 110 110 80 is 29 $ c3d rgb.mha -foreach -probe 110x110x80mm -endfor Interpolated image value at 110 110 80 is 49.5198 #### -nomcs, -no-multicomponent-split: Disable splitting of multi-component images on read Syntax: `-nomcs` Used to reverse the effect of previous **-mcs** command. #### -o: Output (write) last image on the stack to image file Syntax: `-o filename` Write image, overriding an existing image. Without the **-o** option, **convert3d** will write an image only if it does not exist. The **-o** options protects input images from being accidentally deleted. Here we copy an image, changing format: c3d image1.mha -o image2.nii The **-o** option can also be used to save an intermediate image in the stack: c3d image1.img -threshold 1 10 1 0 -o thresh.img -resample 50% -o final.img #### -omc, -output-multicomponent: Output multiple images to single file Syntax: `-omc [number] filename` Write multiple images on the **Convert3d** stack as a single multi-component image file. If the optional number *n* is specified, only the last *n* images on the stack will be used. Not all file formats support multi-component output. NIFTI is the safest bet. c3d red.nii green.nii blue.nii -omc rgb.mha For 2D images, this command can be used to generate color PNG files: c3d image.nii -slice z 50% -colormap jet -type uchar -omc colorslice.png #### -oo: Output multiple images to multiple files Syntax: `-oo image_list` or `-oo image_spec` Write all images on the **convert3d** stack as multiple files. There are two ways to use this command. The first is to supply a list of file names, separated by spaces: c3d labelimage.nii -split -oo labelA.nii labelB.nii labelC.nii In the above example, the image at the end of the stack will be saved as *labelC.nii*, the image next to the end of the stack will be saved as *labelB.nii* and so on. The second way to use the **-oo** command is to supply a pattern for the output filenames. In this case, all the images on the stack will be written. The format for the pattern is the same as for the [C++ printf command][8]. For example, the following command c3d labelimage.nii -split -oo label%02d.nii will generate images *label00.nii*, *label01.nii*, *label02.nii* and so on. The image at the end of the stack will have the highest number, and the image at the beginning of the stack will have number 00. #### -oomc: Output multiple multi-component images to multiple files Syntax: `-oomc n_comp image_list` or `-oomc n_comp image_spec` Write all images on the **convert3d** stack as multiple multi-component image files. The command is a mixture of the **-omc** and **-oo** commands. There must be a multiple of 'n_comp' images on the stack. Every consecutive 'n_comp' images on the stack will be written to a separate multi-component image. ### Commands: Stack Manipulation and Flow Control These commands are used to manipulate the **convert3d** stack. The stack is a linear array of images. Every time an image is specified on the command line, it is loaded and placed at the end of the stack. Most operations take one image from the end of the stack, apply some operation to it, and place the result on the end of the stack. Certain commands like **-levelset** and **-reslice-matrix** take two images from the end of the stack as the input and replace them with a single output. Some other commands, like **-mean** and **-vote** take all images on the stack and replace them with a single output. Sometimes, for complex operations, it is useful to change the order of the images on the stack, to duplicate images, or to execute the same command multiple times. The stack manipulation and flow control commands allow you to complete complex tasks without saving intermediate images to the disk. #### -accum, -endaccum: Accumulate operations over all images Syntax: `-accum command-list -endaccum` Apply a binary operation (such as addition or multiplication) to all the images on the stack in a cumulative fashion. The command(s) will be applied to the last and second-to-last images on the stack, then to the result of this operation and the third-to-last image on the stack and so on. Below is the example of using the command to add multiple images. c3d image*.nii -accum -add -endaccum -o sum.nii #### -as: Assign image at the end of the stack to a variable Syntax: `-as var` Associates the image currently at the end of the stack with variable name 'var'. This allows you to retrieve the image later on the command line using the **-push** command. The **-as** and **-push** commands are useful when you need to use a certain image more than once during a convert3d operation. For example, if you want to compute the distance transform of a binary image and mask it so that the values outside of the binary image region have value 0, you would use the following command: c3d binary.img -as A -sdt -push A -times -o masked_distance.img #### -clear: Clear the image stack Syntax: `-clear` Clears the image stack. Images assigned a name with the **-as** command will remain in memory. #### -foreach, -endfor: Loop commands over all images on the stack Syntax: `-foreach commands-list -endfor` This command forces the commands between **-foreach** and **-endfor** to be applied to every image on the stack. The main use of this command is to automate processing of multiple datasets. For example, c3d epi*.nii -foreach -smooth 3mm -endfor -oo epism%03d.nii #### -foreach-comp, -endfor: Loop commands over components of a multi-component image Syntax `-foreach-comp commands-list -endfor` This command runs the list of commands separately for each component of a set of multi-component images loaded with -mcs. This makes it possible to perform component-wise operations on multi-component images. For example, it can be used to average several multi-component images. If the image stack contains images *x1* *y1* *z1* *x2* *y2* *z2*, then the operations will be run on *[x1,x2]*, *[y1,y2]*, *[z1,z2]*. For example, if multi_1.nii to multi_10.nii are three-component images, then the mean three-component image is given by c2d -mcs multi_*.nii -foreach-comp -mean -endfor -omc multi_mean.nii #### -insert: Insert image anywhere in the stack Syntax: `-insert var pos` This command is similar to **-push**, but allows you to insert the image associated with 'var' at any position in the stack, counting from the end. When 'pos' is 0, the image is placed at the end of the stack (same as **-push**). When pos is one, the image is placed at the next-to-end position, and so on. #### -pop: Remove last image from the stack Syntax: `-pop` Removes the last image from the image stack. Images assigned a name with the **-as** command will remain in memory. #### -popas: Remove last image from the stack and assign to variable Syntax: `-popas var` Removes the last image from the stack, but also assigns it the name 'var', keeping the image in memory. Same as calling **-as** *var* followed by **-pop**. #### -push: Place variable at the end of the stack Syntax: `-push var` Places the image associated with variable name 'var' on end of the image stack. Variable names are assigned using the **-as** command. The **-as** and **-push** commands are useful when you need to use a certain image more than once during a convert3d operation. For example, if you want to compute the distance transform of a binary image and mask it so that the values outside of the binary image region have value 0, you would use the following command: c3d binary.img -as A -sdt -push A -times -o masked_distance.img #### -reorder: Rearrange images on the stack Syntax: `-reorder k` or `-reorder fraction` Rearranges images in the stack, such that images that are k positions apart become next to each other on the stack. In other words, if the original order of the images is 1, 2, ..., n, the new order of the images becomes 1, 1+k, 1+2k, ..., 2, 2+k, 2+2k, ..., k, 2k, ... n. Of course, n must be divisible by k. As an alternative to specifying k, you can specify a floating point number (i.e., **-reorder** 0.5), in which case k is obtained by multiplying n by the floating point number and rounding to the nearest integer. The following three commands are equivalent: c3d a1.nii a2.nii a3.nii a4.nii b1.nii b2.nii b3.nii b4.nii -reorder 4 ... c3d a1.nii a2.nii a3.nii a4.nii b1.nii b2.nii b3.nii b4.nii -reorder 0.5 ... c3d a1.nii b1.nii a2.nii b2.nii a3.nii b3.nii a4.nii b4.nii ... The **-reorder** command us useful when you specify two sets of images using wildcards and then want to perform pairwise operations on the images. For example c3d weight*.nii gray*.nii -reorder 0.5 -weighted-sum-voxelwise -o wsum.nii is equivalent to the command c3d weight1.nii gray1.nii weight2.nii gray2.nii ... -weighted-sum-voxelwise -o wsum.nii #### -dup: Duplicate the last image on the stack Syntax: `-dup` Duplicates the image at the end of the stack. This is equivalent to **-as var -push var**, but shorter. An example is when you want to pass an image as both arguments to a binary operator, e.g., computing the square of the image intensity: c3d input.img -dup -times -o square.img #### -pick: Select one or more images from the stack by index Syntax: `-pick [indices]` Replaces the image stack with one or more selected images. Images can be specified using 0-based indexing (0 is the first image on the stack, etc) or negative indexing (-1 is the last image on the stack). c3d -mcs rgb.nii.gz -pick -1 -o blue.nii.gz c3d -mcs rgb.nii.gz -pick 0 -o red.nii.gz c3d -mcs pick -1 -2 -3 -omc bgr.nii.gz ### Commands: Voxelwise Calculations #### -abs: Voxelwise absolute value Syntax: `-abs` Computes the absolute value of the voxels in the last image on the stack: c3d signed.nii.gz -abs -o absval.nii.gz #### -add: Voxelwise image addition Syntax: `-add` Adds the last two images on the stack, and places the sum at the end of the stack. # Add two images: x = a + b c3d a.img b.img -add -o x.img # Add three images, x = (a + b) + c in the first example, x = a + (b + c) in the second c3d a.img b.img -add c.img -add -o x.img c3d a.img b.img c.img -add -add -o x.img # Subtract two images, using -scale command: x = a - b c3d a.img b.img -scale -1 -add -o x.img #### -atan2: Voxelwise angle from sine and cosine Syntax: `-atan2` Computes the angle in radians from images containing sine and cosine. This is a voxel-wise operation. It requires two images on the stack (sine followed by cosine): c3d sin_theta.nii.gz cos_theta.nii.gz -atan2 -o theta.nii.gz #### -ceil: Round up image intensities Syntax: `-ceil ` Each image intensity is replaced by the smallest integer larger or equal to it c3d input.img -ceil -o output.img #### -clip: Clip image intensity to range Syntax: `-clip iMin iMax` Clips image intensities, so that the values below *iMin* are converted to *iMin* and values greater than *iMax* are converted to *iMax*. This is useful for eliminating hyperintensities in images. Values *iMin* and *iMax* are intensity specifications (see below). c3d mri.img -clip 1000 8000 -o mriclip01.img // Clips below and above c3d mri.img -clip -inf 8000 -o mriclip02.img // Clips above only c3d mri.img -clip -inf 95% -o mriclip03.img // Clips at 95th percentile #### -composite: Composite one image onto another Syntax: `-composite` Takes two images, A and B from the stack and for each pixel, returns the value from A if B is background at that pixel, or B if B is not background. c3d a.nii b.nii -composite -o c.nii #### -cos: Voxelwise cosine Syntax: `-sin` Replaces the last image on the stack with the cosine trigonometric operation applied to all voxels. Input must be in radians. #### -divide: Voxelwise image division Syntax: `-divide` Divides one image by another. For instance to compute C = A / B, use the command c3d A.img B.img -divide -o C.img Divison may generate infinite and not-a-number (NaN) values if B contains zeros. You can use **-replace** to get rid of these values c3d A.img B.img -divide -replace inf 1000 -inf -1000 NaN 0 -o C2.img #### -exp: Voxelwise natural exponent Syntax: `-exp` Computes exponent of each voxel in the last image on the stack. c3d input.img -exp -o output.img #### -erf: Standard error function Syntax: `-erf mu sigma` Computes the standard error function. This is useful for applying soft thresholds. The function computes y = erf((x - mu)/sigma). c3d input.img -erf 5 2 -o erf.img #### -floor: Round down image intensities Syntax: `-floor ` Each image intensity is replaced by the largest integer smaller or equal to it. c3d input.img -floor -o output.img To round each intensity to the closest integer, use c3d input.img -shift 0.5 -floor #### -log, -ln: Voxelwise natural logarithm Syntax: `-log` Computes natural logarithm of each voxel in the last image on the stack. #### -log10: Voxelwise base 10 logarithm Syntax: `-log10` Computes base 10 logarithm of each voxel in the last image on the stack. #### -max: Voxel-wise maximum of two images Syntax: `-max` Computes the voxel-wise maximum of two images. Can be used with the **-accum** command to compute maximum of all images. c3d i1.nii i2.nii -max -o max12.nii c3d i1.nii i2.nii i3.nii i4.nii -accum -max -endaccum -o max1234.nii #### -min: Voxel-wise minimum of two images Syntax: `-min` Computes the voxel-wise minimum of two images. Can be used with the **-accum** command to compute minimum of all images. c3d i1.nii i2.nii -min -o min12.nii c3d i1.nii i2.nii i3.nii i4.nii -accum -min -endaccum -o min1234.nii #### -mean: Mean of all images on the stack Syntax: `-mean ` Computes the mean of all the images on the stack. All images on the stack are replaced with the mean image. c3d image_*.nii -mean -o mean.nii #### -multiply, -times: Multiply two images Syntax: `-multiply` Multiply two images voxel-by-voxel. The operation is applied to the last two images on the stack. # Compute x = a * b c3d a.img b.img -multiply -o x.img # Compute x = a * (b + c) using add and -multiply c3d a.img b.img c.img -multiply -add -o x.img Combine with the **-dup** command to compute voxelwise square of the image # Compute x = a^2 c3d a.img -dup -multiply -o x.img #### -noise-gaussian, -noise: Apply additive Gaussian noise Syntax: `-noise-gaussian ` Adds Gaussian noise to an image with zero mean and standard deviation sigma. Please see [Noise simulation article][15] by G. Lehmann for details. c3d image.nii -noise-gaussian 5 -o noisy.nii #### -noise-poisson: Apply Poisson noise Syntax: `-noise-poisson ` Applies Poisson (shot) noise to an image with given scale. Please see [Noise simulation article][15] by G. Lehmann for details. c3d image.nii -noise-poisson 5 -o noisy.nii #### -noise-salt-pepper: Apply salt and pepper noise Syntax: `-noise-salt-pepper ` Applies salt and pepper noise to an image with given probability. Please see [Noise simulation article][15] by G. Lehmann for details. c3d image.nii -noise-salt-pepper 0.1 -o noisy.nii #### -noise-speckle: Apply Poisson noise Syntax: `-noise-speckle ` Applies Speckle noise to an image with given standard deviation. Please see [Noise simulation article][15] by G. Lehmann for details. c3d image.nii -noise-speckle 5 -o noisy.nii #### -otsu: Otsu's thresholding Syntax: `-otsu [number of classes] [number of histogram bins]` Applies the classical [Otsu's thresholding algorithm](https://en.wikipedia.org/wiki/Otsu%27s_method) to classify voxels into different labels. If zero arguments are given the original itkOtsuThresholdImageFilter is used which separate image foreground from background. This filter returns an image of zeros (background) and ones (foreground). c3d image.nii -otsu -o otsu.nii If one or more arguments are used then the more general itkOtsuMultipleThresholdsImageFilter filter is utilized. If is set to 1 then the filter acts as the classical Otsu's binary thresholding described above (but outputs the inverse mask, see note below). If higher are input, then it returns an multilabel image of zeros (background) and not zero (foreground) divided into different label ids. For the itkOtsuMultipleThresholdsImageFilter the can be set, if not set it defaults to 256. c3d image.nii -otsu 2 -o otsu2.nii c3d image.nii -otsu 3 200 -o otsu3-200.nii Note: the two commands '-otsu' and '-otsu 1' do not output the same result, they actually produce the inverse of each other. Instead the following two commands produce equal results. c3d image.nii -otsu -replace 0 1 1 0 -o otsu-inverse.nii c3d image.nii -otsu 1 -o otsu1.nii This behavior is intentional by the filter creators, as discussed on the [ITK mailing list](https://itk.org/pipermail/community/2016-April/011137.html). The no argument c3d -otsu option is kept for backwards compatibility. #### -reciprocal: Image voxelwise reciprocal Syntax: `-reciprocal ` Computes the reciprocal of an image. For instance to compute B = 1 / A, use the command c3d A.img -reciprocal -o B.img #### -replace: Replace intensities in image Syntax: `-replace I1 J1 I2 J2 ... ` Replace intensity I1 by J1, I2 by J2 and so on. Allowed values of intensity include **nan**, **inf** and **-inf**. c3d img1.img -replace 1 128 nan 0.0 -o img2.img #### -retain-labels: Retain labels in a label image Syntax: `-retain-labels I1 I2 ... IN` Assuming that the input is a multi-label segmentation image, this command keeps all labels specified in the list and replaces the remaining labels with the background value. c3d seg.nii -retain-labels 2 3 4 8 -o subseg.nii #### -rgb2hsv: Convert RGB image to HSV image Syntax `-rgb2hsv` Takes the last three images on the stack and treats them as red, green, and blue channels. Outputs three images corresponding to hue, saturation, value. To read color images you need the ***-msc*** command. c3d -mcs color.png -rgb2hsv -omc hsv.png #### -rms: Voxelwise vector norm Syntax: `-rms` Computes RMS (root mean square) of all images on the stack. The command takes the square of each image on the stack, adds all the squared images and takes the square root of the result. This is very useful for statistical operations. Images must have the same size. c3d img1.img img2.img img3.img img4.img -rms -o rms.img The equivalent of this command is c3d img1.img img2.img img3.img img4.img -foreach -dup -times -endfor \ -accum -add -endaccum -sqrt -o rms.img #### -scale: Scale intensity by constant factor Syntax: `-scale ` Multiplies the intensity of each voxel in the last image on the stack by the given factor. c3d img1.img -scale 0.5 -o img2.img #### -shift: Shift image intensity by constant Syntax: `-shift ` Adds the given constant to every voxel. c3d img1.img -shift 100 -o img2.img #### -sin: Sine Syntax: `-sin` Replaces the last image on the stack with the sine trigonometric operation applied to all voxels. Input must be in radians. #### -sqrt: Take square root of image Syntax: `-sqrt ` Computes square root of each voxel in the image. c3d input.img -sqrt -o output.img #### -stretch: Stretch image intensities linearly Syntax: `-stretch ` Stretches the intensities in the image linearly, such that u1 maps to v1 and u2 maps to v2. The linear transformation is applied to all intensities in the image, whether inside the range or not. For example, to map a floating point image with intensities in interval (0,1) to the full range of an unsigned short image, use c3d input.img -stretch 0.0 1.0 0 65535 -type ushort -o output.img #### -thresh, -threshold: binary thresholding syntax: `-thresh ` thresholds the image, setting voxels whose intensity is in the range [u1,u2] to vin and all other voxels to vout. values *u1* and *u2* are intensity specifications (see below). this means that you can supply values **inf** and **-inf** for u1 and u2 to construct a one-sided threshold. you can also specify *u1* and *u2* as percentiles. c3d in.img -threshold -inf 128 1 0 -o out.img c3d in.img -threshold 64 128 1 0 -o out.img c3d in.img -threshold 20% 40% 1 0 -o out.img #### -voxel-sum: Print sum of all voxel intensities Syntax: `-voxel-sum ` Print the sum of all voxels in the image. $ c3d image.img -voxel-sum Voxel Sum: 200923123 #### -voxel-integral: Print volume integral of all voxel intensities Syntax: `-voxel-integral` Like **-voxel-sum**, but multiplies the sum of voxel intensities by voxel volume. This is useful for computing volumes of objects represented by binary images. The result is in 'ml'. $ c3d image.img -voxel-integral Voxel Integral: 2341 #### -wsum, -weighted-sum: Weighed sum of images with constant weights Syntax: `-wsum weight1 weight2 ... weightN ` Computes weighted sum of the last N images on the stack. c3d image1.nii image2.nii image3.nii -wsum 0.2 0.7 0.1 -o wsum.nii This command is particularly useful for combining components in a multicomponent image. For example, for an RGB image, we can convert it to grayscale (using [ImageMagick][13] formula) as follows: c3d -mcs rgb.nii -wsum 0.29900 0.58700 0.11400 -o gray.nii #### -wsv, -weighed-sum-voxelwise: Weighed sum of images with spatially varying weights Syntax: `-wsv ` Computes weighted sum of N weight images and N scalar images. The images must be interleaved on the stack. All images on the stack are used. c3d weight1.nii image1.nii weight2.nii image2.nii weight3.nii image3.nii -wsv -o mysum.nii.gz The **-reorder** command can simplify loading the images: c3d weight*.nii image*.nii -reorder 0.5 -wsv -o mysum.nii.gz ### Commands: Image Header Manipulation #### -copy-transform: Copy header information Syntax: `-copy-transform` Copies the image header, specifically the image to physical space transform (origin, spacing, direction cosines), from the first image (reference) to the second image (target). This is best done with NIFTI images, which store this information well. In the example below, *out.nii* will have the same header as *first.nii* and the same intensities as *second.nii*. c3d first.nii second.nii -copy-transform -o out.nii #### -mbb, -match-bounding-box: Match bounding box of one image to another Syntax: `-mbb` Given two images on the stack (reference and target), sets the header of the target image so that the two images occupy the same physical space. The direction cosines of the target image are set to match the reference image. This command is related to '-copy-transform' but supports images of different size. c3d reference.nii target.nii -mbb -o out.nii #### -orient: Change image orientation Syntax: `-orient CODE` Set the orientation of the image using one of 48 canonical orientations. The orientation describes the mapping from the voxel coordinate system (i,j,k) to the physical coordinate system (x,y,z). In the voxel coordinate system, i runs along columns of voxels, j runs along rows of voxels, and k runs along slices of voxels. It is assumed (by the NIFTI convention) that the axes of the physical coordinate system run as follows: x from (L)eft to (R)ight, y from (P)osterior to (A)nterior, z from (I)nferior to (S)uperior. The CODE passed in is a three-letter code consisting of letters RLAPSI. Each letter describes the anatomical direction corresponding to the voxel coordinates (i,j,k). For example, code RAI means that i runs from Right to Left, j from Anterior to Posterior, and k from Inferior to Superior. c3d input.img -orient RAI -o output.img c3d input.img -orient SAL -o output.img This command has the same behavior as the 'Reorient Image' menu option in ITK-SNAP. #### -origin: Set image origin Syntax: `-origin vector ` Set the origin of the image. The origin is the world coordinate (in NIfTI coordinate space) of the center of the voxel (0,0,0) in the image. The origin should be specified in millimeters. c3d input.img -origin 100x100x100mm -o output.img #### -origin-voxel: Assign image origin to a voxel Syntax: `-origin-voxel vector ` Set the origin of the image by specifying the voxel coordinates of the center of the patient (RAS) coordinate system. The vector should be specified in voxel units. c3d input.img -origin-voxel 60x70x35 -o output.img c3d input.img -origin-voxel 50% -o output.img # image centered around origin #### -origin-voxel-coord: Set coordinate of specified voxel Syntax: `-origin-voxel-coord: ` This command updates the origin of the image such that the specifed voxel has the specified coordinate. For example, you can use the command to set the world coordinate (in NIFTI coordinate space) of the center voxel of the image, as follows: c3d input.nii -origin-voxel-coord: 50% 10x10x5mm -o output.nii #### -set-sform: Set the transform to physical space Syntax: `-set-sform ` Sets the Nifti sform of the last image on the stack to the 4x4 matrix provided. #### -spacing: Set voxel spacing Syntax: `-spacing ` Sets the voxel spacing of the image. This should always be a vector with positive components. For example, to set the spacing of the image to 1mm isotropic, use the command below. This command only changes the header of the image, not its contents. c3d img.nii -spacing 1x1x1mm -o out.img #### -swapdim: Reorder the coordinate axes of an image Syntax `-swapdim ` This command reorders the image axes (columns, rows, slices) to achieve a desired transformation between voxel space and physical space. The image remains exactly the same in physical space, but the encoding of the voxels in memory and on disk is changed to obtain the desired transformation. The transformation is specified as a three-letter 'RAI' code, as in the '''-orient''' command. c3d img.nii -swapdim ASL -info -o out.nii ### Commands: Image Processing The following commands invoke an action that is applied to images. Unary commands apply the action to the last image on the stack, binary commands apply to the last two images and so on. Commands are affected by options, which are listed separately. #### -ad, -anisotropic-diffusion: Perona-Malik anisotropic diffusion filter Syntax: `-ad conductance n_iter` Executes the Perona-Malik anisotropic diffusion algorithm on the image. This smoothes the image, but with edge preservation. *Conductance* is a number between 0 and 1 that determines how well edges are preserved. *n_iter* is the number of iterations, which affects the scale of the smoothing. c3d x.img -ad 0.1 100 -o ad.img #### -biascorr: Automatic MRI bias field correction Syntax: `-biascorr` Performs automatic bias field correction for MRI images. This feature uses the [N3 implementation in ITK by Dr. Tustison][4], based on the N3 algorithm by Sled et al. c3d mri.nii.gz -biascorr -o mricorr.nii.gz #### -alm, -align-landmarks: Align images based on landmark matching Syntax: `-alm dof outfile` Performs rigid or affine alignment between to sets of landmark images. A landmark image is an image where for every intensity value, the centroid of all voxels with that intensity represents a landmark. Landmarks can be created using the paintbrush tool in ITK-SNAP (they can be spheres, cubes, etc). The first image on the stack is the target/fixed/reference image, and the second is the moving image. The parameters are the degrees of freedom, which is a number (6 for rigid, 7 for rigid+scale, 12 for affine) and the output matrix file. In this example, we have images **fixed.nii* and **moving.nii** with corresponding landmark images. We use landmarks to align the moving image to the fixed: c3d fixed_landmarks.nii moving_landmarks.nii -alm 6 rigid.mat c3d fixed.nii moving.nii -reslice-matrix rigid.mat -o moving_resliced_to_fixed.nii #### -binarize: Convert image to binary Syntax: `-binarize` Converts an image to binary by mapping all background values (the background is 0 by default and can be changed by the option **-background**) to 0 and all non-background values to 1. The **-binarize** command is shorthand for the **-threshold** command. c3d test.img -binarize -o binary.img c3d -background 10 -binarize -o binary.img c3d test.img -threshold 10 10 0 1 // equivalent to above command #### -canny: Canny edge detector Syntax: `-canny ` Performs edge detection on the last image on the stack using the Canny filter. The parameters are a vector of standard deviations defining the scale of the edges detected and lower and upper thresholds for edge selection. See documentation on the [ITK Canny Filter][14]. #### -centroid: Report centroid of foreground voxels Syntax: `-centroid` Reports the centroid, in physical coordinates, of all foreground voxels in the image. c3d binaryimage.img -centroid // centroid of all non-0 voxels c3d grayimage.img -thresh 1000 7000 1 0 -centroid // centroid of all voxels in range 1000-7000 c3d labelimage.img -thresh 5 5 1 0 -centroid // centroid of all voxels with label 5 c3d labelimage.img -split -foreach -centroid -endfor // centroids of all labels (including 0) #### -centroid-mark: Mark the centroid of foreground voxels Syntax: `-centroid-mark