I have two GeoTiff images that will represent training datasets classes that will be fed to a supervised classifier.

As opposed to this classification example where the geographic regions are extracted from a fusion table and the overlaid on the image, I don't have a fusion table so I'm going to use an image with its values directly as a training dataset.

My question is how can I transform an Image object into an array (or a FeatureCollection if I follow the example linked above)?


I've managed to convert my two training datasets rasters into a KML vector, and from there I've uploaded them to Google Fusion Tables (See in the code below class1 and class2). The two raster images to train and classify (i.e. image1 and image2 in the source code) were uploaded as assets; while the first one was used to train the classifier, the second one has to be classified. The problem with the code below is that all the pixels in the second image were classified in the same class (class = 0):

// load vector masks from fusion tables
var class1 = ee.FeatureCollection('ft:xxx');
var class2 = ee.FeatureCollection('ft:yyy');
var classes = class1.merge(class2);

// load training image and the image to classify
var image1 = ee.Image('aaa').select('b1');
var image2 = ee.Image('bbb').select('b1');

// train the classifier
var training = image1.sampleRegions({
    collection: classes,
    properties: ['class'],
    scale: 1
var classifier = ee.Classifier.svm().train(training, 'class', ['b1']);
var classified_image2 = image2.classify(classifier);

// show classified image
Map.addLayer(classified_image2, {}, 'Classified image');

Assuming you combine your two training images into one training image (in this combined image, each pixel can be one class, and classes are represented by consecutive integers starting from 0), you then add predictor bands and sample() the stacked image.

  • Thanks very much for the response. I made some progress since I've asked that question (I've edited the question with the current source-code). There is still an issue to solve.
    – Hakim
    Nov 6 '17 at 20:26

This problem of misclassification was solved by using the stratifiedSample() method instead of the sampleRegions() method during the training of the classifier, as can be in the code below. This new sampling method allows to avoid the Out of memory error (which is caused by decreasing the scale parameter to match the image resolution, and therefore increasing the number of samples) by specifying the number of pixels to sample. However, its classBand parameter needs to be provided as a band of image1 (That's why the classes raster was added as a band to image1), not as a FeatureCollection as before:

// training samples
var classes = ee.Image('<resource>').byte().rename('class');
var training = image1.addBands(classes).stratifiedSample({
  classBand: 'class',
  scale: 2.2,
  numPoints: 50000

// training & classification
var classifier = ee.Classifier.cart().train(training, 'class', ['b1']);
var classified_image2 = image2.classify(classifier);

Also, the CART classifier was used instead of the SVM as the former converges quicker.

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.