%%capture
#Librerías para tratamiento de datos
!pip install numpy
!pip install pandas
#Librería para visualización de imágenes
!pip install matplotlib
!pip install plotly
#Librería para lectura de archivos envi
!pip install spectral
#Librerías para tratamiento de datos
import numpy as np
import pandas as pd
#Librería para visualización de imágenes
import matplotlib.pyplot as plt
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly.express as px
#Librería para lectura de archivos envi
import spectral.io.envi as envi
from spectral import *
#from skimage import io
git_token = "glpat-6mmhJ5Y39dfXnpEwjcwv"
!git clone https://JhonnyOsorio:{git_token}@gitlab.com/JhonnyOsorio/Hyperspectral_Images_Analysis
#Reference image suelos
path_reference_hdr_suelos = "Hyperspectral_Images_Analysis/suelos/suelo_reference.hdr"
path_reference_raw_suelos = "Hyperspectral_Images_Analysis/suelos/suelo_reference.raw"
#Sample image suelos
path_sample_hdr_suelos= "Hyperspectral_Images_Analysis/suelos/suelo_sample.hdr"
path_sample_raw_suelos = "Hyperspectral_Images_Analysis/suelos/suelo_sample.raw"
#Sample paleta
path_sample_hdr_paleta = "Hyperspectral_Images_Analysis/paleta/paleta.hdr"
path_sample_raw_paleta = "Hyperspectral_Images_Analysis/paleta/paleta.raw"
#Sample_hojas
path_sample_hdr_hojas = "Hyperspectral_Images_Analysis/hojas/hojas.hdr"
path_sample_raw_hojas = "Hyperspectral_Images_Analysis/hojas/hojas.raw"
#Sample paleta
path_sample_hdr_hoja_españa = "Hyperspectral_Images_Analysis/hojas_españa/hoja_españa.hdr"
path_sample_raw_hoja_españa = "Hyperspectral_Images_Analysis/hojas_españa/hoja_españa.raw"
def features(path_hdr):
info_img= envi.read_envi_header(path_hdr)
features = ["Spectral Bands","Resolution","Points"]
values = [info_img["bands"],info_img["samples"] + "x" + info_img["lines"],int(info_img["samples"])*int(info_img["bands"])*int(info_img["lines"])]
df_feature = pd.DataFrame({"features":features,"values":values})
return df_feature.style.hide_index()
def wavelength(path_hdr):
#espectro_visible =[]
#infrarojo = []
info_img= envi.read_envi_header(path_hdr)
wavelength = [str(int(float(i))) for i in info_img["wavelength"]]
bands = [i for i in range(len(wavelength))]
data = pd.DataFrame({"bands":bands,"wavelength":wavelength})
return data.style.hide_index()
#path_reference_hdr
#path_sample_hdr
features(path_sample_hdr_hojas)
wavelength(path_sample_hdr_hojas)
def show_static_images(path_hdr,path_raw,snip=(0,None,0,None),cols=5):
img = envi.open(path_hdr,path_raw)
info_img= envi.read_envi_header(path_hdr)
rows = round(int(info_img["bands"])/cols)
image = img[snip[0]:snip[1],snip[2]:snip[3],:]
wavelength = [str(int(float(i))) for i in info_img["wavelength"]]
images = []
for i in wavelength:
globals()["img_"+i] = img.read_band(wavelength.index(i))[snip[0]:snip[1],snip[2]:snip[3]]
images.append("img_"+i)
fig, axs = plt.subplots(rows, cols, figsize=(15, 3*rows))
L_rows = []
for i in range(rows):
L_rows += cols*[i]
L_cols = rows*list(range(cols))
for i in images :
axs[L_rows[images.index(i)],L_cols[images.index(i)]].imshow(eval(i))
axs[L_rows[images.index(i)],L_cols[images.index(i)]].set_title("λ ="+i[4:])
show_static_images(path_reference_hdr_suelos,path_reference_raw_suelos,cols=5)
show_static_images(path_sample_hdr_hojas,path_sample_raw_hojas,cols=5)
def show_interactive_images(path_hdr,path_raw,snip=(0,None,0,None)):
img = envi.open(path_hdr,path_raw)
info_img= envi.read_envi_header(path_hdr)
data = img[:,:,:]
fig = px.imshow(data,binary_string=True,animation_frame=2, labels=dict(animation_frame="wavelength"),color_continuous_scale='viridis',range_color=[0,255])
fig.update_traces(hovertemplate="x: %{x} <br> y: %{y} <br> color: %{color[0]}")
fig.show()
show_interactive_images(path_reference_hdr_suelos,path_reference_raw_suelos)
show_interactive_images(path_sample_hdr_hojas,path_sample_raw_hojas)
def show_images_wavelength(path_hdr,path_raw,wavelength,snip=(0,None,0,None),interactive =False):
img = envi.open(path_hdr,path_raw)
info_img= envi.read_envi_header(path_hdr)
wl = [str(int(float(i))) for i in info_img["wavelength"]]
imagen = img.read_band(wl.index(wavelength))[snip[0]:snip[1],snip[2]:snip[3]]
if interactive == False:
return plt.imshow(imagen)
else:
return px.imshow(imagen,color_continuous_scale='viridis')
show_images_wavelength(path_sample_hdr_hojas,path_sample_raw_hojas,wavelength="782",interactive=True)
def unfolding(path_hdr,path_raw,snip=(0,None,0,None), matrix= False):
array_data = []
img = envi.open(path_hdr,path_raw)
info_img= envi.read_envi_header(path_hdr)
wavelength =[str(int(float(i))) for i in info_img["wavelength"]]
images = []
for i in wavelength:
globals()["img_"+i] = img.read_band(wavelength.index(i))[snip[0]:snip[1],snip[2]:snip[3]]
images.append("img_"+i)
for i in images:
col = eval(i).flatten()
array_data.append(col)
data = np.stack(array_data, axis=1)
df = pd.DataFrame(data).set_axis(wavelength, axis=1)
if matrix == True:
return data
else:
return df
unfolding(path_reference_hdr_suelos,path_reference_raw_suelos)
This chart is empty
Chart was probably not set up properly in the notebook
unfolding(path_sample_hdr_hoja_españa,path_sample_raw_hoja_españa)
def wavelength_histogram(path_hdr,path_raw,wavelength):
img = envi.open(path_hdr,path_raw)
info_img= envi.read_envi_header(path_hdr)
wl = [str(int(float(i))) for i in info_img["wavelength"]]
banda = wl.index(wavelength)
save_rgb(wavelength+".png",img.read_band(banda),format='png')
data = io.imread(wavelength+".png")
fig = make_subplots(1, 2)
# We use go.Image because subplots require traces, whereas px functions return a figure
fig.add_trace(go.Image(z=data), 1, 1)
data_wavelength = unfolding(path_hdr,path_raw)[wavelength]
fig.add_trace(go.Histogram(x=data_wavelength,marker_color="#636EFA"), 1, 2)
fig.update_layout(height=450)
fig.show()
wavelength_histogram(path_sample_hdr_hojas,path_sample_raw_hojas,wavelength = "782")
import plotly.express as px
def histogram_wavelength(path_sample_hdr,path_sample_raw,wavelength):
info_img= envi.read_envi_header(path_sample_hdr)
wavelength = wavelength
data_wavelength = unfolding(path_sample_hdr,path_sample_raw)[wavelength]
fig = px.histogram(data_wavelength, x=wavelength)
fig.show()
histogram_wavelength(path_sample_hdr_hojas,path_sample_raw_hojas,wavelength = "782")
def clusters(path_hdr,path_raw,snip=(0,None,0,None),k=2,i=3, k_means= False):
img = envi.open(path_hdr,path_raw)
image = img[snip[0]:snip[1],snip[2]:snip[3],:]
info_img= envi.read_envi_header(path_hdr)
wavelength = info_img["wavelength"]
(m, c) = kmeans(image,k,i)
fig = go.Figure()
for i in range(c.shape[0]):
fig.add_trace(go.Scatter(go.Scatter(x =wavelength, y =c[i] )))
if k_means == False:
return px.imshow(m,color_continuous_scale='viridis')
else:
return fig.show()
clusters(path_sample_hdr_hojas,path_sample_raw_hojas,k=2,i=10)
clusters(path_sample_hdr_hojas,path_sample_raw_hojas,k=2,i=10,k_means= True)
def unfolding_filter(path_sample_hdr,path_sample_raw,wavelength,intensity,black_background=True,matrix=False):
data =unfolding(path_sample_hdr,path_sample_raw,matrix = True)
info_img= envi.read_envi_header(path_sample_hdr_hojas)
wl = [str(int(float(i))) for i in info_img["wavelength"]]
banda = wl.index(wavelength)
sd = []
if black_background==True:
condition = "data[i,banda] < intensity"
else:
condition = "data[i,banda] > intensity"
for i in range(len(data)):
if eval(condition):
sd.append(i+1)
else:
sd.append(0)
dep_data = []
for band in range(int(info_img["bands"])):
sub_data = []
for delt in range(len(sd)-1):
if sd[delt] == 0:
sub_data.append(data[delt,band])
dep_data.append(sub_data)
data_stack = np.stack(dep_data, axis=1)
df_depured = pd.DataFrame(data_stack).set_axis(wl, axis=1)
Data_Set_Reconstruction = []
for band in range(int(info_img["bands"])):
ReSub_Data_Set = []
for delt in range(0, len(sd)):
if sd[delt] == 0:
ReSub_Data_Set.append(data[delt,band])
else:
ReSub_Data_Set.append(0)
Data_Set_Reconstruction.append(ReSub_Data_Set)
if matrix == True:
return np.transpose(Data_Set_Reconstruction)
else:
unfolding_filter = np.transpose(Data_Set_Reconstruction)
df_unfolding_filter = pd.DataFrame(unfolding_filter).set_axis(wl, axis=1)
return df_unfolding_filter
unfolding_filter(path_sample_hdr_hojas,path_sample_raw_hojas,wavelength="920",intensity= 60 )
def image_without_background(path_sample_hdr,path_sample_raw,band,wavelength,intensity,black_background=True):
Data_Set_Reconstruction = unfolding_filter(path_sample_hdr=path_sample_hdr,path_sample_raw=path_sample_raw,wavelength=wavelength,intensity=intensity,black_background=black_background,matrix=True)
info_img= envi.read_envi_header(path_sample_hdr)
wl = [str(int(float(i))) for i in info_img["wavelength"]]
j = wl.index(band)
info_img= envi.read_envi_header(path_sample_hdr_hojas)
n = (np.transpose(Data_Set_Reconstruction).shape)[1]/int(info_img["samples"])
imgR = []
for i in range(int(info_img["bands"])):
imgr = np.stack(np.array_split(np.transpose(Data_Set_Reconstruction)[i],n))
imgR.append(imgr)
return px.imshow(imgR[j],color_continuous_scale='viridis')
image_without_background(path_sample_hdr_hojas,path_sample_raw_hojas,band = "920",wavelength="920",intensity= 60)
def reciprocal(i):
if i == 0:
return i
if i !=0:
return 1/i
def matrix_normalization(matrix_sam,matrix_ref):
reci = np.vectorize(reciprocal)
matrix_reciprocal = reci(matrix_ref)
matrix_hadamard = np.multiply(matrix_sam,matrix_reciprocal)
return matrix_hadamard
def dataframe_normalization(path_sample_hdr,path_sample_raw,path_reference_hdr,path_reference_raw,snip=(0,None,0,None),matrix=False):
info_img= envi.read_envi_header(path_sample_hdr,)
wl = [str(int(float(i))) for i in info_img["wavelength"]]
matrix_sample = unfolding(path_sample_hdr,path_sample_raw ,matrix = True)
matrix_reference = unfolding(path_reference_hdr,path_reference_raw,matrix = True)
matrix_normalized = matrix_normalization(matrix_sample,matrix_reference)
if matrix == True:
return matrix_normalized
else:
dataframe = pd.DataFrame(matrix_normalized).set_axis( wl , axis=1)
return dataframe
dataframe_normalization(path_sample_hdr_suelos,path_sample_raw_suelos,path_reference_hdr_suelos,path_reference_raw_suelos)
def histogram_normalization(path_sample_hdr,path_sample_raw,path_reference_hdr,path_reference_raw,wavelength):
info_img= envi.read_envi_header(path_sample_hdr)
wavelength = wavelength
data_wavelength = dataframe_normalization(path_sample_hdr,path_sample_raw,path_reference_hdr,path_reference_raw)[wavelength]
fig = px.histogram(data_wavelength, x=wavelength)
fig.update_xaxes(range=[0,1.5])
fig.show()
histogram_normalization(path_sample_hdr_suelos,path_sample_raw_suelos,
path_reference_hdr_suelos,path_reference_raw_suelos,"920")
def normalized_image(path_sample_hdr,path_sample_raw,path_reference_hdr,path_reference_raw,band,cube=False):
Data_Set_Reconstruction = dataframe_normalization(path_sample_hdr,path_sample_raw,path_reference_hdr,path_reference_raw,matrix=True)
info_img= envi.read_envi_header(path_sample_hdr_hojas)
n = (np.transpose(Data_Set_Reconstruction).shape)[1]/int(info_img["samples"])
imgR = []
for i in range(int(info_img["bands"])):
imgr = np.stack(np.array_split(np.transpose(Data_Set_Reconstruction)[i],n))
imgR.append(imgr)
data = np.stack(imgR, axis=2)
if cube == True:
return data
else:
info_img= envi.read_envi_header(path_sample_hdr)
wl = [str(int(float(i))) for i in info_img["wavelength"]]
j = wl.index(band)
return px.imshow(imgR[j],color_continuous_scale='viridis')
%%javascript
IPython.OutputArea.auto_scroll_threshold = 9999
normalized_image(path_sample_hdr_suelos,path_sample_raw_suelos,
path_reference_hdr_suelos,path_reference_raw_suelos,band = "920")
def show_interactive_images(path_sample_hdr,path_sample_raw,path_reference_hdr,path_reference_raw):
info_img= envi.read_envi_header(path_sample_hdr)
data = normalized_image(path_sample_hdr,path_sample_raw,path_reference_hdr,path_reference_raw,band = "920",cube=True)
fig = px.imshow(data,binary_string=True,animation_frame=2, labels=dict(animation_frame="wavelength"),color_continuous_scale='viridis',range_color=[0,1])
fig.update_traces(hovertemplate="x: %{x} <br> y: %{y} <br> color: %{color}")
fig.show()
show_interactive_images(path_sample_hdr_suelos,path_sample_raw_suelos,
path_reference_hdr_suelos,path_reference_raw_suelos)