Announcement: All noncommercial projects registered to use Earth Engine before April 15, 2025 must verify noncommercial eligibility to maintain Earth Engine access.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-13 UTC."],[[["\u003cp\u003eClassifies an image using a pre-trained classifier.\u003c/p\u003e\n"],["\u003cp\u003eTakes an image and a classifier as input.\u003c/p\u003e\n"],["\u003cp\u003eReturns a classified image with a new band named "classification" (by default) containing the classification results.\u003c/p\u003e\n"],["\u003cp\u003eCan be used for various image classification tasks, such as land cover mapping.\u003c/p\u003e\n"]]],["The core process involves classifying an image using a trained classifier. First, a classifier is trained on a sample dataset with labeled land cover data. This training includes splitting the data into training and validation sets to assess the classifier's performance using error matrices and accuracy metrics. Finally, the trained classifier is applied to a reflectance image, generating a classified output. This output can then be visualized alongside the original image and the training data.\n"],null,["Classifies an image.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|----------------------------------------------|---------|\n| Image.classify`(classifier, `*outputName*`)` | Image |\n\n| Argument | Type | Details |\n|---------------|-----------------------------------|----------------------------------------------------------------------------------------------------------------------------------------|\n| this: `image` | Image | The image to classify. Bands are extracted from this image by name and it must contain all the bands named in the classifier's schema. |\n| `classifier` | Classifier | The classifier to use. |\n| `outputName` | String, default: \"classification\" | The name of the band to be added. If the classifier produces more than 1 output, this name is ignored. |\n\nExamples\n\nCode Editor (JavaScript) \n\n```javascript\n// A Sentinel-2 surface reflectance image, reflectance bands selected,\n// serves as the source for training and prediction in this contrived example.\nvar img = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG')\n .select('B.*');\n\n// ESA WorldCover land cover map, used as label source in classifier training.\nvar lc = ee.Image('ESA/WorldCover/v100/2020');\n\n// Remap the land cover class values to a 0-based sequential series.\nvar classValues = [10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 100];\nvar remapValues = ee.List.sequence(0, 10);\nvar label = 'lc';\nlc = lc.remap(classValues, remapValues).rename(label).toByte();\n\n// Add land cover as a band of the reflectance image and sample 100 pixels at\n// 10 m scale from each land cover class within a region of interest.\nvar roi = ee.Geometry.Rectangle(-122.347, 37.743, -122.024, 37.838);\nvar sample = img.addBands(lc).stratifiedSample({\n numPoints: 100,\n classBand: label,\n region: roi,\n scale: 10,\n geometries: true\n});\n\n// Add a random value field to the sample and use it to approximately split 80%\n// of the features into a training set and 20% into a validation set.\nsample = sample.randomColumn();\nvar trainingSample = sample.filter('random \u003c= 0.8');\nvar validationSample = sample.filter('random \u003e 0.8');\n\n// Train a 10-tree random forest classifier from the training sample.\nvar trainedClassifier = ee.Classifier.smileRandomForest(10).train({\n features: trainingSample,\n classProperty: label,\n inputProperties: img.bandNames()\n});\n\n// Get information about the trained classifier.\nprint('Results of trained classifier', trainedClassifier.explain());\n\n// Get a confusion matrix and overall accuracy for the training sample.\nvar trainAccuracy = trainedClassifier.confusionMatrix();\nprint('Training error matrix', trainAccuracy);\nprint('Training overall accuracy', trainAccuracy.accuracy());\n\n// Get a confusion matrix and overall accuracy for the validation sample.\nvalidationSample = validationSample.classify(trainedClassifier);\nvar validationAccuracy = validationSample.errorMatrix(label, 'classification');\nprint('Validation error matrix', validationAccuracy);\nprint('Validation accuracy', validationAccuracy.accuracy());\n\n// Classify the reflectance image from the trained classifier.\nvar imgClassified = img.classify(trainedClassifier);\n\n// Add the layers to the map.\nvar classVis = {\n min: 0,\n max: 10,\n palette: ['006400' ,'ffbb22', 'ffff4c', 'f096ff', 'fa0000', 'b4b4b4',\n 'f0f0f0', '0064c8', '0096a0', '00cf75', 'fae6a0']\n};\nMap.setCenter(-122.184, 37.796, 12);\nMap.addLayer(img, {bands: ['B11', 'B8', 'B3'], min: 100, max: 3500}, 'img');\nMap.addLayer(lc, classVis, 'lc');\nMap.addLayer(imgClassified, classVis, 'Classified');\nMap.addLayer(roi, {color: 'white'}, 'ROI', false, 0.5);\nMap.addLayer(trainingSample, {color: 'black'}, 'Training sample', false);\nMap.addLayer(validationSample, {color: 'white'}, 'Validation sample', false);\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\nColab (Python) \n\n```python\n# A Sentinel-2 surface reflectance image, reflectance bands selected,\n# serves as the source for training and prediction in this contrived example.\nimg = ee.Image(\n 'COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG'\n).select('B.*')\n\n# ESA WorldCover land cover map, used as label source in classifier training.\nlc = ee.Image('ESA/WorldCover/v100/2020')\n\n# Remap the land cover class values to a 0-based sequential series.\nclass_values = [10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 100]\nremap_values = ee.List.sequence(0, 10)\nlabel = 'lc'\nlc = lc.remap(class_values, remap_values).rename(label).toByte()\n\n# Add land cover as a band of the reflectance image and sample 100 pixels at\n# 10 m scale from each land cover class within a region of interest.\nroi = ee.Geometry.Rectangle(-122.347, 37.743, -122.024, 37.838)\nsample = img.addBands(lc).stratifiedSample(\n numPoints=100, classBand=label, region=roi, scale=10, geometries=True\n)\n\n# Add a random value field to the sample and use it to approximately split 80%\n# of the features into a training set and 20% into a validation set.\nsample = sample.randomColumn()\ntraining_sample = sample.filter('random \u003c= 0.8')\nvalidation_sample = sample.filter('random \u003e 0.8')\n\n# Train a 10-tree random forest classifier from the training sample.\ntrained_classifier = ee.Classifier.smileRandomForest(10).train(\n features=training_sample,\n classProperty=label,\n inputProperties=img.bandNames(),\n)\n\n# Get information about the trained classifier.\ndisplay('Results of trained classifier', trained_classifier.explain())\n\n# Get a confusion matrix and overall accuracy for the training sample.\ntrain_accuracy = trained_classifier.confusionMatrix()\ndisplay('Training error matrix', train_accuracy)\ndisplay('Training overall accuracy', train_accuracy.accuracy())\n\n# Get a confusion matrix and overall accuracy for the validation sample.\nvalidation_sample = validation_sample.classify(trained_classifier)\nvalidation_accuracy = validation_sample.errorMatrix(label, 'classification')\ndisplay('Validation error matrix', validation_accuracy)\ndisplay('Validation accuracy', validation_accuracy.accuracy())\n\n# Classify the reflectance image from the trained classifier.\nimg_classified = img.classify(trained_classifier)\n\n# Add the layers to the map.\nclass_vis = {\n 'min': 0,\n 'max': 10,\n 'palette': [\n '006400',\n 'ffbb22',\n 'ffff4c',\n 'f096ff',\n 'fa0000',\n 'b4b4b4',\n 'f0f0f0',\n '0064c8',\n '0096a0',\n '00cf75',\n 'fae6a0',\n ],\n}\nm = geemap.Map()\nm.set_center(-122.184, 37.796, 12)\nm.add_layer(\n img, {'bands': ['B11', 'B8', 'B3'], 'min': 100, 'max': 3500}, 'img'\n)\nm.add_layer(lc, class_vis, 'lc')\nm.add_layer(img_classified, class_vis, 'Classified')\nm.add_layer(roi, {'color': 'white'}, 'ROI', False, 0.5)\nm.add_layer(training_sample, {'color': 'black'}, 'Training sample', False)\nm.add_layer(\n validation_sample, {'color': 'white'}, 'Validation sample', False\n)\nm\n```"]]