Hi and welcome back! Another powerful tool of the Google Earth Engine is the capability to classify images. It can be useful for a variety of cases, when you want to create a thematic map of a certain area rather than an image, or when you want to assess what land cover changes took place over a certain time period.
In the Earth Engine there are two ways to classify pixels of an image. You could do a supervised classification or an unsupervised classification. With a supervised classification you have a sample of training data.
These are points with spectral values, of which you know what type of land cover they are So for example a table like this, where I have a set of points with spectral values. And for each point I know what the land cover is. With this training data, a classification algorithm can determine the spectral characteristics for a certain land cover.
And then for each pixel in the image, it will determine what the most likely land cover is by comparing it with these spectral characteristics of the different land covers. The difference with unsupervised classification is whether you know what the land cover of the training data is or not. With unsupervised classification you don’t know the land use of the training data, and the algorithm tries to make a certain amount of distinct groups based on the spectral values.
Of course there are a lot of different algorithms to perform these tasks, but in this video I will only go over the principles of image classification, training the classifiers, validation of the results and so on. I will make another video where I will go into depth in the different algorithms that are available. When that is finished I will put a link on the screen, or in the comments.
If there is no link there yet at the time you are watching, you could subscribe to the channel or hit the bell icon to get notified once it is uploaded. Okey, First of all let’s find an image to perform a supervised classification on. This looks like a nice region.
I will use images from Landsat 8 but you could use any image source you like. I will filter the images based on this region take only images from the last 3 years and remove the images with a high cloud cover. It is perfectly possible to run an image classification on an entire collection, but as you can see, there can be differences in the reflectance values of different images.
Because images are taken over a different time period, atmospheric conditions might change the spectral values. To reduce this effect you could for example take the mean value of all images in the collection. This already strongly reduces this effect.
Now we have an image to classify. The next step is to create training data. So as mentioned already, the training data is simply a table with spectral data, and the known land cover type.
You could import this table from another source, or just create it in the Earth Engine itself. This is what i will show you When I look at the image, I see four common land cover types, We clearly see water, bare soil, vegetation and some built up areas. I will try to classify these.
To create the training data, I will create four individual feature collections, which I can merge into one list of training samples. I select the option feature collection, and add the attribute LC, for land cover, with a value 0 this zero will represent all water pixels. These values have to be numeric for the classification to work.
When sampling, it is important to spread your samples across the area and take as much of the variation within a land cover class. When working with low resolution images, also try to select pure pixels. Pixels that for 100% consist of that land cover you are sampling.
20 pixels should be enough for this example. This will not be enough for an accurate end result, but it will still show you the patterns. And I do exactly the same for the other three classes.
Okey, so now we have our training points for the different land cover classes To group them into one training data feature class, I simply merge the four feature classes together and when we print this out we indeed see 80 points with an attribute indicating their land cover. But for the classification algorithm we do not need the points and their land cover, we need a table of the land covers and their spectral values For this, the Earth Engine has a specific tool called sampleRegion(). When this is done you get a feature collection with each feature containing the spectral values and the land cover type of your training points.
After this step it is possible that you end up with less features than the amount of points you started with. In this case we started with 80 points, and still have 80 points. But if for example one training point did not have a value for one specific band the entire training point would be removed from the dataset If a lot of training data is missing because of this, you might want to figure out what band causes this and remove that from the selection.
Now that we have our feature set we can start and make the classifier. I create a new variable, and make it a classifier object. I just pick the first one and don’t bother with all the parameters.
The types of classifiers and the possible parameters are a subject for another video. Now we have our classifier, but this one not trained yet. For that we use the train() function.
The train function first requires features, These are our training samples we created already. Secondly it needs to know which attribute stores the data on the land cover type of the sample. Next are the input properties, this are the properties of the table that will be used to do the training.
You can choose to train only on a limited set of attributes, if for example the training data contains information not relevant for classification. Often your feature class will have a property called ‘bandOrder’ and this already indicates which attributes your training data has. By default, this list will be used as the list of attributes to train with.
I will add this however, because the landsat 8 images have a band dedicated to the quality of the image, but this is irrelevant for the training. And now that we have trained our classifier it is time to perform this classification on the entire map. When you use the classifier it is important that your image has the same band names as used in the classification.
So if your classification is trained on for example band “B4”, “B3” and “B2” It will look for those bands in the image. If they are not present, the classification will not work. But since we will do the classification on the same image we took the training samples from this won’t be a problem.
And then we display the classification on the map. The result is a single band image, with each value being a land cover type. I had land covers 0, 1, 2 and 3 for water, vegetation, soil and urban land covers, so these values on the map are the classifications.
You can display them with a palette of colors. If you search for an html color picker online you can easily find colors you like. I already have a palette ready for this.
you also want to set the minimum and maximum values to display from 0 to 3. And that's it! You can see it did great for most of the map.
It only had difficulties in distinguishing some bare soil types from urban land And now you can use this classifier object to classify other images as well, but because training samples are based on this area, it will not continue to work well if you stray too far from this region. And to wrap this up, I would like to show one final function you can use to validate the results of your classification. Because if you can’t validate the accuracy of your classification, How can you trust its results?
The function to do this is the confusion matrix. This function returns a matrix with the rows being pixels of the verification pixels. These are your training samples.
And the columns of the matrix are the classes in which these pixels where classified according to the classifier. In this case, because we did not have that many training points, all points were also correctly classified in the classification. But when you start training on a lot of data, which is what you should do, some of your training data might become classified as something else.
Or for example when you would try to classify between different types of vegetation, the classification will have a harder time and get some pixels wrong. In those cases, the confusion matrix is a useful tool for determining which classes are accurately classified and which not, or what the overall accuracy of your classification is. So that was a first introduction on how to do supervised classifications in the GEE.
I hope this could get you started. The next video will go further into depth about the different classification methods so I’ll see you there.