2

I am trying to create an image which two kernels and then a conditional was applied to pixels such as:

IF P1>0 AND P2>0 AND P1 + P2 > 100 THEN 1
IF NOT 0

I want a new image with pixels values are only 0 or 1 after the condition is applied.

Here is my progress:

var sentinelCollection = ee.ImageCollection("COPERNICUS/S2_SR"),
    tRegion = /* color: #0b4a8b */ee.Geometry.Polygon(
        [[[28.182826834874845, 40.23986986920176],
          [28.147121268468595, 40.12970921265055],
          [28.227458792882658, 40.04355461665369],
          [28.421092826085783, 40.050913533775855],
          [28.525462943273283, 40.13075920541663],
          [28.462291556554533, 40.18953290914673],
          [28.415599662023283, 40.246159359987665]]]);

Map.centerObject(tRegion);

//Select images
var sentinel = ee.ImageCollection(sentinelCollection
  .filterBounds(tRegion)
  .filterDate('2019-02-01', '2019-02-28')
  .select(['B8','B11','B12'])
  .first();

// Create a list of weights for a 13x13 kernel. 45 degree1
var line1a1 =  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var line2a1 =  [0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0];
var line3a1 =  [0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 1, 0, 0];
var line4a1 =  [0, 0, 0, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0];
var line5a1 =  [0, 0, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0, 0];
var line6a1 =  [0, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0, 0, 0];
var centera1 = [0, 0, 0, 0, -1, 0, 1, 0, 0, 0, 0, 0, 0];
var line8a1 =  [0, 0, 0, -1, 0, 1, 0, 0, 0, 0, 0, 0, 0];
var line9a1 =  [0, 0, -1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0];
var line10a1 = [0, -1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var line11a1 = [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var line12a1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var line13a1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

var matrix45_a1 = [line1a1,line2a1,line3a1,
                   line4a1,line5a1,line6a1,
                           centera1,
                   line8a1,line9a1,line10a1,
                   line11a1,line12a1,line13a1];

// Create a list of weights for a 13x13 kernel. 45 degree2
var line1a2 =  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var line2a2 =  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var line3a2 =  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0];
var line4a2 =  [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0];
var line5a2 =  [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0];
var line6a2 =  [0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0];
var centera2 = [0, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0];
var line8a2 =  [0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0];
var line9a2 =  [0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 0];
var line10a2 = [0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 0, 0];
var line11a2 = [0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0];
var line12a2 = [0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var line13a2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

var matrix45_a2 = [line1a2,line2a2,line3a2,
                   line4a2,line5a2,line6a2,
                           centera2,
                   line8a2,line9a2,line10a2,
                   line11a2,line12a2,line13a2];

var kernela1 = ee.Kernel.fixed(13, 13, matrix45_a1);
print(kernela1);
var kernela2 = ee.Kernel.fixed(13, 13, matrix45_a2);
print(kernela2);

var convolvedA1 = sentinel.convolve(kernela1);
var convolvedA2 = sentinel.convolve(kernela2);

I want to apply abovementioned condition as P1 = convolvedA1, P2 = convolvedA2

How can I apply a condition to create new image?

1

I solved it but in case someone needs it, I'm sharing it as follows:

var conditionA1 = convolvedA1.gt(0)
var conditionA2 = convolvedA2.gt(0)
var conditionA12 = convolvedA1.updateMask(conditionA1)
                              .add(convolvedA2
                                   .updateMask(conditionA2))
                                               .gt(100)

(1) I created two binary images 1 IF Pixel > 0, 0 IF Pixel <= 0
(2) I masked values where Pixel = 0
(3) I added pixel values of two images. It creates a masked image where the pixels do not follow my first condition P1>0 and P2>0 are eliminated. So, my condition was basically changed to:

P1 + P2 IF P1>0 AND P2>0

(4) I applied the last condition IF P1 + P2 > 100, 1 IF NOT 0 with gt() method which returns 1 if pixels are greater than 100.

Any other approach is highly appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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