!pip install spectral
Collecting spectral Downloading spectral-0.22.4-py3-none-any.whl (212 kB) |████████████████████████████████| 212 kB 37.0 MB/s Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from spectral) (1.19.5) Installing collected packages: spectral Successfully installed spectral-0.22.4
import matplotlib
matplotlib.rcParams['figure.figsize'] = (15,10)
%config InlineBackend.figure_format = 'retina'
!pip install spectral
Collecting spectral Downloading spectral-0.22.4-py3-none-any.whl (212 kB) |████████████████████████████████| 212 kB 4.1 MB/s Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from spectral) (1.19.5) Installing collected packages: spectral Successfully installed spectral-0.22.4
# Bibliotecas
import tifffile as tif
import matplotlib.pyplot as plt
from spectral import imshow
import numpy as np
# Lendo imagem
img = tif.imread('Cubo_SR.tif')
# Visualizando composição colorida
imshow(img, (2,1,0), stretch=(0.02), figsize=(15,10))
# Definindo espectros
mata = img[600,1400,:]
cerrado = img[600,600,:]
urbano = img[1000,200,:]
agua = img[500,700,:]
espectros = [mata, cerrado, urbano, agua]
# Visualizando epectros
bandas = ['Blue', 'Green', 'Red', 'NIR', 'SWIR-1','SWIR-2']
plt.plot(bandas,img[600,1400,:])
# Visualizando espectros separados
cores = ['Darkgreen', 'lime', 'Red', 'Blue']
nomes = ['Mata', 'Cerrado', 'Urbano', 'Água']
fig, ax = plt.subplots(4, 1, sharex=True, figsize=(15,10))
for i in range(len(espectros)):
ax[i].plot(bandas, espectros[i], color=cores[i])
ax[i].set_title(nomes[i])
# Visualizando espectros em uma única figura
fig2, ax2 = plt.subplots()
for i in range(len(espectros)):
ax2.plot(bandas, espectros[i], color=cores[i])
ax2.legend(nomes, loc="lower left")
# Calculando NDVI
ndvi = ( img[:,:,3] - img[:,:,2] ) / ( img[:,:,3] + img[:,:,2] )
# Visualizando de forma contínua
plt.imshow(ndvi, cmap='RdYlGn')
plt.colorbar()
from matplotlib import colors
# Visualizando de forma discreta
cmap = colors.ListedColormap(['red', 'yellow','orange','lime','darkgreen'])
norm = colors.BoundaryNorm(bounds, cmap.N)
bounds=[np.min(ndvi),0.03,0.25,0.5,0.75,np.max(ndvi)]
norm = colors.BoundaryNorm(bounds, cmap.N)
im = plt.imshow(ndvi,cmap=cmap, norm=norm)
plt.colorbar(im)
# Reclassificando ndvi
ndvi_reclass= ndvi.copy()
ndvi_reclass[ndvi <= 0.03] = 1
ndvi_reclass[(ndvi > 0.03) & (ndvi <= 0.25)] = 2
ndvi_reclass[(ndvi > 0.25) & (ndvi <= 0.50)] = 3
ndvi_reclass[(ndvi > 0.50) & (ndvi <= 0.75)] = 4
ndvi_reclass[(ndvi > 0.75)] = 5
plt.imshow(ndvi_reclass, cmap='RdYlGn')