Skip to content
Merged
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
37 changes: 22 additions & 15 deletions src/machinevisiontoolbox/ImageConstants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
if TYPE_CHECKING:
from typing import Self

from machinevisiontoolbox.ImageCore import Image

import cv2
import numpy as np
from spatialmath import Polygon2
Expand Down Expand Up @@ -38,7 +40,7 @@ def _getshape(
h: int | None,
colororder: str | None,
size: int | Sequence[int] | None,
):
) -> list[int]:
p = None

if size is not None:
Expand All @@ -58,7 +60,8 @@ def _getshape(

if w is None or h is None:
raise ValueError("dimensions not specified by size, w, h")
shape = [h, w]
# h and w are ints by here, but mypy cannot see it through the Sequence option
shape = cast(list[int], [h, w])

if size is not None and isinstance(size, Iterable) and len(size) == 3:
p = size[2]
Expand All @@ -81,10 +84,10 @@ def _resolve_pattern_options(
size: Any,
dtype: Dtype | None,
colororder: str | None,
like: Any,
like: Image | None,
default_size: Any,
default_dtype: Dtype,
):
) -> tuple[Any, Any, str | None]:
if like is not None and not isinstance(like, cls):
raise TypeError("like must be an Image")

Expand Down Expand Up @@ -113,7 +116,7 @@ def _resolve_pattern_options(
return size, dtype, colororder


def _pattern_image(cls, image: np.ndarray, colororder: str | None):
def _pattern_image(cls, image: np.ndarray, colororder: str | None) -> Any:
if colororder is not None:
image = np.repeat(
image[..., np.newaxis], len(cls.colordict(colororder)), axis=2
Expand All @@ -122,6 +125,10 @@ def _pattern_image(cls, image: np.ndarray, colororder: str | None):


class ImageConstantsMixin(ImageMixinBase):
"""
Methods of :class:`Image` that create images of constant, random or patterned pixel values
"""

# ======================= patterns ================================== #

@classmethod
Expand All @@ -131,7 +138,7 @@ def Zeros(
size: int | Sequence[int] | None = None,
colororder: str | None = None,
dtype: Dtype | None = None,
like=None,
like: Image | None = None,
) -> Self:
"""
Create image with zero value pixels
Expand Down Expand Up @@ -189,7 +196,7 @@ def Constant(
colororder: str | None = None,
dtype: Dtype | None = None,
size: Sequence[int] | None = None,
like=None,
like: Image | None = None,
) -> Self:
"""
Create image with all pixels having same value
Expand Down Expand Up @@ -351,7 +358,7 @@ def String(
:seealso: :meth:`Constant`
"""

def str2array(s):
def str2array(s: str) -> list[list[int]]:
if "|" in s:
pixels = []
for row in s.split("|"):
Expand Down Expand Up @@ -398,7 +405,7 @@ def Random(
dtype: Dtype | None = None,
maxval: int | float | None = None,
pdf: np.ndarray | None = None,
like=None,
like: Image | None = None,
) -> Self:
"""
Create image with random pixel values
Expand Down Expand Up @@ -554,7 +561,7 @@ def Squares(
bg: Any = 0,
dtype: Dtype | None = None,
colororder: str | None = None,
like=None,
like: Image | None = None,
) -> Self:
"""
Create image containing grid of squares
Expand Down Expand Up @@ -649,7 +656,7 @@ def Circles(
bg: Any = 0,
dtype: Dtype | None = None,
colororder: str | None = None,
like=None,
like: Image | None = None,
) -> Self:
"""
Create image containing grid of circles
Expand Down Expand Up @@ -746,7 +753,7 @@ def Ramp(
size: int | Sequence[int] | None = None,
dtype: Dtype | None = None,
colororder: str | None = None,
like=None,
like: Image | None = None,
) -> Self:
"""
Create image of linear ramps
Expand Down Expand Up @@ -845,7 +852,7 @@ def Sin(
size: int | Sequence[int] | None = None,
dtype: Dtype | None = None,
colororder: str | None = None,
like=None,
like: Image | None = None,
) -> Self:
"""
Create image of sinusoidal intensity pattern
Expand Down Expand Up @@ -931,7 +938,7 @@ def Chequerboard(
size: int | Sequence[int] | None = None,
dtype: Dtype | None = None,
colororder: str | None = None,
like=None,
like: Image | None = None,
) -> Self:
"""Create chequerboard pattern

Expand Down Expand Up @@ -1018,7 +1025,7 @@ def Polygons(
shift: int = 0,
dtype: str | np.dtype = "uint8",
colororder: str | None = None,
like=None,
like: Image | None = None,
) -> Self:
"""
Create an image containing filled polygons
Expand Down
Loading