Google Earth Engine – Solving Issues in Applying Condition Expression to All Band Values

bandexpressiongoogle-earth-enginegoogle-earth-engine-javascript-apiimage classification

I want to apply the following condition statement to all the pixels of an indices image having only 1 band. The condition is as follows-

Condition I want to apply

For that first I have calculated the mean value of my image. Next I have printed the bands existing and then used the expression by substituting the band values.

var mean = MNDWI.reduceRegion(ee.Reducer.mean(),clip_layer,28);
print('mean', mean)

var band_names = MNDWI.bandNames();
print(band_names)

var index = MNDWI

var recode = ee.Image().expression(
'(mean > 0 and index >= mean) or ' +
'(mean <= 0 and index >= (mean**5 - 0.02)) ' +
'? 254 : 0', {
mean: mean, 
index: index
})

Map.addLayer(recode)

Here (MNDWI) represents the index, and "B3" is the single band that index is having which can be observed in the print statement output.

Print output values

But I am getting this error-

Layer error: Image.gt, argument 'image1': Invalid type.
Expected type: Image.
Actual type: Dictionary.
Actual value: {B3=-0.14653535282222163}

Error image

Best Answer

The error message is almost certainly accurate. MBUI probably doesn't have a B3 band. But your code isn't complete enough to say.

As for the expression, to do power, you don't use ^ but **. It also doesn't correctly implement your specified condition. You could perhaps implement it like this:

var mean = ee.Image(0)
var index = ee.Image(0)

var recode = ee.Image().expression(
  '(mean > 0 and index >= mean) or ' +
  '(mean <= 0 and index >= (mean**5 - 0.02)) ' +
  '? 254 : 0', {
  mean: mean, 
  index: index
})

Map.addLayer(recode)

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

Related Question