In [1]:
%pylab inline
GDAL (Geospatial Data Abstraction Library) possui um módulo Python:
In [2]:
import gdal
Podemos utilizá-lo para manipular arquivos GeoTIFF:
In [3]:
bahia_data = gdal.Open('data/2010-07-03T142001_RE4_3A-NAC_6683686_113276.tif')
In [4]:
bahia_data.GetMetadata()
Out[4]:
In [5]:
bahia_data.GetDriver().ShortName
Out[5]:
In [6]:
bahia_data.GetDriver().LongName
Out[6]:
In [7]:
print bahia_data.RasterXSize
print bahia_data.RasterYSize
print bahia_data.RasterCount
In [8]:
bahia_data.GetProjection()
Out[8]:
In [9]:
geotransform = bahia_data.GetGeoTransform()
print 'Origin', (geotransform[0], geotransform[3])
print 'Pixel Size', (geotransform[1], geotransform[5])
In [10]:
B = bahia_data.ReadAsArray()
In [71]:
B.shape
Out[71]:
Da documentação do RapidEye, sabemos o significado das 5 bandas:
In [12]:
band_name = {0:'Blue', 1:'Green', 2:'Red', 3:'Red Edge', 4:'Near-Infrared'}
E seus comprimentods de onda:
In [14]:
band_range = {0:(440,510), 1:(520,590), 2:(630,685), 3:(690,730), 4:(760,850)}
In [15]:
bands = range(5)
for b in bands:
print 'Banda %d, %s, %s' % (b, band_name[b], band_range[b])
In [16]:
figsize(20,30)
for b in bands:
subplot(3,2,b+1)
imshow(B[b], interpolation='nearest', cmap=cm.hot)
colorbar()
title(band_name[b])
%whosdel
In [19]:
C = B.copy()
In [20]:
%whos
In [22]:
del(C)
%whos
Vamos reduzir a dimensionalidade dos dados com PCA (Principal Components Analysis):
In [24]:
from sklearn.decomposition import PCA
In [25]:
pca = PCA(n_components=1)
Vamos representar nossos $5000 \times 5000$ pixels como 25 milhões de pontos com 5 dimensões (as 5 bandas)
Primeiro, vamos usar dstack para "empilhar" as 5 bandas em uma imagem de $5000 \times 5000 \times 5$:
In [26]:
X = dstack([B[b] for b in bands])
X.shape
Out[26]:
Agora vamos redimensionar nossos dados $\mathtt{X}$ para $N$ elementos com 5 dimensões.
$N = 5.000 \cdot 5.000 = 25.000.000$
In [27]:
X = X.reshape(5000**2, -1)
X.shape
Out[27]:
PCA fitting:
In [29]:
pca.fit(X)
Out[29]:
Transformação:
In [30]:
Y = pca.transform(X)
Y.shape
Out[30]:
In [31]:
Y = Y.reshape(5000, 5000)
imshow(Y, cmap=cm.gray)
Out[31]:
Apenas para ilustrar o potencial do aprendizado de máquina em segmentação de imagens, vamos aplicar clustering K-médias à imagem:
In [33]:
from sklearn.cluster import KMeans
In [34]:
kmeans = KMeans(n_clusters=5)
In [48]:
kmeans.fit(X)
Out[48]:
In [50]:
labels = kmeans.labels_
labels.shape
Out[50]:
In [52]:
subplot(1,2,1)
imshow(Y, cmap=cm.gray)
subplot(1,2,2)
imshow(labels.reshape(5000, 5000))
Out[52]: