The key to working with
mrcalc in general, is to become used to working with “postfix” notation, also known as “Reverse Polish Notation” (RPN). The Wikipedia article on this is actually pretty decent, with a nice example; even though it mostly focuses on binary operators. Basically, rather than putting your operator in between your operands (“infix notation”), as you’re probably more used to, you put it after your operands instead. So rather than writing “5 + 3”, you write “5 3 +”. See the Wikipedia article to convince yourself if needed, but essentially one of the benefits is that you suddenly don’t need brackets anymore to write down any expression you can dream of. But instead, the position of your operators in the whole expression will implicitly define “where the brackets sit” for an equivalent infix notation. Also, postfix notation allows in a more generalised way to have operators with different numbers of operands, e.g. unary, binary, ternary, etc… (whereas infix notation strictly only applies to binary operators, because the operator needs to sit between the first and the second operand). For your command, you might for instance be thinking of using the
-if operator: it’s basically an if-then-else operator that can check for something (1st operand), and then return the 2nd operand if the checked thing is true, or return the 3rd operand if the check thing is false.
The most explicit way to write your command I can think of (there’s many ways you could achieve the same result; but I personally prefer those that are as explicitly readable as possible), would be:
mrcalc dwi_denoised.mif 0 -lt 0 dwi_denoised.mif -if dwi_denoised_capped.mif
The first little bit up until and including the
-lt (“less than”) operator there compares the input image with 0, and returns true if the input image value is less than 0. The result of that (true or false) then is the 1st operand to
-if. If the test is returns true, the
-if will return the 2nd operand (0), and else it’ll return the 3rd operand (the input image value itself). That is then in the end stored in the output image, which always sits at the end of the expression. So basically, for a postfix-trained reader, this
mrcalc expression reads as “if dwi_denoised.mif is less than zero, return zero, else return dwi_denoised.mif”. Or zero-capping indeed.
Another, far shorter, way of achieving exactly the same thing though:
mrcalc dwi_denoised.mif 0 -max dwi_denoised_capped.mif
In some peoples’ minds, this immediately yells “capping”, but in others’, it doesn’t. Regardless, you might prefer it because it’s shorter. Up to personal taste, I’d say.