Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable VoxelArray in subregion #287

Merged
merged 5 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/darsia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from darsia.image.image import *
from darsia.image.indexing import *
from darsia.image.patches import *
from darsia.image.subregions import *
from darsia.image.imread import *
from darsia.image.arithmetics import *
from darsia.measure.integration import *
Expand All @@ -33,6 +32,7 @@
from darsia.utils import linalg
from darsia.utils import quadrature
from darsia.utils import plotting
from darsia.image.subregions import *
from darsia.corrections.basecorrection import *
from darsia.corrections.shape.transformation import *
from darsia.corrections.shape.curvature import *
Expand Down
5 changes: 3 additions & 2 deletions src/darsia/corrections/basecorrection.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ def __call__(

Args:
image (array or Image): image
overwrite (bool): flag controlling whether the original image is
overwritten or the correction is applied to a copy.
overwrite (bool): flag controlling whether the original image is overwritten
or the correction is applied to a copy. This option has to be used with
case.

Returns:
array or Image: corrected image, data type depends on input.
Expand Down
9 changes: 6 additions & 3 deletions src/darsia/corrections/shape/curvature.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,22 @@ def pre_bulge_correction(self, **kwargs) -> None:
self.current_image, **self.config["init"]
)

def crop(self, corner_points: list) -> None:
def crop(self, corner_points: darsia.VoxelArray) -> None:
"""
Crop the image along the corners of the image.

The four corner points of the image should be provided, and this method
will update the config file and modify the current image.

Arguments:
corner_points (list): list of the corner points. Preferably the list
corner_points (VoxelArray): list of the corner points. Preferably the list
should be ordered starting from the upper left corner
and going counter clockwise.
"""

if not isinstance(corner_points, darsia.VoxelArray):
corner_points = darsia.make_voxels(corner_points)

self.config["crop"] = {
"pts_src": corner_points,
"width": self.width,
Expand All @@ -224,7 +227,7 @@ def crop(self, corner_points: list) -> None:
self.current_image, **self.config["crop"]
)

def bulge_corection(
def bulge_correction(
self, left: int = 0, right: int = 0, top: int = 0, bottom: int = 0
) -> None:
"""
Expand Down
5 changes: 2 additions & 3 deletions src/darsia/image/arithmetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def superpose(images: list[darsia.Image]) -> darsia.Image:
mapped_voxel_origin = image.coordinatesystem.voxel(origin)
mapped_voxel_opposite_corner = image.coordinatesystem.voxel(opposite_corner)

pts_dst = np.array(
pts_dst = darsia.make_voxel(
[
[mapped_voxel_origin[0], mapped_voxel_origin[1]],
[mapped_voxel_opposite_corner[0], mapped_voxel_origin[1]],
Expand All @@ -204,7 +204,7 @@ def superpose(images: list[darsia.Image]) -> darsia.Image:
rows, cols = array.shape

# Use corners as pts_src
pts_src = np.array(
pts_src = darsia.make_voxel(
[
[0, 0],
[rows, 0],
Expand All @@ -218,7 +218,6 @@ def superpose(images: list[darsia.Image]) -> darsia.Image:
img_src=array,
pts_src=pts_src,
pts_dst=pts_dst,
indexing="matrix",
interpolation="inter_area",
shape=space_shape,
)
Expand Down
86 changes: 44 additions & 42 deletions src/darsia/image/subregions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,25 @@
"""

from typing import Literal, Optional, Union
from warnings import warn

import cv2
import numpy as np

IndexingOption = Literal["matrix", "reverse matrix"]
"""Indexing option consi else:dered in the following conversion."""
import darsia


def to_reverse_matrix_indexing(
pixel: np.ndarray, indexing: IndexingOption = "reverse matrix"
) -> np.ndarray:
"""Convert pixel coordinates to reverse matrix indexing format.
def _to_reverse_matrix_indexing(pixel: np.ndarray) -> np.ndarray:
"""Auxiliary routine: Convert pixel coordinates to reverse matrix indexing format.

Args:
pixel (np.ndarray): pixel coordinates
indexing (IndexingOption): indexing of pixel

Returns:
pixel converted to 'reverse matrix' indexing

Raises:
NotImplementedError: if dimension not 2
NotImplementedError: if indexing not among IndexingOption

"""
if not len(pixel.shape) == 2:
raise NotImplementedError

if indexing == "reverse matrix":
return pixel
elif indexing == "matrix":
return np.fliplr(np.atleast_2d(pixel))
else:
raise NotImplementedError
return np.fliplr(np.atleast_2d(pixel))


InterpolationOption = Literal["inter_nearest", "inter_linear", "inter_area"]
Expand All @@ -45,8 +30,8 @@ def to_reverse_matrix_indexing(

def extract_quadrilateral_ROI(
img_src: np.ndarray,
pts_src: Optional[Union[list, np.ndarray]] = None,
indexing: IndexingOption = "reverse matrix",
pts_src: Optional[Union[list, darsia.VoxelArray, np.ndarray]] = None,
indexing: Literal["matrix", "reverse matrix"] = "reverse matrix",
interpolation: InterpolationOption = "inter_linear",
**kwargs
) -> np.ndarray:
Expand All @@ -55,6 +40,12 @@ def extract_quadrilateral_ROI(
given known corner points of a square (default) object.

Args:
img_src (np.ndarray): source image
pts_src (array, optional): N points with pixels, can be provided in different
indexing formats
indexing (IndexingOption): indexing of pixel (only relevant if pts_src is list
or np.ndarray)
interpolation (InterpolationOption): interpolation method; adopted from cv2.
kwargs (optional keyword arguments):
width (int or float): width of the physical object
height (int or float): height of the physical object
Expand Down Expand Up @@ -99,31 +90,42 @@ def extract_quadrilateral_ROI(

# Fetch corner points in the provided image
if pts_src is None:
pts_src = [
[0, 0],
[original_shape[0], 0],
[original_shape[0], original_shape[1]],
[0, original_shape[1]],
]
pts_src = darsia.make_voxel(
[
[0, 0],
[original_shape[0], 0],
[original_shape[0], original_shape[1]],
[0, original_shape[1]],
]
)
if indexing == "reverse matrix":
pts_src = np.fliplr(np.array(pts_src))

if isinstance(pts_src, list):
pts_src = np.array(pts_src)

pts_src = to_reverse_matrix_indexing(pts_src, indexing)
pts_src = np.fliplr(pts_src)
else:
if isinstance(pts_src, darsia.VoxelArray):
# Voxels use by definition matrix indexing
pts_src = _to_reverse_matrix_indexing(pts_src)
else:
warn("Try using darsia.VoxelArray instead of list or np.ndarray.")
if isinstance(pts_src, list):
pts_src = np.array(pts_src)
if indexing == "matrix":
pts_src = _to_reverse_matrix_indexing(pts_src)

# Assign corner points as destination points if none are provided.
if "pts_dst" in kwargs:
pts_dst: np.ndarray = to_reverse_matrix_indexing(
np.array(kwargs.get("pts_dst")), indexing
)

pts_dst = kwargs.get("pts_dst")
if isinstance(pts_dst, darsia.VoxelArray):
# Voxels use by definition matrix indexing
pts_dst = _to_reverse_matrix_indexing(pts_dst)
else:
warn("Try using darsia.VoxelArray instead of list or np.ndarray.")
if isinstance(pts_dst, list):
pts_dst = np.array(pts_dst)
if indexing == "matrix":
pts_dst = _to_reverse_matrix_indexing(pts_dst)
else:
# Assume implicitly that corner points have been provided,
# and that their orientation is mathematically positive,
# starting with the top left corner.
# Furthermore, use reverse matrix indexing, i.e., (col,row).
# If no destination points are provided, assume implicitly that
# the target image is a square with the same size as the source image.
pts_dst = np.array(
[
[0, 0],
Expand Down
Loading