# Create NDVI image and recode

Dear all,

What I want to do sounds simple, but I am not sure, what the fastest option would be.
I have several thousand 4channel images for which I want to calculate the NDVI and then recode the images based on thresholds into several classes, e.g.
NDVI<0 --> class value 1
0=<NDVI<=0.4 --> class value 2
NDVI >0.4 -->class value 3
My approach would be to caclulate the NDVI image first using “otbcli_RadiometricIndices” first.
But how to perform the recode? Can I apply multiple conditions in BandMath/BandMathx?

Any hints are well appreciated
Oliver

Hello Oliver,

to apply a condition with `BandMath` and `BandMathX` the syntax is

`"logical expression ? value if true : value if false"`

For example `im1b1 < 0 ? 1 : 2`

It is possible to chain this expression to apply several condtions at once, in your case, if im1b1 is the NDVI (for example computed by RadiometricIndices) :

`-exp "im1b1 < 0 ? 1 : im1b1 <= 0.4 ? 2 : 3"`

If you are looking for performances I think the most efficient way to compute your classes would be to chain RadiometricIndices and BandMath in memory in a python script to avoid writing the NDVI on disk (see https://www.orfeo-toolbox.org/CookBook-develop/PythonAPI.html#in-memory-connection).

You can also try to compute the NDVI directly in the BandMath Expression, but you’ll need to compute it twice (one for each expression), so I think it would be less efficient. (in an expression you can use `ndvi(im1b1, im1b2)` where b1 is the red band and b2 is the NIR band).

Cédric

Thanks Cedric,

for your fast reply. Sound like the way to go, I will try.
How would I set the condition for a value between two values? With a logical AND?

`-exp "im1b1 < 0 ? 1 : im1b1 <= 0.4 && im1b1 >= 0 ? 2 : 3"`

Yes,

(but in this case this is not needed)

Cédric