سؤال

I have a script which calculates the Normalized Difference Vegetation Index (NDVI) from 4-band geotiffs and writes the NDVI as a geotiff. I am working with aerial imagery as 4-band geotiffs at 1m spatial resolution. The output NDVI geotiff ranges from -1 to 1 and is 32-bit floating point. How can I apply a linear stretch so that the output ranges from 0 - 255 in 8-bit unsigned integer format?

file = 'D:\create_ndvi\3711101_se1.tif';
[I R] = geotiffread(file);
outputdir = 'D:\create_ndvi\'

NIR = im2single(I(:,:,4));
red = im2single(I(:,:,1));

ndvi = (NIR - red) ./ (NIR + red);
double(ndvi);
imshow(ndvi,'DisplayRange',[-1 1]);

tiffdata = geotiffinfo(file);
outfilename = [outputdir 'ndvi_' 'temp' '.tif'];  
geotiffwrite(outfilename, ndvi, R, 'GeoKeyDirectoryTag', tiffdata.GeoTIFFTags.GeoKeyDirectoryTag) 
هل كانت مفيدة؟

المحلول

To spell out what's already in the comments:

ndvi = floor((ndvi + 1) * 128); % [-1 1] -> [0 256]
ndvi(ndvi < 0) = 0;             % not really necessary, just in case & for symmetry
ndvi(ndvi > 255) = 255;         % in case the original value was exactly 1
ndvi = uint8(ndvi);             % change data type from double to uint8
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top