1

I have images of plants taken in Red(R) and Near End Infrared(NIR) field,

I calculate the NDVI image per pixel using the below formula,

      NDVI = (R-NIR)/(R+NIR) , for all pixels,

I get a range from [-1,1].

Here NDVI image is used to separate soil from plant.

Now How do I convert it to a range from 0 to 255(unsigned int) for image processing.

Any help ?

EDIT

I am coding in C++ and I want a formula that does the conversion so that I can code it.I use OpenCV for image processing. I am not using any tool to the conversion.

3
  • What GIS software are you using?
    – BERA
    May 29, 2017 at 6:27
  • I am not using any tool...I am actually doing machine learning for agriculture and coding in C++.
    – Nitron_707
    May 29, 2017 at 6:32
  • Why do you want to rescale the image to 8-bit? This is not necessary for analysis and historically was only done to save disk space. Apr 2, 2018 at 21:47

2 Answers 2

4

the general equation for rescaling is

(pixel_value - min_value)/(max_pixel_value - min_pixel_value)*(max_rescale_value-min_rescale_value)+min_rescale_value

in your example, this would be

(pixel_value - (-1))/(1 - (-1))*(255 - 0)+0

that is #include char rescaledPixelValue(0); rescaledPixelValue = (char)round(255*(pixel_value +1 )/2);

You could also optimize the rescaling by using the "real" min and max values of your image, but then i) you need to read all your image once to find out these values and ii) you will not be able to compare 2 different images.

You could also get more details in the useful range by only converting [0-1] into [0-255]

char rescaledPixelValue(0); 
rescaledPixelValue = (pixel_value<0)?0:((char)round(255*pixel_value));

there is no risk to have vegetation with a negative NDVI, so you keep more precision when converting to uint8.

PS: you could look at OTB, it's a nice C++ library including this type of tools.

0
2

Since you didn't specify any software, I can propose to use gdal_translate to rescale your data from Min:-1, Max:1 to Min:0, Max:255.

gdal_translate -of GTiff -ot UInt16 -scale -1 1 0 255 F:\Path\To\Input\Test.tif F:\Path\To\Input\Test_Output.tif

If you have QGIS installed in your machine you can access gdal_translate from Raster Menu -> Conversion -> Translate (Convert format), then customize the command by click on the pencil.

enter image description here

1
  • I am not using any tool. I am coding in C++ and using OpenCV for Image processing. Can you ,Please check the edits once, i have edited the question.
    – Nitron_707
    May 29, 2017 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.