Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save om-henners/c6c8d40389dab75cf535 to your computer and use it in GitHub Desktop.
Save om-henners/c6c8d40389dab75cf535 to your computer and use it in GitHub Desktop.
Example classifying raster imagery using scikit-learn for imagery classification
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Unsupervised classification of imagery using `scikit-learn`\n",
"\n",
"\n",
"This example shows how to classify imagery (for example from LANDSAT) using [`scikit-learn`](http://scikit-learn.org/stable/). There are many classification methods available, but for this example we will use [K-Means](http://scikit-learn.org/stable/modules/clustering.html#k-means) as it's simple and fast. For imagery I grabbed the [North Carolina dataset](http://grassbook.org/datasets/datasets-3rd-edition/) raster sample and I'm using the red, green and blue bands of the landsat 7 imagery within this pacakge. I'm using [`rasterio`](https://github.com/mapbox/rasterio) to read in the data."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"import rasterio\n",
"import sklearn.cluster"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"red_path = r\"C:\\projects\\quick_scripts\\gis_se\\landsat\\ncrast\\lsat7_2002_30.tif\"\n",
"green_path = r\"C:\\projects\\quick_scripts\\gis_se\\landsat\\ncrast\\lsat7_2002_20.tif\"\n",
"blue_path = r\"C:\\projects\\quick_scripts\\gis_se\\landsat\\ncrast\\lsat7_2002_10.tif\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(3L, 475L, 527L)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"with rasterio.open(red_path) as red, rasterio.open(green_path) as green, rasterio.open(blue_path) as blue:\n",
" data = np.array([red.read(1), green.read(1), blue.read(1)])\n",
"\n",
"data.shape # Note that this is a three band image giving us a 3 dimensional array."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.image.AxesImage at 0x155ef898>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment