TransWikia.com

Normalized difference of all bands

Geographic Information Systems Asked by Asad Delsouzkhaki on December 7, 2020

How I can get the normalized difference of all bands of an image with a function?
to qualify it; I want to gain normalized difference of b(i) with b(j) if j>i and i start from 1.
I want to stack the results.

One Answer

This is the way I've been doing this:

var image = ee.Image('COPERNICUS/S2/20200101T100319_20200101T100321_T32TQM')
var normalizedDifferences = combinePairwise(image, normalizedDifference, '_nd')

print(normalizedDifferences)
Map.addLayer(normalizedDifferences.select('B3_B4_nd'), {min: -1, max: 1})
Map.centerObject(image, 10)


function normalizedDifference(img1, img2) {
  return img1.expression('(img1 - img2) / (img1 + img2)', {
    img1: img1, 
    img2: img2
  })
}


function combinePairwise(image, algorithm, suffix) {
  suffix = suffix || ''
  return ee.Image(image.bandNames().iterate(function (b1, accImage) {
    b1 = ee.String(b1)
    accImage = ee.Image(accImage)
    var img1 = image.select(b1).rename('img1')
    var i1 = image.bandNames().indexOf(b1)
    var combinations = ee.Image(image.bandNames().slice(i1.add(1)).iterate(function (b2, accImage) {
      b2 = ee.String(b2)
      accImage = ee.Image(accImage)
      var img2 = image.select(b2).rename('img2')
      return accImage.addBands(
        algorithm(img1, img2)
          .rename(b1.cat('_').cat(b2).cat(suffix || ''))
      )
    }, ee.Image([])))
    return accImage.addBands(combinations)
  }, ee.Image([])))
  return segment.select('ndfi_.*')
    .addBands(segment.select('.*_avg'), null, true)
    .addBands(segment.expression('s.numObs / (s.tEnd - s.tStart)', 
      {s: segment}).rename('densityObs')
    )
    .addBands(nd)  
} 

https://code.earthengine.google.com/a7e3725c220ac7f9c21a82e94bfe2f56

Answered by Daniel Wiell on December 7, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP