TransWikia.com

"image" is not defined in this scope

Geographic Information Systems Asked on February 3, 2021

I am new to coding and need some help debugging…I am trying to classify a region in Peru using Sentinel 2. The error I am receiving is "’image’ is not defined in this scope." I believe my issues is on line 31 and the fact that I am working within a collection not an image…but I am still completely lost. Any tips?

var growth = ee.FeatureCollection('users/gtnatia/just_trees_training_2');
growth = growth.geometry();
Map.centerObject(growth);
Map.addLayer(growth, {color: 'red'}, 'growth');

//

var all_other = ee.FeatureCollection('users/gtnatia/all_other_training');
all_other = all_other.geometry();
Map.centerObject(all_other);
Map.addLayer(all_other, {color: 'black'}, 'all_other');


// Make a cloud-free Landsat 8 TOA composite (from raw imagery).
var sentinel2 = ee.ImageCollection('COPERNICUS/S2_SR')
                    .filterDate('2018-01-01', '2018-12-31')
                    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
                    .filterBounds(aoi);

// Use these bands for prediction.
var bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B10', 'B11'];

//

var polygons = ee.FeatureCollection([
  ee.Feature(growth, {'class': 0}),
  ee.Feature(all_other, {'class': 1}),
]);

// Get the values for all pixels in each polygon in the training.
var training = image.sampleRegions({
  // Get the sample from the polygons FeatureCollection.
  collection: polygons,
  // Keep this list of properties from the polygons.
  properties: ['class'],
  // Set the scale to get Landsat pixels in the polygons.
  scale: 30
});

// Create an SVM classifier with custom parameters.
var classifier = ee.Classifier.libsvm({
  kernelType: 'RBF',
  gamma: 0.5,
  cost: 10
});

// Train the classifier.
var trained = classifier.train(training, 'class', bands);

// Classify the image.
var classified = image.classify(trained);

// Display the classification result and the input image.
Map.setCenter(-72.355,-13.880, 9);
Map.addLayer(image, {bands: ['B4', 'B3', 'B2'], max: 0.5, gamma: 2});
Map.addLayer(polygons, {}, 'training polygons');
Map.addLayer(classified,
             {min: 0, max: 1, palette: ['red', 'green']},
             'supervised_classification');

One Answer

There were a few bugs in the code.

  1. "image" is not defined in the scope because there is no variable called "image" in your code. Looking closer at your comments, it seems like you wish to make a Sentinel-2 cloud-free composite image. So I created that by adding the following line right the variable sentinel2 is declared: var image = sentinel2.mean();. This line takes the imageCollection called Sentinel2 (which contains a stack of cloud-free images for the year 2018) and takes the mean of them. You can use .max(), .min() etc. if you wish
  2. "B10" is not part of Sentinel-2 surface reflectance product (B10 refers to high altitude clouds called Cirrus and hence it is not a part of surface reflectance). For a list of all bands in the product use print(image) or visit the documentation and click on bands. I removed "B10" from the bands list.
  3. The year 2018 did not have any images with less than 20% cloud cover so your classifier had zero training data. On running the classifier for the year 2019, it runs ok. You may still need to tweak the classifier parameters to get the desired output, I only mean that the classifier succeeds in training and gives an output.

Link to corrected code.

Answered by kkrao on February 3, 2021

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