# Create NDVI image and recode

**URL:** <https://forum.orfeo-toolbox.org/t/create-ndvi-image-and-recode/767>\
**Category:** OTB Usage\
**Created:** [September 10, 2020, 8:49am UTC](https://forum.orfeo-toolbox.org/t/create-ndvi-image-and-recode/767 "2020-09-10T08:49:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bucol364](https://forum.orfeo-toolbox.org/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@bucol364](https://forum.orfeo-toolbox.org/u/bucol364)\
**Post date:** [September 10, 2020, 8:49am UTC](https://forum.orfeo-toolbox.org/t/create-ndvi-image-and-recode/767/1 "2020-09-10T08:49:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Cedric](https://forum.orfeo-toolbox.org/letter_avatar_proxy/v4/letter/c/ecd19e/32.png) [@Cedric](https://forum.orfeo-toolbox.org/u/Cedric)\
**Post date:** [September 10, 2020, 10:37am UTC](https://forum.orfeo-toolbox.org/t/create-ndvi-image-and-recode/767/2 "2020-09-10T10:37:04Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![bucol364](https://forum.orfeo-toolbox.org/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@bucol364](https://forum.orfeo-toolbox.org/u/bucol364)\
**Post date:** [September 10, 2020, 2:58pm UTC](https://forum.orfeo-toolbox.org/t/create-ndvi-image-and-recode/767/3 "2020-09-10T14:58:00Z")

</div>

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"`

---

<div class="post-metadata">

**Author:** ![Cedric](https://forum.orfeo-toolbox.org/letter_avatar_proxy/v4/letter/c/ecd19e/32.png) [@Cedric](https://forum.orfeo-toolbox.org/u/Cedric)\
**Post date:** [September 10, 2020, 4:17pm UTC](https://forum.orfeo-toolbox.org/t/create-ndvi-image-and-recode/767/4 "2020-09-10T16:17:43Z")

</div>

Yes,

(but in this case this is not needed)

Cédric
