Spectral Image
This example demonstrates how to load and inspect a spectral image using the SiaPy library:
- Load a spectral image.
- Access various properties and metadata.
from pathlib import Path
import spectral as sp
from siapy.entities import SpectralImage
# Set the path to the directory containing the data
# !! ADJUST THIS PATH TO YOUR DATA DIRECTORY !!
data_dir = "./docs/examples/data"
# Find all header and image files in the data directory
header_paths = sorted(Path(data_dir).rglob("*.hdr"))
image_paths = sorted(Path(data_dir).rglob("*.img"))
header_path_img0 = header_paths[0]
image_path_img0 = image_paths[0]
# Load the image using spectral library and then wrap over SpectralImage object
sp_file = sp.envi.open(file=header_path_img0, image=image_path_img0)
assert not isinstance(sp_file, sp.io.envi.SpectralLibrary)
image = SpectralImage(sp_file)
# or you can do the same just by running
image = SpectralImage.envi_open(
header_path=header_path_img0,
image_path=image_path_img0,
)
# Now you can easily use various property and util functions of the SpectralImage object
# Get the shape of the image
print("Image shape:", image.shape)
# Get the number of bands
print("Number of bands:", image.bands)
# Get the wavelength information
print("Wavelengths:", image.wavelengths)
# Get the file path
print("File path:", image.filepath)
# Get the metadata
print("Metadata:", image.metadata)
# Get the number of rows
print("Number of rows:", image.rows)
# Get the number of columns
print("Number of columns:", image.cols)
# Get the default bands
print("Default bands:", image.default_bands)
# Get the description
print("Description:", image.description)
# Get the camera ID
print("Camera ID:", image.camera_id)
# Get the geometric shapes
print("Geometric shapes:", image.geometric_shapes)
Source: spectral_image_01.py
This example demonstrates how to perform various operations on a spectral image:
- Convert a spectral image to a NumPy array.
- Calculate the mean value per band.
- Create a
Pixels
object from pixel coordinates. - Extract spectral signatures and subarrays.
- Display the spectral image.
from pathlib import Path
import matplotlib.pyplot as plt
from siapy.entities import Pixels, SpectralImage
# Set the path to the directory containing the data
# !! ADJUST THIS PATH TO YOUR DATA DIRECTORY !!
data_dir = "./docs/examples/data"
# Get first image
header_path_img0 = sorted(Path(data_dir).rglob("*.hdr"))[1]
image_path_img0 = sorted(Path(data_dir).rglob("*.img"))[1]
# Load spectral image
image = SpectralImage.envi_open(
header_path=header_path_img0,
image_path=image_path_img0,
)
# Convert to numpy
image_np = image.to_numpy(nan_value=0.0)
print("Image shape:", image_np.shape)
# Calculate mean
mean_val = image.mean(axis=(0, 1))
print("Mean value per band:", mean_val)
# Create a Pixels object from an iterable with pixels coordinates
# The iterable should be a list of tuples representing (x, y) coordinates
# iterable == [(x1, y1), (x2, y2), ...] -> list of pixels
iterable = [(1, 2), (3, 4), (5, 6)]
pixels = Pixels.from_iterable(iterable)
# Convert the pixel coordinates to spectral signatures
signatures = image.to_signatures(pixels)
print("Signatures:", signatures)
# Extract a subarray from the image using the pixel coordinates
subarray = image.to_subarray(pixels)
print("Subarray shape:", subarray.shape)
# Convert to displayable image
display_image = image.to_display(equalize=True)
# Display the image using matplotlib
plt.figure()
plt.imshow(display_image)
plt.axis("off") # Hide axes for better visualization
plt.show()
Source: spectral_image_02.py