Misc584
Misc584

Reputation: 357

Convert RGB image to single band grayscale image within Google Earth Engine using Python API

I wish to extract summary statistics on the texture of a set of RGB satellite images within Google Earth Engine (GEE) using a gray-level co-occurrence matrix (GLCM). GEE has a built in image.glcm() function to do this, however the example code from this page (https://developers.google.com/earth-engine/image_texture) suggests it requires a single band as input:

// Load a high-resolution NAIP image.
var image = ee.Image('USDA/NAIP/DOQQ/m_3712213_sw_10_1_20140613');

// Get the NIR band.
var nir = image.select('N');

// Compute the gray-level co-occurrence matrix (GLCM), get contrast.
var glcm = nir.glcmTexture({size: 4});
var contrast = glcm.select('N_contrast');
Map.addLayer(contrast,
             {min: 0, max: 1500, palette: ['0000CC', 'CC0000']},
             'contrast');

Is there a way to convert an RGB image into a single band grayscale image within GEE?

I am using the Python API, so answers in Python would be ideal, but any advice would be much appreciated!

Upvotes: 0

Views: 1926

Answers (1)

Misc584
Misc584

Reputation: 357

OK, I figured out a method. This paper (https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3254613/) assessed the performance of different RGB to Grayscale conversions and found that Luminance performed particularly well for texture recognition and moderately well for object detection and so is a good bet for GLCM analysis. Luminance is calculated as 0.3R + 0.59G + 0.11B.

I've put together some Python code to create a Luminance layer here:

image = ee.Image("COPERNICUS/S2/20160620T072622_20160620T075216_T36LYJ")

grayscale = image.expression(
      '(0.3 * R) + (0.59 * G) + (0.11 * B)', {
      'R': image.select(['B4']),
      'G': image.select(['B3']),
      'B': image.select(['B2'])
})

Here is a Java example that works in GEE Code Editor as well:

var image = ee.Image("COPERNICUS/S2/20160620T072622_20160620T075216_T36LYJ");

var grayscale = image.expression(
      '(0.3 * R) + (0.59 * G) + (0.11 * B)', {
      'R': image.select('B4'),
      'G': image.select('B3'),
      'B': image.select('B2')
});

Map.setCenter(35.524263, -14.955732, 9);
Map.addLayer(grayscale, {min:700, max:1300}, 'Grayscale');

Upvotes: 1

Related Questions