Skip to content

Commit

Permalink
refactor: added option to directly calculate correction factor from i…
Browse files Browse the repository at this point in the history
…mage
  • Loading branch information
janezlapajne committed Sep 18, 2024
1 parent 12ffa05 commit b3e559f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
30 changes: 22 additions & 8 deletions siapy/utils/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import spectral as sp

from siapy.core import logger
from siapy.core.exceptions import InvalidInputError
from siapy.core.types import ImageDataType, ImageType
from siapy.entities import SpectralImage
from siapy.transformations.image import rescale
Expand Down Expand Up @@ -187,14 +188,27 @@ def convert_radiance_image_to_reflectance(
def calculate_correction_factor_from_panel(
image: SpectralImage,
panel_reference_reflectance: float,
panel_shape_label: str = "reference_panel",
) -> np.ndarray | None:
panel_shape = image.geometric_shapes.get_by_name(panel_shape_label)
if panel_shape is None:
return None

panel_signatures = image.to_signatures(panel_shape.convex_hull())
panel_radiance_mean = panel_signatures.signals.mean()
panel_shape_label: str | None = None,
) -> np.ndarray:
if panel_shape_label:
panel_shape = image.geometric_shapes.get_by_name(panel_shape_label)
if not panel_shape:
raise InvalidInputError(
input_value={"panel_shape_label": panel_shape_label},
message="Panel shape label not found.",
)
panel_signatures = image.to_signatures(panel_shape.convex_hull())
panel_radiance_mean = panel_signatures.signals.mean()

else:
temp_mean = image.mean(axis=(0, 1))
if not isinstance(temp_mean, np.ndarray):
raise InvalidInputError(
input_value={"image": image},
message=f"Expected image.mean(axis=(0, 1)) to return np.ndarray, but got {type(temp_mean).__name__}.",
)
panel_radiance_mean = temp_mean

panel_reflectance_mean = np.full(image.bands, panel_reference_reflectance)
panel_correction = panel_reflectance_mean / panel_radiance_mean
return panel_correction
Expand Down
15 changes: 13 additions & 2 deletions tests/utils/test_utils_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def shape(self) -> tuple[int, int, int]:
assert save_path.exists()


def test_calculate_correction_factor_from_panel(spectral_images):
def test_calculate_correction_factor_from_panel_with_label(spectral_images):
pixels = Pixels.from_iterable([(900, 1150), (1050, 1300)])
rect = Shape.from_shape_type(
shape_type="rectangle", pixels=pixels, label="reference_panel"
Expand All @@ -156,7 +156,6 @@ def test_calculate_correction_factor_from_panel(spectral_images):
panel_shape_label="reference_panel",
)

assert panel_correction is not None
assert isinstance(panel_correction, np.ndarray)
assert panel_correction.shape == (image_vnir.bands,)

Expand All @@ -168,6 +167,18 @@ def test_calculate_correction_factor_from_panel(spectral_images):
)


def test_calculate_correction_factor_from_panel_without_label(spectral_images):
image_vnir = spectral_images.vnir
panel_correction = calculate_correction_factor_from_panel(
image=image_vnir,
panel_reference_reflectance=0.3,
)
direct_panel_calculation = np.full(image_vnir.bands, 0.3) / image_vnir.mean(
axis=(0, 1)
)
assert np.array_equal(direct_panel_calculation, panel_correction)


def test_convert_radiance_image_to_reflectance_without_saving(spectral_images):
image_vnir = spectral_images.vnir
panel_correction = np.random.default_rng().random(image_vnir.bands)
Expand Down

0 comments on commit b3e559f

Please sign in to comment.