In [6]:
import numpy as np
from skimage import io
from matplotlib import pyplot as plt
classes = {'vegetation': 0, 'building': 1, 'water': 2}
n_classes = len(classes)
palette = np.uint8([[0, 255, 0], [255, 0, 0], [0, 0, 255]])
img = io.imread('https://i.stack.imgur.com/TFOv7.png')
rows, cols, bands = img.shape
print rows, cols, bands
In [9]:
io.imshow(img)
plt.show()
In [31]:
X = img.reshape(rows*cols, bands)
print X.shape
X
Out[31]:
In [29]:
# we put some labeling selecting by hand the bounding boxes
supervised = n_classes*np.ones(shape=(rows, cols), dtype=np.int)
supervised[200:220, 150:170] = classes['building']
supervised[40:60, 40:60] = classes['vegetation']
supervised[100:120, 200:220] = classes['water']
y = supervised.ravel()
print supervised.shape
print y.shape
In [14]:
train = np.flatnonzero(supervised < n_classes)
test = np.flatnonzero(supervised == n_classes)
from sklearn.svm import SVC
clf = SVC()
clf.fit(X[train], y[train])
y[test] = clf.predict(X[test])
supervised = y.reshape(rows, cols)
In [15]:
io.imshow(palette[supervised])
plt.show()