Transformations
This example demonstrates how to perform various transformations on spectral images:
- Select corresponding pixels in VNIR and SWIR images.
- Calculate the transformation matrix.
from pathlib import Path
from siapy.entities import SpectralImage
from siapy.transformations import corregistrator
from siapy.utils.plots import pixels_select_click
# 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("coregister*corr2_rad_f32.hdr"))[0]
image_path_img0 = sorted(Path(data_dir).rglob("coregister*corr2_rad_f32.img"))[0]
header_path_img1 = sorted(Path(data_dir).rglob("coregister*corr_rad_f32.hdr"))[0]
image_path_img1 = sorted(Path(data_dir).rglob("coregister*corr_rad_f32.img"))[0]
# Load VNIR and SWIR spectral images
image_swir = SpectralImage.envi_open(
header_path=header_path_img0,
image_path=image_path_img0,
)
image_vnir = SpectralImage.envi_open(
header_path=header_path_img1,
image_path=image_path_img1,
)
# Select the same pixels in both images.
# The more points you select, the better the transformation between image spaces will be.
# Click enter to finish the selection.
pixels_vnir = pixels_select_click(image_vnir)
pixels_swir = pixels_select_click(image_swir)
# Perform the transformation and transform the selected pixels from the VNIR image to the space of the SWIR image.
matx, _ = corregistrator.align(pixels_swir, pixels_vnir, plot_progress=False)
print("Transformation matrix:", matx)
Source: transformations_01.py
This example demonstrates how to apply the calculated transformation matrix to spectral images:
- Select areas on VNIR image.
- Apply the transformation matrix to the selected areas.
- Transform the selected pixels from VNIR to SWIR space.
- Display both images with areas.
from pathlib import Path
import numpy as np
from siapy.entities import SpectralImage
from siapy.transformations import corregistrator
from siapy.utils.plots import display_multiple_images_with_areas, pixels_select_lasso
# 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"))[0]
image_path_img0 = sorted(Path(data_dir).rglob("*.img"))[0]
header_path_img1 = sorted(Path(data_dir).rglob("*.hdr"))[1]
image_path_img1 = sorted(Path(data_dir).rglob("*.img"))[1]
# Load VNIR and SWIR spectral images
image_swir = SpectralImage.envi_open(
header_path=header_path_img0,
image_path=image_path_img0,
)
image_vnir = SpectralImage.envi_open(
header_path=header_path_img1,
image_path=image_path_img1,
)
# Transformation matrix was calculated in previous example
matx = np.array(
[
[5.10939099e-01, -3.05286868e-03, -1.48283389e00],
[-2.15777211e-03, 5.17836773e-01, -2.50694723e01],
[3.02412467e-18, 7.36518494e-18, 1.00000000e00],
]
)
# Select area of the image
# Click enter to finish the selection.
selected_areas_vnir = pixels_select_lasso(image_vnir)
# Transform the selected areas from the VNIR image to the space of the SWIR image.
selected_areas_swir = [
corregistrator.transform(pixels_vnir, matx) for pixels_vnir in selected_areas_vnir
]
# Display the selected areas in both images
display_multiple_images_with_areas(
[
(image_vnir, selected_areas_vnir),
(image_swir, selected_areas_swir),
],
plot_interactive_buttons=False,
)
Source: transformations_02.py
This example demonstrates how to apply various image transformations:
- Add Gaussian noise to the image.
- Perform random cropping.
- Apply random mirroring.
- Rotate the image randomly.
- Rescale the image.
- Normalize the image area.
from pathlib import Path
from siapy.entities import SpectralImage
from siapy.transformations.image import (
add_gaussian_noise,
area_normalization,
random_crop,
random_mirror,
random_rotation,
rescale,
)
# 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"))[0]
image_path_img0 = sorted(Path(data_dir).rglob("*.img"))[0]
# Load VNIR and SWIR spectral images
image_swir = SpectralImage.envi_open(
header_path=header_path_img0,
image_path=image_path_img0,
)
# Convert image to numpy array
image_swir_np = image_swir.to_numpy()
# Apply transformations to image_swir
# Add Gaussian noise
noisy_image = add_gaussian_noise(image_swir_np, mean=0.0, std=1.0, clip_to_max=True)
# Random crop
cropped_image = random_crop(image_swir_np, output_size=(100, 100))
# Random mirror
mirrored_image = random_mirror(image_swir_np)
# Random rotation
rotated_image = random_rotation(image_swir_np, angle=45)
# Rescale
rescaled_image = rescale(image_swir_np, output_size=(200, 200))
# Area normalization
normalized_image = area_normalization(image_swir_np)
Source: transformations_03.py