Re: please help with ndvi calculation [message #52832] |
Thu, 01 March 2007 21:26 |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
beardown911@gmail.com writes:
> I am new in idl programming, and have been trying to figure out to
> write a simple code to calculate ndvi.
>
> Here is what I've done, but gave weird value.
> ------------------------------------------------------
> pro ndvi
> image = read_tiff ('/rsi/idl62/training/data/image.tif')
> help, image
>
> sub = float(image[0,*,*]-image[1,*,*])
> sum = float(image[0,*,*]+image[1,*,*])
>
> ndvi_image = sub / sum
>
> write_tiff, '/rsi/idl62/training/data/ndvi_test.tif', ndvi_image
> end
> -------------------------------------------------------
> The image.tif have three bands(nir, red, green) 8 bit image.
> The resulting ndvi image is still integer and values are totally
> wrong.
> Could anybody help me to fix this?
Cast your image to float *before* you do the calculations.
You are casting the *result* to a float, but the damage
has already been done when you did the calculations in
BYTE or INTEGER math.
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
Re: please help with ndvi calculation [message #52834 is a reply to message #52832] |
Thu, 01 March 2007 21:23  |
Pierre V.
Messages: 13 Registered: December 2006
|
Junior Member |
|
|
Phil,
My guess is that the function write_tiff is internally converting your
NDVI to type byte before writing it to a file. You can either scale
your data to properly fit into bytes using something like:
ndvi_byte = bytscl(ndvi_image, min = -1, max = 1).
Or you can try using the /float keyword to write_tiff to force it to
write out your data as floats.
-Pierre
|
|
|