diff --git a/pyproject.toml b/pyproject.toml index f89740a5..58f35181 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -67,7 +67,7 @@ dependencies = [ # ocr - OCR support via pytesseract # tool - dependencies for the mvtbtool interactive shell -dev = ["pytest", "pytest-timeout", "pytest-xvfb", "coverage", "ruff", "nbmake"] +dev = ["pytest", "pytest-timeout", "pytest-xvfb", "coverage", "ruff", "nbmake", "mypy"] ros = ["rosbags", "roslibpy", "websockets"] @@ -127,9 +127,11 @@ known-first-party = ["machinevisiontoolbox"] # honours this file). exclude_dirs = ["tests"] -[tool.pyright] -include = ["src"] -ignore = ["docs", "tests", "examples"] +[tool.mypy] +mypy_path = "src" +files = ["src/machinevisiontoolbox"] +ignore_missing_imports = true +warn_unused_ignores = true [project.scripts] mvtbtool = "machinevisiontoolbox.bin.mvtbtool:main" diff --git a/src/machinevisiontoolbox/BagOfWords.py b/src/machinevisiontoolbox/BagOfWords.py index 207a75f7..7c0a3795 100644 --- a/src/machinevisiontoolbox/BagOfWords.py +++ b/src/machinevisiontoolbox/BagOfWords.py @@ -97,7 +97,7 @@ def __init__( features = images else: # passed images, compute the features - features = [] + features = [] # type: ignore[assignment] for image in images: features += image.SIFT() features.sort(by="scale", inplace=True) @@ -117,7 +117,7 @@ def __init__( if seed is not None: cv2.setRNGSeed(seed) - ret, labels, centroids = cv2.kmeans( + ret, labels, centroids = cv2.kmeans( # type: ignore[call-overload] data=features._descriptor, K=k, bestLabels=None, @@ -131,7 +131,7 @@ def __init__( self._words = labels.ravel() self._labels = labels.ravel() self._centroids = centroids - self._word_freq_vectors = None + self._word_freq_vectors: np.ndarray | None = None self._nstopwords = nstopwords if nstopwords > 0: @@ -141,7 +141,7 @@ def __init__( maxwords = self.k - self.nstopwords - W = [] + Wcols = [] id = np.array(self._features.id) for i in range(self.nimages): # get the words associated with image i @@ -149,8 +149,8 @@ def __init__( # create columns of the W v = BagOfWords._word_freq_vector(words, maxwords) - W.append(v) - W = np.column_stack(W) + Wcols.append(v) + W = np.column_stack(Wcols) N = self.nimages @@ -200,12 +200,14 @@ def wwfv(self, i: int | None = None) -> np.ndarray: .. note:: The word vector is expensive to compute so a lazy evaluation is performed on the first call to this method. """ + if self._word_freq_vectors is None: + raise ValueError("word frequency vectors have not been computed") if i is not None: v = self._word_freq_vectors[:, i] if v.ndim == 1: return np.c_[v] - else: - return self._word_freq_vectors + return v + return self._word_freq_vectors @property def nimages(self) -> int: @@ -465,7 +467,7 @@ def retrieve(self, images: Image | _ImageQueries | np.ndarray) -> tuple[int, flo S = self.similarity(images).ravel() k = np.argmax(S) - return k, S[k] + return int(k), float(S[k]) def features(self, word: int) -> BaseFeature2D: """ @@ -587,7 +589,7 @@ def exemplars( from machinevisiontoolbox import Image exemplars = [] - count = Counter() + count: Counter[Any] = Counter() if images is None: images = self._images for feature in self.features(word): diff --git a/src/machinevisiontoolbox/BundleAdjust.py b/src/machinevisiontoolbox/BundleAdjust.py index dff636b7..a2abccad 100644 --- a/src/machinevisiontoolbox/BundleAdjust.py +++ b/src/machinevisiontoolbox/BundleAdjust.py @@ -22,10 +22,14 @@ # pgraph 1.0 has a single Vertex class and deprecates UVertex; 0.6.x has only # UVertex. A vertex has no direction of its own in 1.0, so they are equivalent. -_Vertex = getattr(pgraph, "Vertex", None) or pgraph.UVertex +_Vertex: Any = getattr(pgraph, "Vertex", None) or pgraph.UVertex class _Common: + _index: int + _index2: int + _fixed: bool + @property def index(self) -> int: """ @@ -209,11 +213,11 @@ def __init__(self, camera: CentralCamera) -> None: self._nlandmarks = 0 # number of landmark points self._nvarstate = 0 - self.views = [] # list of view nodes - self.landmarks = [] # list of landmark nodes + self.views: list[Any] = [] # list of view nodes + self.landmarks: list[Any] = [] # list of landmark nodes - self.fixedviews = [] # list of view nodes that are fixed - self.fixedlandmarks = [] # list of landmark nodes that are fixed + self.fixedviews: list[Any] = [] # list of view nodes that are fixed + self.fixedlandmarks: list[Any] = [] # list of landmark nodes that are fixed self.index_valid = False @@ -595,9 +599,8 @@ def optimize( print(f"Bundle adjustment cost {self.errors(x0):.3g} -- initial") for i in range(iterations): if animate: - if not retain: - plt.clf() - g2.plot() + plt.clf() + self.plot(block=False) plt.pause(0.5) ta = time.perf_counter() diff --git a/src/machinevisiontoolbox/Camera.py b/src/machinevisiontoolbox/Camera.py index b5645b8f..4a0920ce 100644 --- a/src/machinevisiontoolbox/Camera.py +++ b/src/machinevisiontoolbox/Camera.py @@ -8,7 +8,7 @@ from abc import ABC, abstractmethod from collections import namedtuple from math import cos, pi, sin, sqrt, tan -from typing import TYPE_CHECKING, Any, Callable +from typing import TYPE_CHECKING, Any, Callable, cast import cv2 import matplotlib.pyplot as plt @@ -23,11 +23,15 @@ from spatialmath import SE3, Line3 from machinevisiontoolbox import Image -from machinevisiontoolbox.base import idisp -from machinevisiontoolbox.base.imageio import _isnotebook, safe_plt_show +from machinevisiontoolbox.base.imageio import _isnotebook, idisp, safe_plt_show from machinevisiontoolbox.ImagePointFeatures import FeatureMatch from machinevisiontoolbox.mvtb_types import ArrayLike +# spatialmath's argcheck helpers are annotated as returning a union of ndarray, +# list and tuple, but they always return an ndarray unless out="sequence" is given +_getvector = cast(Callable[..., np.ndarray], smb.getvector) +_getmatrix = cast(Callable[..., np.ndarray], smb.getmatrix) + # from machinevisiontoolbox.classes import Image # import CameraVisualizer as CamVis @@ -110,7 +114,7 @@ def __init__( else: self.imagesize = imagesize if pp is None: - self.pp = [x / 2 for x in self.imagesize] + self.pp = [x / 2 for x in self.imagesize] # type: ignore[union-attr] else: self.pp = pp @@ -121,7 +125,7 @@ def __init__( self._rhou = sensorsize[0] / self._imagesize[0] self._rhov = sensorsize[1] / self._imagesize[1] else: - rho = smb.getvector(rho) + rho = _getvector(rho) if len(rho) == 1: self._rhou = rho[0] self._rhov = rho[0] @@ -143,7 +147,7 @@ def __init__( self.pose0 = self.pose - self._noise = noise + self._noise = noise # type: ignore[assignment] self._image = None @@ -199,9 +203,9 @@ def fmt(x: tuple[float, float], spec: str = ".3g") -> str: return f"({x[0]:{spec}}, {x[1]:{spec}})" s = f"{self.__class__.__name__}(name='{self.name}'" - s += f", pixel_size={fmt(self.rho)}" + s += f", pixel_size={fmt(self.rho)}" # type: ignore[arg-type] if self.imagesize is not None: - s += f", image_size={fmt(self._imagesize, spec='d')}" + s += f", image_size={fmt(self._imagesize, spec='d')}" # type: ignore[arg-type] s += f", pose=[{self.pose.strline()}]" return s + ")" @@ -321,7 +325,7 @@ def imagesize(self, npix: ArrayLike) -> None: :seealso: :meth:`width` :meth:`height` :meth:`nu` :meth:`nv` """ - npix = smb.getvector(npix, dtype="int") + npix = _getvector(npix, dtype="int") if len(npix) == 1: self._imagesize = np.r_[npix[0], npix[0]] elif len(npix) in (2, 3): @@ -449,7 +453,7 @@ def pp(self, pp: ArrayLike) -> None: :seealso: :meth:`pp` :meth:`u0` :meth:`v0` """ - pp = smb.getvector(pp) + pp = _getvector(pp) if len(pp) == 1: self._pp = np.r_[pp[0], pp[0]] elif len(pp) == 2: @@ -641,7 +645,7 @@ def noise(self) -> float | None: @noise.setter def noise(self, noise: float | None) -> None: - self._noise = noise + self._noise = noise # type: ignore[assignment] @property def distortion(self) -> np.ndarray | None: @@ -674,7 +678,7 @@ def distortion(self) -> np.ndarray | None: @distortion.setter def distortion(self, distortion: np.ndarray | None) -> None: - self._distortion = distortion + self._distortion = distortion # type: ignore[assignment] def move( self, T: SE3, name: str | None = None, relative: bool = False @@ -756,7 +760,7 @@ def _new_imageplane( fig, ax = plt.subplots() # TODO *args, **kwargs? # TODO elif ax is a plot handle, else raise ValueError self._ax = ax - self._fig = fig + self._fig = fig # type: ignore[assignment] return False def _init_imageplane( @@ -792,8 +796,8 @@ def _init_imageplane( ax.set_xlim(0, self.nu) ax.set_ylim(0, self.nv) else: - ax.set_xlim(self.limits[0], self.limits[1]) - ax.set_ylim(self.limits[2], self.limits[3]) + ax.set_xlim(self.limits[0], self.limits[1]) # type: ignore[index] + ax.set_ylim(self.limits[2], self.limits[3]) # type: ignore[index] ax.autoscale(False) ax.set_aspect("equal") ax.invert_yaxis() @@ -891,10 +895,10 @@ def plot_point( :seealso: :meth:`plot_line2` :meth:`plot_line3` :meth:`plot_wireframe` :meth:`clf` """ - self._init_imageplane(ax) + self._init_imageplane(ax) # type: ignore[arg-type] if not isinstance(P, np.ndarray): - P = smb.getvector(P) + P = _getvector(P) if P.shape[0] == 3: # plot world points @@ -909,7 +913,7 @@ def plot_point( defaults = dict(markersize=6, color="k") if len(fmt) == 0: - fmt = ["o"] + fmt = ["o"] # type: ignore[assignment] kwargs = {**defaults, **kwargs} assert self._ax is not None @@ -918,7 +922,7 @@ def plot_point( safe_plt_show(block=False) if return_artist: - return p, artist[0] + return p, artist[0] # type: ignore[return-value] else: return p @@ -1141,7 +1145,7 @@ def disp(self, im: Image, **kwargs: Any) -> None: plt.autoscale(False) def plot( - self=None, + self=None, # type: ignore[assignment] pose: SE3 | None = None, scale: float = 1, shape: str = "camera", @@ -1222,7 +1226,7 @@ def plot( # Matplotlib >=3.10 can raise while autoscaling ragged 3D line # collections (eg. from cuboid/cylinder helper primitives). Disable # autoscale only for these collection additions. - _orig_add_collection3d = ax.add_collection3d + _orig_add_collection3d = ax.add_collection3d # type: ignore[union-attr] def _add_collection3d_compat(collection, *args, **kwargs): kwargs.setdefault("autolim", False) @@ -1233,7 +1237,7 @@ def _add_collection3d_compat(collection, *args, **kwargs): kwargs.pop("autolim", None) return _orig_add_collection3d(collection, *args, **kwargs) - ax.add_collection3d = _add_collection3d_compat + ax.add_collection3d = _add_collection3d_compat # type: ignore[union-attr] try: # draw camera-like object: @@ -1253,7 +1257,7 @@ def _add_collection3d_compat(collection, *args, **kwargs): elif frame is not False: pose.plot(**frame) finally: - ax.add_collection3d = _orig_add_collection3d + ax.add_collection3d = _orig_add_collection3d # type: ignore[union-attr] return ax @@ -1300,7 +1304,7 @@ def _plot_frustum(ax: Axes, pose: SE3, scale: float, alpha: float) -> None: np.array([b3, b0, t0, t3]), # -y face ] poly = Poly3DCollection(points, facecolors=["r", "g", "r", "y"], alpha=alpha) - ax.add_collection3d(poly) + ax.add_collection3d(poly) # type: ignore[attr-defined] @staticmethod def _plot_camera_icon( @@ -1347,7 +1351,7 @@ def _plot_camera_icon( if label: ax.set_xlabel("X") ax.set_ylabel("Y") - ax.set_zlabel("Z") + ax.set_zlabel("Z") # type: ignore[attr-defined] def _add_noise_distortion(self, uv: np.ndarray) -> np.ndarray: """ @@ -1399,7 +1403,7 @@ class CentralCamera(CameraBase): _fu: float _fv: float - _distortion: np.ndarray | None + _distortion: np.ndarray | None # type: ignore[assignment] def __init__( self, f: ArrayLike = 1, distortion: np.ndarray | None = None, **kwargs @@ -1547,7 +1551,7 @@ def project_point( if P.ndim == 1: P = P.reshape((-1, 1)) # make it a column else: - P = smb.getvector(P, out="col") + P = _getvector(P, out="col") if P.shape[0] == 3: P = smb.e2h(P) # make it homogeneous @@ -1584,7 +1588,7 @@ def project_point( & (x[1, :] < self.nv) ) - return x, visible + return x, visible # type: ignore[return-value] else: return x else: @@ -1833,7 +1837,7 @@ def ray(self, points: np.ndarray | ArrayLike, pose: SE3 | None = None) -> Line3: Mi = np.linalg.inv(C[:3, :3]) v = C[:, 3] lines = [] - for point in smb.getmatrix(points, (2, None)).T: + for point in _getmatrix(points, (2, None)).T: lines.append(Line3.PointDir(-Mi @ v, Mi @ smb.e2h(point))) return Line3(lines) @@ -2027,7 +2031,7 @@ def f(self, f: ArrayLike) -> None: :type f: scalar or array_like(2) :raises ValueError: incorrect length of ``f`` """ - f = smb.getvector(f) + f = _getvector(f) if len(f) == 1: self._fu = f[0] @@ -2293,7 +2297,7 @@ def images2C( ) valid.append(i) - ret, C, distortion, rvecs, tvecs = cv2.calibrateCamera( # type: ignore[call-overload] + ret, C, distortion, rvecs, tvecs = cv2.calibrateCamera( objectPoints=objpoints, imagePoints=imgpoints, imageSize=grey.shape[::-1], @@ -2456,7 +2460,7 @@ def H(self, T: SE3, n: ArrayLike, d: float) -> np.ndarray: if d < 0: raise ValueError(d, "plane distance d must be > 0") - n = smb.getvector(n) + n = _getvector(n) if n[2] < 0: raise ValueError(n, "normal must be away from camera (n[2] >= 0)") @@ -2596,7 +2600,7 @@ def decomposeH( # not quite proper SO(3) values pose = SE3.Rt(smb.trnorm(R), t).inv() T.append(pose) - return T, normals + return T, normals # type: ignore[return-value] # if K is None: # K = np.identity(3) @@ -2815,9 +2819,9 @@ def points2F( # add various return values retval = [F] if residual: - e = smb.e2h(p2[:, mask]).T @ F @ smb.e2h(p1[:, mask]) + e = smb.e2h(p2[:, mask]).T @ F @ smb.e2h(p1[:, mask]) # type: ignore[misc] resid = np.linalg.norm(np.diagonal(e)) - retval.append(resid) + retval.append(resid) # type: ignore[arg-type] if method in ("ransac", "lmeds"): retval.append(mask) @@ -3030,7 +3034,7 @@ def decomposeE( if smb.isvector(P, 3): for Rt in possibles: pose = SE3.Rt(Rt[0], Rt[1]).inv() - p = self.project_point(P, pose=pose, behind=True) + p = self.project_point(P, pose=pose, behind=True) # type: ignore[arg-type] # check if point is projected behind the camera, indicated # by nan values if not np.isnan(p[0]): @@ -3090,10 +3094,10 @@ def visjac_p( :seealso: :meth:`flowfield` :meth:`visjac_p_polar` :meth:`visjac_l` :meth:`visjac_e` """ - uv = smb.getmatrix(uv, (2, None)) + uv = _getmatrix(uv, (2, None)) Z = depth - Z = smb.getvector(Z) + Z = _getvector(Z) if len(Z) == 1: Z = np.repeat(Z, uv.shape[1]) elif len(Z) != uv.shape[1]: @@ -3168,13 +3172,12 @@ def visjac_p_polar( """ J = [] - p = smb.getmatrix(p, (2, None)) + p = _getmatrix(p, (2, None)) f = self.f[0] - if smb.isscalar(Z): - Z = [Z] * p.shape[1] + Zs = [Z] * p.shape[1] if smb.isscalar(Z) else Z - for (phi, r), Zk in zip(p.T, Z): + for (phi, r), Zk in zip(p.T, Zs): # type: ignore[arg-type] # k = (f**2 + r**2) / f # k2 = f / (r * Zk) @@ -3273,9 +3276,9 @@ def visjac_l(self, lines: np.ndarray | ArrayLike, plane: ArrayLike) -> np.ndarra :seealso: :meth:`visjac_p` :meth:`visjac_p_polar` :meth:`visjac_e` """ - a, b, c, d = plane + a, b, c, d = _getvector(plane) - lines = smb.getmatrix(lines, (2, None)) + lines = _getmatrix(lines, (2, None)) jac = [] for theta, rho in lines.T: sth = np.sin(theta) @@ -3362,6 +3365,8 @@ def visjac_e(self, E: ArrayLike, plane: ArrayLike) -> np.ndarray: :seealso: :meth:`visjac_p` :meth:`visjac_p_polar` :meth:`visjac_l` """ + E = _getvector(E) + plane = _getvector(plane) a = -plane[0] / plane[3] b = -plane[1] / plane[3] c = -plane[2] / plane[3] @@ -3435,7 +3440,7 @@ def flowfield(self, vel: ArrayLike, Z: float = 2) -> None: :seealso: :meth:`visjac_p` """ - vel = smb.getvector(vel, 6) + vel = _getvector(vel, 6) u = np.arange(0, self.nu, 50) v = np.arange(0, self.nv, 50) @@ -3481,7 +3486,7 @@ def derivatives( Kp = [self.f[0], self.rhou, self.rhov, self.u0, self.v0] - return cameraModel(*x, *P, *Kp) + return cameraModel(*x, *P, *Kp) # type: ignore[misc] def estpose( self, @@ -3645,7 +3650,7 @@ def __str__(self) -> str: s += self.fmt.format("k", self.k, fmt="{:.4g}") return s - def project_point( + def project_point( # type: ignore[override] self, P: np.ndarray | ArrayLike, pose: SE3 | None = None, @@ -3695,7 +3700,7 @@ def project_point( :seealso: :meth:`plot_point` """ - P = smb.getmatrix(P, (3, None)) + P = _getmatrix(P, (3, None)) if pose is not None: T = self.pose.inv() @@ -3817,7 +3822,7 @@ def __str__(self) -> str: s += self.fmt.format("k", self.k, fmt="{:.4g}") return s - def project_point( + def project_point( # type: ignore[override] self, P: np.ndarray | ArrayLike, pose: SE3 | None = None, @@ -3853,7 +3858,7 @@ def project_point( :seealso: :meth:`plot_point` """ - P = smb.getmatrix(P, (3, None)) + P = _getmatrix(P, (3, None)) if pose is not None: T = self.pose.inv() @@ -3906,7 +3911,7 @@ def __init__(self, **kwargs: Any) -> None: super().__init__( camtype="spherical", limits=[-pi, pi, 0, pi], - labels=["Longitude φ (rad)", "Colatitude θ (rad)"], + labels=["Longitude φ (rad)", "Colatitude θ (rad)"], # type: ignore[arg-type] **kwargs, ) @@ -3923,7 +3928,7 @@ def fov(self) -> list[float]: """ return [2 * pi, 2 * pi] - def project_point( + def project_point( # type: ignore[override] self, P: np.ndarray | ArrayLike, pose: SE3 | None = None, @@ -3958,7 +3963,7 @@ def project_point( :seealso: :meth:`plot_point` """ - P = smb.getmatrix(P, (3, None)) + P = _getmatrix(P, (3, None)) if pose is None: pose = self.pose @@ -4012,11 +4017,11 @@ def visjac_p( """ J = [] - if smb.isscalar(depth): - depth = [depth] * p.shape[1] - assert depth is not None + depths = [depth] * p.shape[1] if smb.isscalar(depth) else depth # type: ignore[union-attr] + if depths is None: + raise ValueError("depth must be given") - for (phi, theta), R in zip(p.T, depth): + for (phi, theta), R in zip(p.T, depths): # type: ignore[union-attr, arg-type] sp = np.sin(phi) cp = np.cos(phi) st = np.sin(theta) @@ -4031,7 +4036,7 @@ def visjac_p( J.append(Jk) return np.vstack(J) - def plot(self, frame: bool = False, **kwargs: Any) -> None: + def plot(self, frame: bool = False, **kwargs: Any) -> None: # type: ignore[override] smb.plot_sphere( radius=1, filled=True, @@ -4151,7 +4156,7 @@ def plot(self, frame: bool = False, **kwargs: Any) -> None: # if P.ndim == 1: # P = P.reshape((-1, 1)) # make it a column # else: -# P = smb.getvector(P, out='col') +# P = _getvector(P, out='col') # # make it homogeneous if not already # if P.shape[0] == 3: diff --git a/src/machinevisiontoolbox/ImageBlobs.py b/src/machinevisiontoolbox/ImageBlobs.py index 93c32345..d7b27a5b 100644 --- a/src/machinevisiontoolbox/ImageBlobs.py +++ b/src/machinevisiontoolbox/ImageBlobs.py @@ -21,7 +21,8 @@ from spatialmath import SE2 import spatialmath.base as smb -from machinevisiontoolbox.base import plot_labelbox, mpl_styling +from machinevisiontoolbox._image_typing import ImageMixinBase +from machinevisiontoolbox.base.graphics import plot_labelbox, mpl_styling from machinevisiontoolbox.decorators import array_result, scalar_result if TYPE_CHECKING: @@ -94,7 +95,7 @@ def print(self) -> str: class Blobs(UserList): # lgtm[py/missing-equals] - _image = [] # keep image saved for each Blobs object + _image: list[Any] = [] # keep image saved for each Blobs object def __init__( self, @@ -635,7 +636,7 @@ def filter( if not mask: return self[:] - m: np.ndarray = np.array(mask).all(axis=0) # type: ignore[assignment] + m: np.ndarray = np.array(mask).all(axis=0) return self[m.flatten()] @@ -1795,15 +1796,15 @@ def polarmatch(self, target: int) -> tuple[list[float], np.ndarray]: # assert(numrows(r1) == numrows(r2), 'r1 and r2 must have same number of rows'); - R = [] + Rlist = [] for i in range(len(self)): # get the radius profile r, _ = self[i].polar() # normalize to zero mean and unit variance r -= r.mean() r /= np.std(r) - R.append(r) - R = np.array(R) # on row per blob boundary + Rlist.append(r) + R = np.array(Rlist) # on row per blob boundary n = R.shape[1] # get the target profile @@ -2611,7 +2612,7 @@ def dotfile( dot_stream = open(filename, "r") else: f.seek(0) - dot_stream = f + dot_stream = f # type: ignore[assignment] pdffile = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) subprocess.run( @@ -2627,7 +2628,7 @@ def dotfile( dot_stream.close() -class ImageBlobsMixin(_ImageBase if TYPE_CHECKING else object): +class ImageBlobsMixin(ImageMixinBase): def blobs(self, **kwargs) -> Blobs: """ Find and describe blobs in image diff --git a/src/machinevisiontoolbox/ImageColor.py b/src/machinevisiontoolbox/ImageColor.py index f326c491..fc849b8d 100644 --- a/src/machinevisiontoolbox/ImageColor.py +++ b/src/machinevisiontoolbox/ImageColor.py @@ -18,6 +18,7 @@ from scipy import interpolate +from machinevisiontoolbox._image_typing import ImageMixinBase from machinevisiontoolbox.base import color, imageio, name2color if TYPE_CHECKING: @@ -25,7 +26,7 @@ from machinevisiontoolbox._image_typing import _ImageBase -class ImageColorMixin(_ImageBase if TYPE_CHECKING else object): +class ImageColorMixin(ImageMixinBase): """ Image processing color operations on the Image class """ @@ -351,7 +352,6 @@ def colorspace(self, dst: str, src: str | None = None) -> Self: # options gamma, on by default if to is RGB or BGR # options white on by default - out = [] # print('converting from', src, 'to', dst) out = color.colorspace_convert(self._A, src, dst) diff --git a/src/machinevisiontoolbox/ImageConstants.py b/src/machinevisiontoolbox/ImageConstants.py index b3b1b20c..10bc5b8d 100644 --- a/src/machinevisiontoolbox/ImageConstants.py +++ b/src/machinevisiontoolbox/ImageConstants.py @@ -10,7 +10,7 @@ import xml.etree.ElementTree as ET from collections.abc import Iterable from pathlib import Path -from typing import TYPE_CHECKING, Any, Sequence +from typing import TYPE_CHECKING, Any, Sequence, cast if TYPE_CHECKING: from typing import Self @@ -23,7 +23,9 @@ from spatialmath.base import islistof, isscalar # from numpy.lib.arraysetops import isin -from machinevisiontoolbox.base import DTYPE_ALIASES, float_image, int_image, name2color +from machinevisiontoolbox._image_typing import ImageMixinBase +from machinevisiontoolbox.base.color import name2color +from machinevisiontoolbox.base.types import DTYPE_ALIASES from machinevisiontoolbox.base.imageio import convert, idisp, iread, iwrite from machinevisiontoolbox.Kernel import Kernel from machinevisiontoolbox.mvtb_types import Dtype @@ -127,7 +129,7 @@ def _pattern_image(cls, image: np.ndarray, colororder: str | None): return cls(image, colororder=colororder, dtype=True) -class ImageConstantsMixin(_ImageBase if TYPE_CHECKING else object): +class ImageConstantsMixin(ImageMixinBase): # ======================= patterns ================================== # @classmethod @@ -502,13 +504,14 @@ def Random( shape = _getshape(cls, None, None, colororder, size) dtype = np.dtype(dtype) + im: np.ndarray if pdf is None: if np.issubdtype(dtype, np.integer): if maxval is None: maxval = np.iinfo(dtype).max else: maxval = int(maxval) - im = np.random.randint(0, maxval, size=shape, dtype=dtype) + im = np.random.randint(0, maxval, size=shape, dtype=cast(Any, dtype)) elif np.issubdtype(dtype, np.floating): if maxval is None: maxval = 1.0 @@ -987,6 +990,7 @@ def Chequerboard( ) dtype = np.dtype(dtype) + maxval: float if np.issubdtype(dtype, np.integer): maxval = np.iinfo(dtype).max else: @@ -1093,9 +1097,9 @@ def Polygons( height, width = shape[:2] if isinstance(color, Iterable): - if len(color) != len(polygons): + colors = list(color) + if len(colors) != len(polygons): raise ValueError("fill must have same length as number of polygons") - colors = color else: colors = [color] * len(polygons) diff --git a/src/machinevisiontoolbox/ImageCore.py b/src/machinevisiontoolbox/ImageCore.py index 992abc36..dc6439c1 100644 --- a/src/machinevisiontoolbox/ImageCore.py +++ b/src/machinevisiontoolbox/ImageCore.py @@ -11,6 +11,7 @@ from collections.abc import Iterator, Sequence from math import nan from pathlib import Path +from typing import Any import cv2 import numpy as np @@ -19,17 +20,15 @@ from spatialmath.base import islistof, isscalar # from numpy.lib.arraysetops import isin -from machinevisiontoolbox.base import ( - DTYPE_ALIASES, +from machinevisiontoolbox.base.graphics import ( draw_box, draw_circle, draw_labelbox, draw_line, draw_point, draw_text, - float_image, - int_image, ) +from machinevisiontoolbox.base.types import DTYPE_ALIASES, float_image, int_image from machinevisiontoolbox.base.imageio import convert, idisp, iread, iwrite from machinevisiontoolbox.ImageBlobs import ImageBlobsMixin from machinevisiontoolbox.ImageColor import ImageColorMixin @@ -284,8 +283,8 @@ def __init__( gives a C-contiguous copy, so use ``Image(array, copy=True)`` to fix it. """ - self._name = None - self._colororder = None + self._name: str | None = None + self._colororder: dict[str, int] | None = None self.id = id self.domain = domain @@ -299,7 +298,7 @@ def __init__( # Image instance name = image.name colororder = image.colororder - image = image._A + image = image._A # type: ignore[has-type] elif isinstance(image, list): # list of lists @@ -335,7 +334,7 @@ def __init__( # rescaled 0..255). defer_dtype_to_convert = dtype_given and bool(kwargs) if dtype is not None and not defer_dtype_to_convert: - image = image.astype(dtype, copy=False) + image = image.astype(dtype, copy=False) # type: ignore[call-arg] self.name = name @@ -351,7 +350,7 @@ def __init__( image = image[:, :, 0] # squeeze out singleton plane if kwargs: - image = convert(image, dtype=dtype if defer_dtype_to_convert else None, **kwargs) + image = convert(image, dtype=dtype if defer_dtype_to_convert else None, **kwargs) # type: ignore[arg-type] # assign the image to the object, copying if requested if copy: @@ -371,7 +370,7 @@ def __init__( else: self.colororder = color_dict - self._stats = None + self._stats: dict[str, Any] | None = None self.name = name @@ -415,7 +414,7 @@ def _infer_dtype(image: np.ndarray, dtype: Dtype | bool | None) -> np.dtype | No except TypeError: raise ValueError("bad dtype argument passed to Image constructor") - return dtype + return dtype # type: ignore[return-value] def _reshape_to_size( self, image: np.ndarray, size: tuple | list | None @@ -781,9 +780,9 @@ def __str__(self) -> str: if stats is not None: # self._stats is None if the image is mutable, so skip stats in that case if self.iscolor and self.colororder is not None: - colororder = self.colororder - for k in sorted(stats.keys(), key=lambda x: colororder[x]): - s += f"\n {k:s}: {self._format_stats(stats[k])}" + corder = self.colororder + for plane_name in sorted(stats.keys(), key=lambda x: corder[x]): + s += f"\n {plane_name:s}: {self._format_stats(stats[plane_name])}" else: s += f"\n {self._format_stats(stats)}" @@ -2685,6 +2684,8 @@ def plane(self, planes) -> "Image": :seealso: :meth:`red` :meth:`green` :meth:`blue` :meth:``__getitem__`` """ + colororder: dict[str, Any] | None + iplanes: Any if isinstance(planes, int): if planes == 0 and self.nplanes == 1: return self @@ -2850,7 +2851,7 @@ def __getitem__( if out.ndim == 3: # 3 slices, select uv-region and planes color_names = (self.colororder_str or "").split(":") - plane_key = keys[2] # type: ignore[index] + plane_key = keys[2] # type: ignore[misc] if isinstance(plane_key, slice): selected_planes = color_names[plane_key] else: @@ -3629,7 +3630,7 @@ def __imod__(self, other) -> "Image": # __eq__ and __ne__ intentionally return Image (pixel-wise result) rather than bool. # Unlike __add__ etc., these two are defined on object with return type bool, so # pyright flags the override as incompatible — the ignore is justified here. - def __eq__( # pyright: ignore[reportIncompatibleMethodOverride] + def __eq__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] self, other ) -> "Image": """ @@ -3702,7 +3703,7 @@ def sameas(self, other, colororder: bool = True) -> bool: return bool(np.all((self == other).A)) - def __ne__( # pyright: ignore[reportIncompatibleMethodOverride] # same reason as __eq__ + def __ne__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] # same reason as __eq__ self, other ) -> "Image": """ diff --git a/src/machinevisiontoolbox/ImageFiducials.py b/src/machinevisiontoolbox/ImageFiducials.py index 66bcddd9..7612a0c7 100644 --- a/src/machinevisiontoolbox/ImageFiducials.py +++ b/src/machinevisiontoolbox/ImageFiducials.py @@ -15,6 +15,7 @@ from ansitable import ANSITable, Column from spatialmath import SE3 +from machinevisiontoolbox._image_typing import ImageMixinBase from machinevisiontoolbox.base.graphics import _require_c_contiguous from machinevisiontoolbox.ImagePointFeatures import BaseFeature2D @@ -56,7 +57,7 @@ def _fiducial_dict(dict="4x4_1000"): return dict -class ImageFiducialsMixin(_ImageBase if TYPE_CHECKING else object): +class ImageFiducialsMixin(ImageMixinBase): def fiducial( self, dict: str = "4x4_1000", @@ -141,7 +142,7 @@ def fiducial( # raises IndexError: invalid index to scalar variable. # ------------------------------------------------------------------- - fiducials = [] + fiducials: list[Fiducial] = [] if ids is None or len(ids) == 0: return fiducials # no markers found ids = ids.reshape(-1) @@ -225,6 +226,7 @@ def __init__( self.rvec = rvec self.tvec = tvec self.p3d = p3d + self.stats: Any = None def __str__(self) -> str: """ @@ -298,6 +300,8 @@ def draw(self, image: Image, length: int = 100, thick: int = 2) -> None: if not image.isbgr: raise ValueError("image must have BGR color order") _require_c_contiguous(image._A) + if self.K is None or self.rvec is None or self.tvec is None: + raise ValueError("fiducial has no camera intrinsics or pose to draw") cv2.drawFrameAxes( image=image._A, cameraMatrix=self.K, @@ -422,7 +426,7 @@ def estimatePose( setattr(arucoParams, key, item) detector = cv2.aruco.ArucoDetector(self._dict, arucoParams) - cornerss, ids, rejected = detector.detectMarkers(image=image.mono()._A) + cornerss, ids, rejected = detector.detectMarkers(image=image.mono()._A) # type: ignore[has-type] # corners is a list of ndarray(1,4,2) of marker corners # rejected is a list of rejected corners from tags whose inner code is incorrectly coded @@ -450,11 +454,11 @@ def estimatePose( if not matches: raise ValueError("no markers belonging to this board were detected") cornerss = [corners for corners, _ in matches] - ids = [id for _, id in matches] + ids = [id for _, id in matches] # type: ignore[assignment] # match the markers to the board # print(f"{len(ids)} markers found") - objPoints, imgPoints = self.matchImagePoints(cornerss, ids) + objPoints, imgPoints = self.matchImagePoints(cornerss, ids) # type: ignore[arg-type] # objPoints is ndarray(N,1,3) of 3D points in the board frame # imgPoints is ndarray(N,1,2) of 2D points in the image frame @@ -711,8 +715,8 @@ def chart(self, filename: str | None = None, dpi: int = 100) -> Image | None: else: from PIL import Image as PILImage - img = PILImage.fromarray(img) - img.save(filename, dpi=(dpi, dpi)) + pil_img = PILImage.fromarray(img) + pil_img.save(filename, dpi=(dpi, dpi)) return None diff --git a/src/machinevisiontoolbox/ImageIO.py b/src/machinevisiontoolbox/ImageIO.py index 3a095f4a..621e1d4c 100644 --- a/src/machinevisiontoolbox/ImageIO.py +++ b/src/machinevisiontoolbox/ImageIO.py @@ -21,7 +21,7 @@ import cv2 import matplotlib.patches as mpatches import matplotlib.pyplot as plt -from matplotlib.colors import LinearSegmentedColormap +from matplotlib.colors import Colormap, LinearSegmentedColormap import matplotlib.patheffects as path_effects import numpy as np import scipy as sp @@ -31,6 +31,7 @@ if TYPE_CHECKING: from machinevisiontoolbox._image_typing import _ImageBase +from machinevisiontoolbox._image_typing import ImageMixinBase from machinevisiontoolbox.base import ( colorname, float_image, @@ -50,7 +51,7 @@ # from numpy.lib.arraysetops import isin -class ImageIOMixin(_ImageBase if TYPE_CHECKING else object): +class ImageIOMixin(ImageMixinBase): # ======================= image i/io ================================== # @classmethod @@ -126,7 +127,7 @@ def Read( if image_arr.ndim > 2: colororder = "RGB" if rgb else "BGR" if alpha and image_arr.shape[2] == 4: - colororder += "A" + colororder = (colororder or "") + "A" return cls( image_arr, name=name, colororder=colororder ) # OpenCV file read order) @@ -308,7 +309,7 @@ def showpixels( self, fmt: str | None = None, ax: Any = None, - cmap: str = None, + cmap: str | Colormap | None = None, grid: int | None = 2, badcolor: str | None = None, **kwargs: Any, @@ -404,6 +405,8 @@ def showpixels( plt.clf() ax = plt.gca() + if isinstance(cmap, str): + cmap = plt.get_cmap(cmap) if badcolor is not None: cmap.set_bad(color=badcolor) ax.imshow(image, cmap=cmap, zorder=1) diff --git a/src/machinevisiontoolbox/ImageLineFeatures.py b/src/machinevisiontoolbox/ImageLineFeatures.py index a19b1e91..f6b3095b 100644 --- a/src/machinevisiontoolbox/ImageLineFeatures.py +++ b/src/machinevisiontoolbox/ImageLineFeatures.py @@ -11,12 +11,13 @@ import numpy as np import scipy as sp from spatialmath import base +from machinevisiontoolbox._image_typing import ImageMixinBase if TYPE_CHECKING: from machinevisiontoolbox._image_typing import _ImageBase -class ImageLineFeaturesMixin(_ImageBase if TYPE_CHECKING else object): +class ImageLineFeaturesMixin(ImageMixinBase): """ Line features are common in in many human-built environments. @@ -188,7 +189,7 @@ def plot_lines(self, lines: np.ndarray, *args: Any, **kwargs: Any) -> None: if lines.shape[1] == 2: # Hough lines theta, rho = lines.T - homlines = np.row_stack((np.cos(theta), np.sin(theta), -rho)) + homlines = np.vstack((np.cos(theta), np.sin(theta), -rho)) base.plot_homline(homlines, *args, **kwargs) else: for line in lines: diff --git a/src/machinevisiontoolbox/ImageMorph.py b/src/machinevisiontoolbox/ImageMorph.py index 5c819c93..774efc92 100644 --- a/src/machinevisiontoolbox/ImageMorph.py +++ b/src/machinevisiontoolbox/ImageMorph.py @@ -11,6 +11,7 @@ import cv2 import numpy as np import scipy as sp +from machinevisiontoolbox._image_typing import ImageMixinBase from machinevisiontoolbox.Kernel import Kernel if sys.version_info >= (3, 11): @@ -23,7 +24,7 @@ from machinevisiontoolbox._image_typing import _ImageBase -class ImageMorphMixin(_ImageBase if TYPE_CHECKING else object): +class ImageMorphMixin(ImageMixinBase): """ Image processing morphological operations on the Image class """ @@ -140,7 +141,7 @@ def erode( src=img_array, kernel=self._getse(se), iterations=n, - borderType=self._bordertype_cv(border, exclude=("wrap")), + borderType=self._bordertype_cv(border, exclude=("wrap",)), borderValue=bordervalue, **kwargs, ) @@ -233,7 +234,7 @@ def dilate( src=img_array, kernel=self._getse(se), iterations=n, - borderType=self._bordertype_cv(border, exclude=("wrap")), + borderType=self._bordertype_cv(border, exclude=("wrap",)), borderValue=bordervalue, **kwargs, ) diff --git a/src/machinevisiontoolbox/ImageMultiview.py b/src/machinevisiontoolbox/ImageMultiview.py index d1229ef8..0b0b7b15 100644 --- a/src/machinevisiontoolbox/ImageMultiview.py +++ b/src/machinevisiontoolbox/ImageMultiview.py @@ -10,6 +10,7 @@ import cv2 import numpy as np from spatialmath.base import argcheck, e2h, getvector, h2e, transl2 +from machinevisiontoolbox._image_typing import ImageMixinBase if sys.version_info >= (3, 11): from typing import Self @@ -21,7 +22,7 @@ from machinevisiontoolbox._image_typing import _ImageBase -class ImageMultiviewMixin(_ImageBase if TYPE_CHECKING else object): +class ImageMultiviewMixin(ImageMixinBase): # ======================= stereo ================================== # def stereo_simple( @@ -201,26 +202,26 @@ def DSI_refine( """ DSI_flat = DSI.reshape((-1, DSI.shape[2])) - YP = [] - Y = [] - YN = [] + yp: list[float] = [] + y: list[float] = [] + yn: list[float] = [] disparity = np.argmax(DSI, axis=2) if drange is None: drange = (int(disparity.min()), int(disparity.max())) for i, d in enumerate(disparity.ravel()): if drange[0] < d < drange[1]: - YP.append(DSI_flat[i, d - 1]) - Y.append(DSI_flat[i, d]) - YN.append(DSI_flat[i, d + 1]) + yp.append(DSI_flat[i, d - 1]) + y.append(DSI_flat[i, d]) + yn.append(DSI_flat[i, d + 1]) else: - YP.append(np.nan) - Y.append(np.nan) - YN.append(np.nan) + yp.append(np.nan) + y.append(np.nan) + yn.append(np.nan) - YP = np.array(YP).reshape(DSI.shape[:2]) - Y = np.array(Y).reshape(DSI.shape[:2]) - YN = np.array(YN).reshape(DSI.shape[:2]) + YP = np.array(yp).reshape(DSI.shape[:2]) + Y = np.array(y).reshape(DSI.shape[:2]) + YN = np.array(yn).reshape(DSI.shape[:2]) A = YP + YN - 2 * Y B = YN - YP diff --git a/src/machinevisiontoolbox/ImagePointFeatures.py b/src/machinevisiontoolbox/ImagePointFeatures.py index b46750e3..41fe1dfb 100644 --- a/src/machinevisiontoolbox/ImagePointFeatures.py +++ b/src/machinevisiontoolbox/ImagePointFeatures.py @@ -2,6 +2,7 @@ Detection and matching of keypoint and descriptor features (SIFT, ORB, etc.) in images. """ +import builtins import math from typing import TYPE_CHECKING, Any, Iterator @@ -13,15 +14,16 @@ import spatialmath.base as smb from ansitable import ANSITable, Column -from machinevisiontoolbox.base import ( +from machinevisiontoolbox._image_typing import ImageMixinBase +from machinevisiontoolbox.base.color import name2color +from machinevisiontoolbox.base.findpeaks import findpeaks2d +from machinevisiontoolbox.base.graphics import ( + _require_c_contiguous, draw_circle, draw_line, draw_point, - findpeaks2d, - name2color, - safe_plt_show, ) -from machinevisiontoolbox.base.graphics import _require_c_contiguous +from machinevisiontoolbox.base.imageio import safe_plt_show from machinevisiontoolbox.decorators import ( array_result, array_result2, @@ -282,7 +284,7 @@ def gridify(self, nbins: int | tuple[int, int], nfeat: int) -> "BaseFeature2D": """ try: # Sort features into grid - nw, nh = nbins + nw, nh = nbins # type: ignore[misc] except (TypeError, ValueError): nw = nbins nh = nbins @@ -306,7 +308,7 @@ def gridify(self, nbins: int | tuple[int, int], nfeat: int) -> "BaseFeature2D": return self.__class__(keep) - def __add__(self, other: "BaseFeature2D | list | None") -> "BaseFeature2D": + def __add__(self, other: "BaseFeature2D | builtins.list | None") -> "BaseFeature2D": """ Add feature sets @@ -393,7 +395,7 @@ def __radd__(self, other) -> "BaseFeature2D": @property @scalar_result - def u(self) -> "list[float]": # Horizontal coordinate of feature point + def u(self) -> "builtins.list[float]": # Horizontal coordinate of feature point """ Horizontal coordinate of feature point @@ -415,7 +417,7 @@ def u(self) -> "list[float]": # Horizontal coordinate of feature point @property @scalar_result - def v(self) -> "list[float]": # Vertical coordinate of feature point + def v(self) -> "builtins.list[float]": # Vertical coordinate of feature point """ Vertical coordinate of feature point @@ -436,7 +438,7 @@ def v(self) -> "list[float]": # Vertical coordinate of feature point @property @scalar_result - def id(self) -> "list[int]": # Image id for feature point + def id(self) -> "builtins.list[int]": # Image id for feature point """ Image id for feature point @@ -460,7 +462,7 @@ def id(self) -> "list[int]": # Image id for feature point @property @scalar_result - def orientation(self) -> "list[float]": # Orientation of feature + def orientation(self) -> "builtins.list[float]": # Orientation of feature """ Orientation of feature @@ -482,7 +484,7 @@ def orientation(self) -> "list[float]": # Orientation of feature @property @scalar_result - def scale(self) -> "list[float]": # Scale of feature + def scale(self) -> "builtins.list[float]": # Scale of feature """ Scale of feature @@ -503,7 +505,7 @@ def scale(self) -> "list[float]": # Scale of feature @property @scalar_result - def strength(self) -> "list[float]": # Strength of feature + def strength(self) -> "builtins.list[float]": # Strength of feature """ Strength of feature @@ -524,7 +526,7 @@ def strength(self) -> "list[float]": # Strength of feature @property @scalar_result - def octave(self) -> "list[int]": # Octave of feature + def octave(self) -> "builtins.list[int]": # Octave of feature """ Octave of feature @@ -870,6 +872,7 @@ def sort( new._descriptor = np.take(self._descriptor, sorted_index, axis=0) new._feature_type = self._feature_type return new + return None def support(self, images: Any, N: int = 50) -> Any: """ @@ -1235,7 +1238,8 @@ def draw( else: if len(args) == 0 and len(kwargs) == 0: kwargs = dict(marker="+y") - draw_point(img, self.p, *args, fontsize=0.6, **kwargs) + kwargs.setdefault("fontsize", 0.6) + draw_point(img, self.p, *args, **kwargs) def draw2(self, image: Any, color: Any = "y", type: str = "point") -> Any: """ @@ -1259,7 +1263,7 @@ def draw2(self, image: Any, color: Any = "y", type: str = "point") -> Any: img = image._A # Draw features into image _require_c_contiguous(img) if isinstance(color, str): - color_ndarray: np.ndarray = name2color( + color_ndarray: Any = name2color( color, dtype=image.dtype, colororder=image.colororder ) color_scalar: tuple[float, float, float, float] = ( @@ -1338,8 +1342,8 @@ def __init__(self, m, fv1: BaseFeature2D, fv2: BaseFeature2D, inliers=None) -> N self._kp1 = fv1 self._kp2 = fv2 self._inliers = inliers - self._inverse_dict1 = None - self._inverse_dict2 = None + self._inverse_dict1: dict[int, int] | None = None + self._inverse_dict2: dict[int, int] | None = None def __getitem__(self, i) -> "FeatureMatch": """ @@ -1461,15 +1465,14 @@ def by_id1(self, id: int) -> "FeatureMatch | None": """ if self._inverse_dict1 is None: # first call, build a dict for efficient mapping - d = {} + d: dict[int, int] = {} for k, m in enumerate(self._matches): d[m[0]] = k self._inverse_dict1 = d - else: - try: - return self[self._inverse_dict1[id]] - except KeyError: - return None + try: + return self[self._inverse_dict1[id]] + except KeyError: + return None def by_id2(self, i: int) -> "FeatureMatch | None": """ @@ -1499,15 +1502,14 @@ def by_id2(self, i: int) -> "FeatureMatch | None": """ if self._inverse_dict2 is None: # first call, build a dict for efficient mapping - d = {} + d: dict[int, int] = {} for k, m in enumerate(self._matches): - d[m[0]] = k + d[m[1]] = k self._inverse_dict2 = d - else: - try: - return self[self._inverse_dict2[i]] - except KeyError: - return None + try: + return self[self._inverse_dict2[i]] + except KeyError: + return None def __str__(self) -> str: """ @@ -1762,7 +1764,7 @@ def estimate( @property @scalar_result - def distance(self) -> "list[float]": + def distance(self) -> "builtins.list[float]": """ Distance between corresponding features @@ -1784,7 +1786,7 @@ def distance(self) -> "list[float]": @property @array_result2 - def p1(self) -> "list[np.ndarray]": + def p1(self) -> "builtins.list[np.ndarray]": """ Feature coordinate in first image @@ -1806,7 +1808,7 @@ def p1(self) -> "list[np.ndarray]": @property @array_result2 - def p2(self) -> "list[np.ndarray]": + def p2(self) -> "builtins.list[np.ndarray]": """ Feature coordinate in second image @@ -1828,7 +1830,7 @@ def p2(self) -> "list[np.ndarray]": @property @array_result - def descriptor1(self) -> "list[np.ndarray]": + def descriptor1(self) -> "builtins.list[np.ndarray]": """ Feature descriptor in first image @@ -1850,7 +1852,7 @@ def descriptor1(self) -> "list[np.ndarray]": @property @array_result - def descriptor2(self) -> "list[np.ndarray]": + def descriptor2(self) -> "builtins.list[np.ndarray]": """ Feature descriptor in second image @@ -1872,7 +1874,7 @@ def descriptor2(self) -> "list[np.ndarray]": @property @scalar_result - def id1(self) -> "list[int]": + def id1(self) -> "builtins.list[int]": """ Feature id in first image @@ -1894,7 +1896,7 @@ def id1(self) -> "list[int]": @property @scalar_result - def id2(self) -> "list[int]": + def id2(self) -> "builtins.list[int]": """ Feature id in second image @@ -2068,7 +2070,7 @@ def _resolve_feature_create(name: str) -> Any: # ------------------------------------------------------------------------- -class ImagePointFeaturesMixin(_ImageBase if TYPE_CHECKING else object): +class ImagePointFeaturesMixin(ImageMixinBase): def _image2feature( self, cls, @@ -2078,7 +2080,7 @@ def _image2feature( scale=False, orient=False, **kwargs, - ) -> BaseFeature2D: + ) -> Any: # https://datascience.stackexchange.com/questions/43213/freak-feature-extraction-opencv algorithms: dict[str, Any] = { "SIFT": getattr(cv2, "SIFT_create"), @@ -2367,12 +2369,12 @@ def ComboFeature( } descriptors = { - "BOOST": (cv2.xfeatures2d.BoostDesc_create, BOOSTFeature), - "BRIEF": (cv2.xfeatures2d.BriefDescriptorExtractor_create, BRIEFFeature), - "DAISY": (cv2.xfeatures2d.BriefDescriptorExtractor_create, DAISYFeature), - "FREAK": (cv2.xfeatures2d.BriefDescriptorExtractor_create, FREAKFeature), - "LATCH": (cv2.xfeatures2d.BriefDescriptorExtractor_create, LATCHFeature), - "LUCID": (cv2.xfeatures2d.BriefDescriptorExtractor_create, LUCIDFeature), + "BOOST": (cv2.xfeatures2d.BoostDesc_create, BOOSTFeature), # type: ignore[attr-defined] + "BRIEF": (cv2.xfeatures2d.BriefDescriptorExtractor_create, BRIEFFeature), # type: ignore[attr-defined] + "DAISY": (cv2.xfeatures2d.BriefDescriptorExtractor_create, DAISYFeature), # type: ignore[attr-defined] + "FREAK": (cv2.xfeatures2d.BriefDescriptorExtractor_create, FREAKFeature), # type: ignore[attr-defined] + "LATCH": (cv2.xfeatures2d.BriefDescriptorExtractor_create, LATCHFeature), # type: ignore[attr-defined] + "LUCID": (cv2.xfeatures2d.BriefDescriptorExtractor_create, LUCIDFeature), # type: ignore[attr-defined] } # eg. Feature2D('FAST', 'FREAK') if detector in detectors: @@ -2384,6 +2386,8 @@ def ComboFeature( else: raise ValueError("unknown detector") + raise NotImplementedError("ComboFeature is a work in progress") + class _Harris_create: def __init__( diff --git a/src/machinevisiontoolbox/ImageProcessing.py b/src/machinevisiontoolbox/ImageProcessing.py index 1b33b44e..d62ac80e 100644 --- a/src/machinevisiontoolbox/ImageProcessing.py +++ b/src/machinevisiontoolbox/ImageProcessing.py @@ -7,7 +7,7 @@ import os.path from collections import namedtuple from pathlib import Path -from typing import TYPE_CHECKING, Any, Callable +from typing import TYPE_CHECKING, Any, Callable, Self import warnings import cv2 @@ -19,26 +19,19 @@ from scipy import interpolate from spatialmath.base import argcheck, e2h, getvector, h2e, transl2 -from machinevisiontoolbox.base import ( - colorname, - float_image, - idisp, - int_image, - iread, - iwrite, - name2color, - safe_plt_show, -) +from machinevisiontoolbox._image_typing import ImageMixinBase +from machinevisiontoolbox.base.color import name2color +from machinevisiontoolbox.base.imageio import iwrite, safe_plt_show if TYPE_CHECKING: from machinevisiontoolbox.ImageCore import Image from machinevisiontoolbox._image_typing import _ImageBase -class ImageProcessingMixin(_ImageBase if TYPE_CHECKING else object): +class ImageProcessingMixin(ImageMixinBase): # ======================= image processing ============================= # - def LUT(self, lut: Any, colororder: str | dict[str, Any] | None = None) -> "Image": + def LUT(self, lut: Any, colororder: str | dict[str, Any] | None = None) -> Self: """ Apply lookup table @@ -95,7 +88,7 @@ def LUT(self, lut: Any, colororder: str | dict[str, Any] | None = None) -> "Imag def apply( self, func: Callable[..., Any], vectorize: bool = False, **kwargs - ) -> "Image": + ) -> Self: """ Apply a function to an image @@ -160,7 +153,7 @@ def apply2( func: Callable[..., Any], vectorize: bool = False, **kwargs, - ) -> "Image": + ) -> Self: """ Apply a function to two images @@ -221,7 +214,7 @@ def apply2( func = np.vectorize(func) return self.__class__(func(self._A, other._A), colororder=self.colororder) - def clip(self, min: int | float, max: int | float) -> "Image": + def clip(self, min: int | float, max: int | float) -> Self: """ Clip pixel values @@ -253,7 +246,7 @@ def roll( rv: int = 0, dx: int | None = None, dy: int | None = None, - ) -> "Image": + ) -> Self: """ Roll image by row or column @@ -284,7 +277,7 @@ def roll( return self.__class__(np.roll(self._A, (ru, rv), (1, 0))) - def normhist(self) -> "Image": + def normhist(self) -> Self: """ Histogram normalisaton @@ -320,7 +313,7 @@ def normhist(self) -> "Image": def stretch( self, max: int | float = 1, range: Any = None, clip: bool = True - ) -> "Image": + ) -> Self: """ Image normalisation @@ -530,7 +523,7 @@ def threshold( if as_bool: false_value: bool | int = False true_value: bool | int = True - binary_dtype = np.bool_ + binary_dtype: Any = np.bool_ else: false_value = 0 true_value = 255 @@ -722,14 +715,14 @@ def apply_mode(threshold): layout=widgets.Layout(width="80%"), ) - def update(change): + def update_widget(change): val = change["new"] im.set_data(apply_mode(val)) lower_limit_line.set_xdata([val, val]) fig.canvas.draw_idle() thresh_holder[0] = val - slider.observe(update, names="value") + slider.observe(update_widget, names="value") safe_plt_show(block=False) display(slider) return self.cast(thresh_holder[0]) @@ -766,7 +759,7 @@ def threshold_adaptive( h: int = 3, method: str = "mean", blocksize: int | None = None, - ) -> "Image": + ) -> Self: r""" Adaptive threshold @@ -985,7 +978,7 @@ def blend( beta: float | None = None, gamma: float = 0, dtype: str | None = None, - ) -> "Image": + ) -> Self: r""" Image blending @@ -1034,11 +1027,11 @@ def blend( raise ValueError("images must be both int or both floating type") if dtype is None: - dtype = -1 + cvdepth = -1 elif dtype in ("float", "float32"): - dtype = cv2.CV_32F + cvdepth = cv2.CV_32F elif dtype in ("double", "float64"): - dtype = cv2.CV_64F + cvdepth = cv2.CV_64F else: raise ValueError("dtype must be 'float', 'double', 'float32', or 'float64'") @@ -1053,11 +1046,11 @@ def blend( src2=image2._A, beta=beta, gamma=gamma, - dtype=dtype, + dtype=cvdepth, ) return self.__class__(out, colororder=self.colororder) - def choose(self, image2: Any, mask: Any) -> "Image": + def choose(self, image2: Any, mask: Any) -> Self: r""" Pixel-wise image merge @@ -1142,7 +1135,7 @@ def choose(self, image2: Any, mask: Any) -> "Image": # possible color value if isinstance(image2, str): # it's a colorname, look it up - color = self.like(name2color(image2)) + color = self.like(name2color(image2)) # type: ignore[arg-type] else: try: color = argcheck.getvector(image2, 3) @@ -1178,7 +1171,7 @@ def paste( position: str = "topleft", copy: bool = False, zero: bool = True, - ) -> "Image": + ) -> Self: """ Paste an image into an image @@ -1339,12 +1332,12 @@ def paste( # only fg is valid fg_mask = cv2.bitwise_and( - src1=fg_set, src2=cv2.bitwise_xor(src1=bg_set, src2=1) + src1=fg_set, src2=cv2.bitwise_xor(src1=bg_set, src2=1) # type: ignore[call-overload] ) # only bg is valid bg_mask = cv2.bitwise_and( - src1=cv2.bitwise_xor(src1=fg_set, src2=1), src2=bg_set + src1=cv2.bitwise_xor(src1=fg_set, src2=1), src2=bg_set # type: ignore[call-overload] ) # merge them @@ -1360,7 +1353,7 @@ def paste( return self.__class__(o, copy=True, colororder=colororder) - def invert(self) -> "Image": + def invert(self) -> Self: r""" Invert image diff --git a/src/machinevisiontoolbox/ImageRegionFeatures.py b/src/machinevisiontoolbox/ImageRegionFeatures.py index ce33c3e5..112c2613 100644 --- a/src/machinevisiontoolbox/ImageRegionFeatures.py +++ b/src/machinevisiontoolbox/ImageRegionFeatures.py @@ -11,8 +11,9 @@ from spatialmath import SE3 from typing import TYPE_CHECKING, Any +from machinevisiontoolbox._image_typing import ImageMixinBase from machinevisiontoolbox.decorators import array_result -from machinevisiontoolbox.base import plot_labelbox +from machinevisiontoolbox.base.graphics import plot_labelbox try: import pytesseract as _pytesseract @@ -26,7 +27,7 @@ from machinevisiontoolbox._image_typing import _ImageBase -class ImageRegionFeaturesMixin(_ImageBase if TYPE_CHECKING else object): +class ImageRegionFeaturesMixin(ImageMixinBase): def MSER(self, **kwargs: Any) -> "MSERFeature": """ Find MSER features in image @@ -160,8 +161,10 @@ def __init__(self, image: Any = None, **kwargs: Any) -> None: :seealso: :meth:`bbox` :meth:`points` """ + self._points: list[np.ndarray] = [] + self._bboxes: np.ndarray = np.zeros((0, 4), dtype=int) if image is not None: - detector = cv2.MSER_create(**kwargs) + detector = cv2.MSER.create(**kwargs) msers, bboxes = detector.detectRegions(image._A) # msers is a tuple of ndarray(M,2), each row is (u,v) @@ -171,7 +174,7 @@ def __init__(self, image: Any = None, **kwargs: Any) -> None: # https://www.toptal.com/machine-learning/real-time-object-detection-using-mser-in-ios self._points = [ - mser.T for mser in msers + np.asarray(mser).T for mser in msers ] # transpose point arrays to be Nx2 # --- OpenCV 4/5 compat --------------------------------------- # When no regions are found, OpenCV 5's detectRegions() returns @@ -187,10 +190,13 @@ def __init__(self, image: Any = None, **kwargs: Any) -> None: # change. Logged as its own issue (#54), not something this # compat fix can address. # ----------------------------------------------------------------- - if len(bboxes) == 0: - bboxes = np.zeros((0, 4), dtype=int) - bboxes[:, 2:] = bboxes[:, 0:2] + bboxes[:, 2:] # convert to lrtb - self._bboxes = bboxes + lrtb = ( + np.zeros((0, 4), dtype=int) + if len(bboxes) == 0 + else np.array(bboxes) + ) + lrtb[:, 2:] = lrtb[:, 0:2] + lrtb[:, 2:] # convert to lrtb + self._bboxes = lrtb def __len__(self) -> int: """ @@ -240,7 +246,7 @@ def __getitem__(self, i: int | slice | np.ndarray | list | tuple) -> "MSERFeatur new = self.__class__() if isinstance(i, int): - new._points = self._points[i] + new._points = self._points[i] # type: ignore[assignment] new._bboxes = self._bboxes[np.newaxis, i, :] # result is 2D elif isinstance(i, slice): new._points = self._points[i] diff --git a/src/machinevisiontoolbox/ImageReshape.py b/src/machinevisiontoolbox/ImageReshape.py index 5584ffbb..2dcb2960 100644 --- a/src/machinevisiontoolbox/ImageReshape.py +++ b/src/machinevisiontoolbox/ImageReshape.py @@ -23,6 +23,7 @@ import machinevisiontoolbox.base as mvb from typing import TYPE_CHECKING +from machinevisiontoolbox._image_typing import ImageMixinBase if TYPE_CHECKING: from machinevisiontoolbox._image_typing import _ImageBase @@ -51,7 +52,7 @@ def _bound(*args: Any, **kwargs: Any) -> Any: return _bound -class ImageReshapeMixin(_ImageBase if TYPE_CHECKING else object): +class ImageReshapeMixin(ImageMixinBase): def trim( self, left: int = 0, right: int = 0, top: int = 0, bottom: int = 0 ) -> Self: @@ -968,7 +969,9 @@ def rotate_spherical(self, R: Any) -> Self: @classinstancemethod def meshgrid( - self: type[Self] | Self, width: int | None = None, height: int | None = None + self: "type[ImageReshapeMixin] | ImageReshapeMixin", + width: int | None = None, + height: int | None = None, ) -> tuple[np.ndarray, np.ndarray]: """ Coordinate arrays for image diff --git a/src/machinevisiontoolbox/ImageSpatial.py b/src/machinevisiontoolbox/ImageSpatial.py index a7190093..a51ca179 100644 --- a/src/machinevisiontoolbox/ImageSpatial.py +++ b/src/machinevisiontoolbox/ImageSpatial.py @@ -9,13 +9,14 @@ import warnings from typing import TYPE_CHECKING, Any, Callable +from machinevisiontoolbox._image_typing import ImageMixinBase from machinevisiontoolbox.Kernel import Kernel if TYPE_CHECKING: from machinevisiontoolbox._image_typing import _ImageBase -class ImageSpatialMixin(_ImageBase if TYPE_CHECKING else object): +class ImageSpatialMixin(ImageMixinBase): @classmethod def Gauss(cls, sigma: float, h: int | None = None) -> Kernel: return Kernel.Gauss(sigma=sigma, h=h) @@ -87,39 +88,6 @@ def _bordertype_cv( except KeyError: raise ValueError(border, "border is not a valid option") - # border options: - _border_opt = { - "constant": cv2.BORDER_CONSTANT, - "replicate": cv2.BORDER_REPLICATE, - "reflect": cv2.BORDER_REFLECT, - "mirror": cv2.BORDER_REFLECT_101, - "wrap": cv2.BORDER_WRAP, - "pad": cv2.BORDER_CONSTANT, - "none": cv2.BORDER_ISOLATED, - } - - @staticmethod - def _border_args_cv( - border: str | int | float, - morpho: bool = False, - allow: list[str] | tuple[str, ...] | None = None, - disallow: list[str] | tuple[str, ...] | None = None, - ) -> dict[str, Any]: - if disallow is not None and border in disallow: - raise ValueError(f"border option {border} not supported") - if allow is not None and border not in allow: - raise ValueError(f"border option {border} not supported") - - if isinstance(border, str): - # given as string, convert to OpenCV flag value - try: - return dict(borderType=_border_opt[border]) - except KeyError: - raise ValueError(border, "border is not a valid option") - elif isinstance(border, int) or isinstance(border, float): - # given as a numeric value, assume 'pad' - return dict(bordertype=_border_opt["pad"], borderValu=border_value) - @staticmethod def _bordertype_sp( border: str, exclude: list[str] | tuple[str, ...] | None = None @@ -325,24 +293,28 @@ def convolve( self._A, "multiple-channel", "CV_8U", "CV_16U", "CV_16S", "CV_32F", "CV_64F" ) img = self._A - if border == "pad" and value != 0: + crop = False + if border == "pad" and bordervalue != 0 and mode == "same": + # OpenCV can only pad with zero, so pad explicitly, filter, then + # crop back to the original size img = cv2.copyMakeBorder( - src=a, - top=kv, - bottom=kv, - left=kh, - right=kh, + src=img, + top=kh, + bottom=kh, + left=kw, + right=kw, borderType=cv2.BORDER_CONSTANT, value=bordervalue, ) + crop = True elif mode == "full": img = cv2.copyMakeBorder( - src=a, - top=kv, - bottom=kv, - left=kh, - right=kh, - borderType=self._bordertype_cv(border), + src=img, + top=kh, + bottom=kh, + left=kw, + right=kw, + borderType=cv2.BORDER_CONSTANT, value=bordervalue, ) @@ -350,11 +322,11 @@ def convolve( src=img, ddepth=-1, kernel=K, borderType=self._bordertype_cv(border) ) - if mode == "valid": + if mode == "valid" or crop: if out.ndim == 2: - out = out[kh:-kh, kw:-kw] + out = out[kh : out.shape[0] - kh, kw : out.shape[1] - kw] else: - out = out[kh:-kh, kw:-kw, :] + out = out[kh : out.shape[0] - kh, kw : out.shape[1] - kw, :] return self.__class__(out, colororder=self.colororder) # def sobel(self, kernel=None): @@ -732,7 +704,7 @@ def pyramid( if impyr.shape[0] == 1 or impyr.shape[1] == 1: break impyr = cv2.pyrDown( - src=impyr, borderType=self._bordertype_cv(border, exclude="constant") + src=impyr, borderType=self._bordertype_cv(border, exclude=("constant",)) ) pyr.append(impyr) @@ -888,7 +860,10 @@ def rankfilter( w = 2 * h + 1 footprint = np.ones((w, w)) - n = np.sum(footprint) + if footprint is None: + raise ValueError("either footprint or h must be given") + + n = int(np.sum(footprint)) if isinstance(rank, str): if rank in ("min", "minimum"): @@ -897,6 +872,8 @@ def rankfilter( rank = 0 elif rank in ("med", "median"): rank = n // 2 + else: + raise ValueError(rank, "unknown rank name") elif not isinstance(rank, int): raise TypeError(rank, "rank must be int or str") @@ -1103,22 +1080,20 @@ def labels_binary( :meth:`labels_MSER` """ if not (connectivity in [4, 8]): - raise ValueError(conn, "connectivity must be 4 or 8") + raise ValueError(connectivity, "connectivity must be 4 or 8") self._opencv_type_check(self._A, "single-channel", "CV_8U") # make labels uint32s, unique and never recycled? # set ltype to default to cv2.CV_32S if ltype == "int32": - ltype = cv2.CV_32S - dtype = np.int32 + cvtype = cv2.CV_32S elif ltype == "uint16": - ltype = cv2.CV_16U - dtype = np.uint16 + cvtype = cv2.CV_16U else: raise TypeError(ltype, "ltype must be either int32 or uint16") retval, labels = cv2.connectedComponents( - image=self.array_as("uint8"), connectivity=connectivity, ltype=ltype + image=self.array_as("uint8"), connectivity=connectivity, ltype=cvtype ) return self.__class__(labels), retval @@ -1163,19 +1138,16 @@ def labels_MSER(self, **kwargs: Any) -> tuple[Any, int]: `opencv.MSER_create `_ """ - mser = cv2.MSER_create(**kwargs) + mser = cv2.MSER.create(**kwargs) regions, _ = mser.detectRegions(self.array_as("uint8")) - if len(regions) < 256: - dtype = np.uint8 - else: - dtype = np.uint32 - + dtype: Any = np.uint8 if len(regions) < 256 else np.uint32 out = np.zeros(self.shape, dtype=dtype) for i, points in enumerate(regions): # print('region ', i, points.shape[0]) - out[points[:, 1], points[:, 0]] = i + pts = np.asarray(points) + out[pts[:, 1], pts[:, 0]] = i return self.__class__(out, dtype=dtype), len(regions) @@ -1216,7 +1188,7 @@ def labels_graphseg( ) out = segmenter.processImage(self.array_as("uint8")) - return self.__class__(out), np.max(out) + 1 + return self.__class__(out), int(np.max(out)) + 1 # -------------------- similarity operations -------------------------- # diff --git a/src/machinevisiontoolbox/ImageTensor.py b/src/machinevisiontoolbox/ImageTensor.py index 683b4430..7e61acaa 100644 --- a/src/machinevisiontoolbox/ImageTensor.py +++ b/src/machinevisiontoolbox/ImageTensor.py @@ -4,6 +4,7 @@ from __future__ import annotations +import sys from typing import TYPE_CHECKING, Any if TYPE_CHECKING: @@ -12,6 +13,12 @@ import numpy as np +if sys.version_info >= (3, 11): + from typing import Self +else: + from typing_extensions import Self +from machinevisiontoolbox._image_typing import ImageMixinBase + try: import torch @@ -23,7 +30,7 @@ # TODO need a resizing option for the torch() method, to allow scaling to a fixed size for model input -class ImageTensorMixin(_ImageBase if TYPE_CHECKING else object): +class ImageTensorMixin(ImageMixinBase): """ PyTorch integration methods for the Image class. @@ -128,7 +135,7 @@ def Tensor( logits: bool = False, colororder: str | None = None, dtype: Any = None, - ) -> "Image": + ) -> Self: """Create an Image from a PyTorch tensor. :param data: tensor input, either a ``torch.Tensor`` of shape diff --git a/src/machinevisiontoolbox/ImageWholeFeatures.py b/src/machinevisiontoolbox/ImageWholeFeatures.py index 7fa2a061..1296134c 100644 --- a/src/machinevisiontoolbox/ImageWholeFeatures.py +++ b/src/machinevisiontoolbox/ImageWholeFeatures.py @@ -4,7 +4,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Literal +from typing import TYPE_CHECKING, Any, Literal, cast import warnings import cv2 @@ -19,12 +19,14 @@ if TYPE_CHECKING: from machinevisiontoolbox._image_typing import _ImageBase -from machinevisiontoolbox.base import findpeaks, findpeaks2d +from machinevisiontoolbox._image_typing import ImageMixinBase +from machinevisiontoolbox.base.findpeaks import findpeaks, findpeaks2d from machinevisiontoolbox.base.imageio import safe_plt_show, set_window_title from machinevisiontoolbox.mvtb_types import * -class ImageWholeFeaturesMixin(_ImageBase if TYPE_CHECKING else object): +class ImageWholeFeaturesMixin(ImageMixinBase): + _stats: dict[str, Any] | None # ------------------ scalar statistics ----------------------------- # @@ -266,7 +268,7 @@ def stats(self) -> dict[str, Any]: :seealso: :meth:`printstats` :meth:`hist` :meth:`min` :meth:`max` :meth:`mean` :meth:`std` :meth:`median` """ - if self._stats is None: + if getattr(self, "_stats", None) is None: if self.iscolor and self.colororder is not None: self._stats = { k: self._plane_stats(plane._A) @@ -274,7 +276,7 @@ def stats(self) -> dict[str, Any]: } else: self._stats = self._plane_stats(self._A) - return self._stats + return cast(dict[str, Any], self._stats) def printstats(self) -> None: """ @@ -1378,9 +1380,9 @@ def plot( # Ensure one y-limit per plotted plane, even if maxy is a NumPy scalar. maxy_arr = np.asarray(maxy) if maxy_arr.ndim == 0: - maxy = np.repeat(float(maxy_arr), self.nplanes) + maxy_planes = np.repeat(float(maxy_arr), self.nplanes) else: - maxy = maxy_arr.reshape(-1) + maxy_planes = maxy_arr.reshape(-1) hist_counts = self.h if self.nplanes == 1: @@ -1419,7 +1421,7 @@ def plot( for i, ax in enumerate(axes): bin_width = x[1] - x[0] ax.stairs( - y[:, i], np.append(x, x[-1] + bin_width), fill=filled, **kwargs + y[:, i], np.append(x, x[-1] + bin_width), fill=bool(filled), **kwargs ) ax.grid() if n == 1: @@ -1427,7 +1429,7 @@ def plot( else: ax.set_ylabel(ylabel1 + " (" + colors[i] + ")") ax.set_xlim(*xrange) - ax.set_ylim(0, maxy[i]) + ax.set_ylim(0, maxy_planes[i]) ax.yaxis.set_major_formatter( ScalarFormatter(useOffset=False, useMathText=True) ) @@ -1570,7 +1572,7 @@ def on_move(event): label="median", ) ax.set_xlim(*xrange) - ax.set_ylim(0, maxy) + ax.set_ylim(0, float(np.max(maxy))) ax.yaxis.set_major_formatter( ScalarFormatter(useOffset=False, useMathText=True) ) diff --git a/src/machinevisiontoolbox/Kernel.py b/src/machinevisiontoolbox/Kernel.py index 82a5cd71..bec1c735 100644 --- a/src/machinevisiontoolbox/Kernel.py +++ b/src/machinevisiontoolbox/Kernel.py @@ -709,9 +709,9 @@ def Circle( # check valid input: if not argcheck.isscalar(radius): # r.shape[1] > 1: - radius = argcheck.getvector(radius) - rmax = radius.max() - rmin = radius.min() + rvec = argcheck.getvector(radius) + rmax = rvec.max() + rmin = rvec.min() else: rmax = radius @@ -734,7 +734,7 @@ def Circle( # annulus case x = np.arange(w) - c X, Y = np.meshgrid(x, x) - ll = np.where(np.round((X**2 + Y**2 - radius**2) <= 0)) + ll = np.where(np.round((X**2 + Y**2 - radius**2) <= 0)) # type: ignore[operator] s[ll] = 1 if normalize: diff --git a/src/machinevisiontoolbox/PointCloud.py b/src/machinevisiontoolbox/PointCloud.py index 14305878..cfd10d63 100644 --- a/src/machinevisiontoolbox/PointCloud.py +++ b/src/machinevisiontoolbox/PointCloud.py @@ -261,8 +261,8 @@ def Read(cls, filename: str, *args: Any, **kwargs: Any) -> "PointCloud": if parts and parts[0] == "data": filename = str(Path(*parts[1:])) if len(parts) > 1 else "" - filename = mvtb_path_to_datafile("data", filename, string=True) - pcd = o3d.io.read_point_cloud(filename, *args, **kwargs) + path = mvtb_path_to_datafile("data", filename, string=True) + pcd = o3d.io.read_point_cloud(str(path), *args, **kwargs) # type: ignore[arg-type] # stubs say PathLike return cls(pcd) @classmethod @@ -346,7 +346,7 @@ def write(self, filename: str) -> bool: The file format is determined by the file extension. """ - return o3d.io.write_point_cloud(filename, self._pcd) + return o3d.io.write_point_cloud(str(filename), self._pcd) # type: ignore[arg-type] def disp(self, block: bool = False, file: str | None = None, **kwargs: Any) -> None: """ @@ -398,7 +398,7 @@ def disp(self, block: bool = False, file: str | None = None, **kwargs: Any) -> N # save to file if requested if file is not None: - vis.capture_screen_image(str(file), do_render=False) + vis.capture_screen_image(str(file), do_render=False) # type: ignore[arg-type] def __rmul__(self, T: SE3) -> "PointCloud": """ @@ -521,7 +521,7 @@ def downsample_random( np.random.seed(seed) n = len(self) ind = np.random.choice(n, int(fraction * n), replace=False) - return self.__class__(self._pcd.select_by_index(ind)) + return self.__class__(self._pcd.select_by_index(ind.tolist())) def estimate_normals(self, **kwargs: Any) -> None: """ @@ -622,10 +622,10 @@ def select( """ if callable(ind): - ind = np.where(np.apply_along_axis(ind, axis=0, arr=self.points))[0] + idx = np.where(np.apply_along_axis(ind, axis=0, arr=self.points))[0] else: - ind = np.array(ind) - return self.__class__(self._pcd.select_by_index(ind, invert=invert)) + idx = np.array(ind) + return self.__class__(self._pcd.select_by_index(idx.tolist(), invert=invert)) def paint(self, color: Any) -> "PointCloud": """ @@ -708,7 +708,7 @@ def write(self, filename: str) -> None: :seealso: :obj:`open3d.io.write_voxel_grid` """ - o3d.io.write_voxel_grid(filename, self._voxels) + o3d.io.write_voxel_grid(str(filename), self._voxels) # type: ignore[arg-type] def disp(self, block: bool = True, file: str | None = None, **kwargs: Any) -> None: """ @@ -749,7 +749,7 @@ def disp(self, block: bool = True, file: str | None = None, **kwargs: Any) -> No # save to file if requested if file is not None: - vis.capture_screen_image(str(file), do_render=False) + vis.capture_screen_image(str(file), do_render=False) # type: ignore[arg-type] if __name__ == "__main__": diff --git a/src/machinevisiontoolbox/Sources.py b/src/machinevisiontoolbox/Sources.py index 6675221c..08442f07 100644 --- a/src/machinevisiontoolbox/Sources.py +++ b/src/machinevisiontoolbox/Sources.py @@ -54,11 +54,11 @@ _rosbags_available = True except ImportError: - _RosBagReader1 = None - _RosBagReader2 = None - _SerdeError = None - _Stores = None - _get_typestore = None + _RosBagReader1 = None # type: ignore[misc,assignment] + _RosBagReader2 = None # type: ignore[misc,assignment] + _SerdeError = None # type: ignore[misc,assignment] + _Stores = None # type: ignore[misc,assignment] + _get_typestore = None # type: ignore[assignment] _rosbags_available = False # What rosbags raises when a serialized message is not exactly the length its type @@ -81,7 +81,7 @@ _open3d_available = True except ImportError: - o3d = None + o3d = None # type: ignore[assignment] _open3d_available = False try: @@ -1011,7 +1011,7 @@ def _query_macos() -> list[str]: def _names_linux() -> dict[int, str]: """Read /sys/class/video4linux/videoN/name on Linux.""" - result = {} + result: dict[int, str] = {} sysfs = pathlib.Path("/sys/class/video4linux") if not sysfs.exists(): return result @@ -1727,18 +1727,19 @@ def __next__(self) -> Image | bytes: else: raise StopIteration - im = self._read(self.i) - if isinstance(im, np.ndarray): - if im.ndim == 3 and not _wants_mono(self.args): + raw = self._read(self.i) + im: Any = raw + if isinstance(raw, np.ndarray): + if raw.ndim == 3 and not _wants_mono(self.args): im = _make_image( - im, + raw, id=self.i, name=self.files[self.i], colororder="BGR", **self.args, ) else: - im = _make_image(im, id=self.i, name=self.files[self.i], **self.args) + im = _make_image(raw, id=self.i, name=self.files[self.i], **self.args) self.i += 1 return im @@ -2592,7 +2593,7 @@ def _process_frame(self, msg: dict) -> None: } dtype, channels = _enc.get(encoding, (np.uint8, 3)) arr = np.frombuffer(img_data, dtype=dtype) - frame = ( + frame: Any = ( arr.reshape((h, w, channels)) if channels > 1 else arr.reshape((h, w)) ) # Normalise to BGR uint8 for consistency with the compressed path @@ -2927,8 +2928,8 @@ def __init__(self, streams: list[ROSTopic], tolerance: float = 0.02) -> None: self.tolerance = tolerance self._tol_ns = int(round(tolerance * 1_000_000_000)) - self._iters = [] - self._buffers = [] + self._iters: list[Any] = [] + self._buffers: list[Any] = [] @staticmethod def _timestamp_ns(item: Any) -> int: @@ -3142,8 +3143,8 @@ def __init__( self._topic_filter = topicfilter self._msgfilter = msgfilter self.verbose = verbose - self.reader = None - self.connections = [] + self.reader: Any = None + self.connections: list[Any] = [] self.typestore = _resolve_typestore(release) self.dtype = dtype self.colororder = colororder @@ -3303,7 +3304,7 @@ def _open_reader(self) -> Any: # Resolve via mvtb_path_to_datafile: returns path unchanged if the file # exists locally, otherwise searches the mvtb-data companion package. try: - path = mvtb_path_to_datafile("data", path) + path = str(mvtb_path_to_datafile("data", path)) except (ValueError, ModuleNotFoundError): pass # fall through; rosbags will raise a clear error on open @@ -3541,7 +3542,7 @@ def _autodetect_typestore(self, rawdata: bytes, msgtype: str) -> bool: if self.verbose: print(f" auto-detect: {name} (ros1 encoding)") return True - except (*_LENGTH_ERRORS, KeyError, UnicodeDecodeError, struct.error) as e: + except (*_LENGTH_ERRORS, KeyError, UnicodeDecodeError, struct.error) as e: # type: ignore[misc] # wrong store, or the message is the wrong length for it if self.verbose: print( @@ -3579,7 +3580,7 @@ def _autodetect_typestore(self, rawdata: bytes, msgtype: str) -> bool: KeyError, UnicodeDecodeError, struct.error, - ) as e: + ) as e: # type: ignore[misc] if self.verbose: print( f" auto-detect: {name} (CDR) failed: {type(e).__name__}: {e}" @@ -3788,7 +3789,7 @@ def __iter__(self) -> Iterator[Any]: if isinstance(self.dtype, dict): dtype = self.dtype.get(connection.topic, "uint8") else: - dtype = self.dtype or "uint8" + dtype = self.dtype or "uint8" # type: ignore[assignment] if self.colororder is not None: # command line override @@ -3856,7 +3857,7 @@ def __iter__(self) -> Iterator[Any]: offsets.append(field.offset) # itemsize must match point_step to handle potential padding bytes - dtype = np.dtype( + dtype = np.dtype( # type: ignore[assignment] { "names": names, "formats": formats, @@ -4074,7 +4075,7 @@ def _update_cloud(pcd, first=False): shown_first = True if animate: - state = { + state: dict[str, Any] = { "fps": max(1.0, float(fps)), "paused": False, "quit": False, diff --git a/src/machinevisiontoolbox/VisualServo.py b/src/machinevisiontoolbox/VisualServo.py index 8754f11b..750cac54 100644 --- a/src/machinevisiontoolbox/VisualServo.py +++ b/src/machinevisiontoolbox/VisualServo.py @@ -11,14 +11,20 @@ import numpy as np import spatialmath.base as smbase from spatialmath import SE3 +from spatialmath.base import Animate from machinevisiontoolbox import CentralCamera -from machinevisiontoolbox.base import mkgrid +from machinevisiontoolbox.base.shapes import mkgrid class VisualServo(ABC): class _history: - pass + # a bag of per-step quantities, which differ between the subclasses + def __getattr__(self, name: str) -> Any: + raise AttributeError(name) + + def __setattr__(self, name: str, value: Any) -> None: + object.__setattr__(self, name, value) def __init__( self, @@ -77,7 +83,7 @@ def __init__( """ self.camera = camera - self.history = [] + self.history: list[Any] = [] self.niter = niter @@ -89,12 +95,12 @@ def __init__( self.pose_0 = pose_0 - self.P = P + self.P: Any = P if P is not None: self.npoints = P.shape[1] if p_d is None: - p_d = camera.project_point(P, pose=pose_d) - self.p_star = p_d + p_d = camera.project_point(P, pose=pose_d) # type: ignore[arg-type] + self.p_star: Any = p_d # self.ax = smbase.plotvol3(plotvol) self.movie = movie @@ -126,7 +132,12 @@ def __init__( self.ax_3dview.set_facecolor("#f0f0f0") if title is not None: - self.fig.canvas.manager.set_window_title(title) + if self.fig.canvas.manager is not None: + self.fig.canvas.manager.set_window_title(title) + + def step(self, t: float) -> int: + """Compute one timestep of the simulation, implemented by subclasses.""" + raise NotImplementedError def init(self) -> None: """ @@ -264,7 +275,7 @@ def plot_p(self) -> None: ) if isinstance(self, PBVS): - p = self.camera.project_point(self.P, pose=self.pose_d.inv()) + p = self.camera.project_point(self.P, pose=self.pose_d.inv()) # type: ignore[union-attr] smbase.plot_polygon( p, "k*:", @@ -399,7 +410,7 @@ def plot_error(self) -> np.ndarray | None: """ if len(self.history) == 0: - return + return None e = np.array([h.e for h in self.history]) if self.type == "point": @@ -407,9 +418,9 @@ def plot_error(self) -> np.ndarray | None: plt.plot(e[:, 1::2], "b") plt.ylabel("Feature error (pixel)") - plt.legend("u", "v") + plt.legend(["u", "v"]) else: - plot(e) + plt.plot(e) plt.ylabel("Feature error") plt.grid(True) @@ -435,13 +446,13 @@ def plot_all(self) -> None: self.plot_vel() plt.figure() - self.plotpose() + self.plot_pose() plt.figure() self.plot_error() # optional plots depending on what history was recorded - if hasattr(history[0], "Z_est"): + if hasattr(self.history[0], "Z_est"): plt.figure() self.plot_z() @@ -472,11 +483,12 @@ def plot_point(self, *args: Any, **kwargs: Any) -> Any: return self.camera.plot_point(*args, **kwargs) def plot(self, *args: Any, **kwargs: Any) -> Any: - return self.camera.plot(*args, ax=self.ax_3dview, **kwargs) + kwargs.setdefault("ax", self.ax_3dview) + return self.camera.plot(*args, **kwargs) def clear_3dview(self) -> None: for child in self.ax_3dview.get_children(): # ax.lines: - if __class__.__name__ == "Line3DCollection": + if child.__class__.__name__ == "Line3DCollection": child.remove() @@ -529,7 +541,7 @@ def __init__( super().__init__(camera, type="point", title="PBVS simulation", **kwargs) self.eterm = eterm - self.lmbda = lmbda + self.lmbda: Any = lmbda if self.pose_d is None: self.pose_d = SE3(0, 0, 1) @@ -562,7 +574,7 @@ def step(self, t: float) -> int: Te_C_G = self.camera.estpose(self.P, uv, frame="camera") # estimate motion to desired relative pose - T_delta = Te_C_G * self.pose_d.inv() + T_delta = Te_C_G * self.pose_d.inv() # type: ignore[union-attr] # update the camera pose Td = T_delta.interp1(self.lmbda) @@ -682,8 +694,8 @@ def init(self) -> None: # initialize the vservo variables super().init() - self.vel_prev = None - self.uv_prev = None + self.vel_prev: Any = None + self.uv_prev: Any = None self.e0 = None def step(self, t: float) -> int: @@ -976,7 +988,6 @@ def step(self, t: float) -> int: :seealso: :meth:`run` :meth:`VisualServo.run` """ status = 0 - Z_est = [] # compute the lines f = self.getlines(self.camera.pose) @@ -1071,6 +1082,8 @@ def plot_line_tr(camera: CentralCamera, lines: np.ndarray, **kwargs: Any) -> Non # % See also Hough. ax = camera._ax + if ax is None: + raise RuntimeError("image plane has not been initialised") x = np.r_[ax.get_xlim()] y = np.r_[ax.get_ylim()] @@ -1230,7 +1243,6 @@ def step(self, t: float) -> int: """ status = 0 - Z_est = [] # compute feature vector f = np.r_[ @@ -1254,7 +1266,7 @@ def step(self, t: float) -> int: if self.verbose: # print(f"{cond=}, {v=}") - self.pose.printline() + self.camera.pose.printline() # update the history variables hist = self._history() @@ -1382,7 +1394,6 @@ def step(self, t: float) -> int: :seealso: :meth:`run` :meth:`VisualServo.run` """ status = 0 - Z_est = [] # compute image plane error as a column p = self.camera.project_point(self.P) # (phi, theta) @@ -1400,7 +1411,7 @@ def step(self, t: float) -> int: P_C = self.camera.pose.inv() * self.P J = self.camera.visjac_p(p, P_C[2, :]) else: - J = self.camera.visjac_p(pt, self.depth) + J = self.camera.visjac_p(p, self.depth) # compute the velocity of camera in camera frame try: @@ -1581,14 +1592,14 @@ def __init__( # monkey patch the plot setup for the CentralCamera object import types - camera._init_imageplane = types.MethodType(self._init_imageplane, camera) - camera._project_point = camera.project_point - camera.project_point = types.MethodType(self._project_polar, camera) + camera._init_imageplane = types.MethodType(self._init_imageplane, camera) # type: ignore[method-assign] + camera._project_point = camera.project_point # type: ignore[attr-defined] + camera.project_point = types.MethodType(self._project_polar, camera) # type: ignore[method-assign] # invoke superclass constructor super().__init__(camera, type="point", **kwargs) - self.lmbda = lmbda + self.lmbda: Any = lmbda self.eterm = eterm self.depth = depth @@ -1647,7 +1658,6 @@ def init(self) -> None: def step(self, t: float) -> int: status = 0 - Zest = [] hist = self._history() diff --git a/src/machinevisiontoolbox/__init__.py b/src/machinevisiontoolbox/__init__.py index 21f6c37c..2aa2ebcb 100644 --- a/src/machinevisiontoolbox/__init__.py +++ b/src/machinevisiontoolbox/__init__.py @@ -62,6 +62,12 @@ IBVS_polar, ) +from machinevisiontoolbox.base.findpeaks import ( # noqa: F401 + findpeaks, + findpeaks2d, + findpeaks3d, +) +from machinevisiontoolbox.base.meshgrid import meshgrid, spherical_rotate # noqa: F401 from machinevisiontoolbox.base import ( blackbody, loadspectrum, @@ -102,11 +108,6 @@ mksphere, mkcylinder, mkgrid, - meshgrid, - spherical_rotate, - findpeaks, - findpeaks2d, - findpeaks3d, mvtb_version, mpl_styling, ) diff --git a/src/machinevisiontoolbox/_image_typing.py b/src/machinevisiontoolbox/_image_typing.py index f6754aac..cab8202b 100644 --- a/src/machinevisiontoolbox/_image_typing.py +++ b/src/machinevisiontoolbox/_image_typing.py @@ -14,7 +14,7 @@ from __future__ import annotations import sys -from typing import Any, Protocol +from typing import TYPE_CHECKING, Any, Protocol if sys.version_info >= (3, 11): from typing import Self @@ -40,6 +40,10 @@ def __getitem__(self, key: Any) -> Any: ... # ---- pixel data -------------------------------------------------- _A: np.ndarray + _stats: dict[str, Any] | None + + @staticmethod + def _plane_stats(plane: np.ndarray) -> dict[str, float | int]: ... @property def A(self) -> np.ndarray: ... @@ -205,3 +209,12 @@ def __ior__(self, other: Any) -> Self: ... def __ixor__(self, other: Any) -> Self: ... def __ilshift__(self, other: Any) -> Self: ... def __irshift__(self, other: Any) -> Self: ... + + +# Base class for the Image mixins: the Protocol for type checkers, plain +# ``object`` at run time. mypy cannot evaluate a conditional expression in a +# base-class list, so the choice is made once here. +if TYPE_CHECKING: + ImageMixinBase = _ImageBase +else: + ImageMixinBase = object diff --git a/src/machinevisiontoolbox/base/color.py b/src/machinevisiontoolbox/base/color.py index 479a1779..21bca286 100755 --- a/src/machinevisiontoolbox/base/color.py +++ b/src/machinevisiontoolbox/base/color.py @@ -84,7 +84,7 @@ def _loaddata(filename: str, verbose: bool = False, **kwargs: Any) -> np.ndarray return data -_spectra = {} +_spectra: dict[Any, Any] = {} def loadspectrum( @@ -288,7 +288,7 @@ def lambda2rg( rgb = cmfrgb(λ, **kwargs) else: e = smb.getvector(e) - if len(e) != len(λ): + if len(e) != len(λ): # type: ignore[arg-type] raise ValueError("number of wavelengths and intensities must match") rgb = cmfrgb(λ, e, **kwargs) @@ -352,7 +352,7 @@ def cmfrgb(λ: ArrayLike, e: ArrayLike | None = None, **kwargs: Any) -> np.ndarr # assume steps are equal sized if e is not None: e = smb.getvector(e, out="row") # e is a vector Nx1 - dλ = λ[1] - λ[0] + dλ = λ[1] - λ[0] # type: ignore[index] ret = (e @ cmf) / cmf.shape[0] * dλ else: ret = cmf @@ -537,7 +537,7 @@ def cmfxyz( if e is not None: # approximate rectangular integration - dλ = λ[1] - λ[0] + dλ = λ[1] - λ[0] # type: ignore[index] XYZ = smb.getvector(e) @ cmfxyz * dλ return XYZ else: @@ -676,7 +676,7 @@ def ccxyz(λ: np.ndarray | float, e: np.ndarray | float | None = None) -> np.nda else: e = smb.getvector(e) xyz = xyz / (e * np.ones((1, 3))) - xyz = np.sum(xyz) + xyz = np.sum(xyz) # type: ignore[assignment] cc = xyz / (np.sum(xyz) * np.ones((1, 3))) return cc @@ -713,7 +713,7 @@ def _loadrgbdict(fname: str) -> dict[str, np.ndarray]: # result is an ndarray of tuples # convert data to a dictionary - rgbdict = {} + rgbdict: dict[str, Any] = {} for *rgb, name in data: rgbdict[str(name).lower()] = [x / 255.0 for x in rgb] @@ -909,7 +909,7 @@ def colorname( stacklevel=2, ) if isinstance(arg, str): - return name2color(arg, colorspace) + return name2color(arg, colorspace) # type: ignore[return-value] else: return color2name(arg, colorspace) @@ -1231,7 +1231,7 @@ def plot_spectral_locus( # well-spaced points around the locus if lambda_ticks is None: - λ = np.arange(460, 550, 10) + λ = np.arange(460, 550, 10) # type: ignore[assignment] λ = np.hstack((λ, np.arange(560, 620, 20))) else: λ = np.asarray(lambda_ticks) @@ -1337,7 +1337,7 @@ def colorspace_convert(image: ArrayLike, src: str, dst: str) -> np.ndarray: else: # not an image, see if it's Nx3 image = smb.getmatrix(image, (None, 3), dtype=np.float32) - image = image.reshape((-1, 1, 3)) + image = image.reshape((-1, 1, 3)) # type: ignore[union-attr] converted = cv2.cvtColor(image, code=operation) if converted.shape[0] == 1: return converted.flatten().astype(np.float64) diff --git a/src/machinevisiontoolbox/base/data.py b/src/machinevisiontoolbox/base/data.py index 1dfe5abf..4dd92f16 100644 --- a/src/machinevisiontoolbox/base/data.py +++ b/src/machinevisiontoolbox/base/data.py @@ -46,7 +46,7 @@ def mvtb_load_matfile(filename: str) -> dict[str, Any]: for key, value in data.items(): if isinstance(value, mat_struct): # scipy private API # type: ignore[arg-type] # print("fixing") - nt = namedtuple("matstruct", value._fieldnames) + nt = namedtuple("matstruct", value._fieldnames) # type: ignore[misc] # field names are only known at run time data[key] = nt(*[getattr(value, n) for n in value._fieldnames]) return data diff --git a/src/machinevisiontoolbox/base/graphics.py b/src/machinevisiontoolbox/base/graphics.py index 9e08bf53..ef17c201 100644 --- a/src/machinevisiontoolbox/base/graphics.py +++ b/src/machinevisiontoolbox/base/graphics.py @@ -60,6 +60,7 @@ def _roundvec(x: np.ndarray | list[Any] | tuple[Any, ...]) -> tuple[int, ...]: :return: rounded vector :rtype: tuple """ + y: Any if isinstance(x, np.ndarray): y = x.round(0).flatten().astype(int) else: @@ -643,7 +644,7 @@ def draw_text( else: lt = cv2.LINE_8 cv2.putText( - image, text, _roundvec(pos), _fontdict[font], fontsize, color, fontthickness, lt + image, text or "", _roundvec(pos), _fontdict[font], fontsize, color, fontthickness, lt ) return image @@ -766,12 +767,14 @@ def draw_point( _fontdict[font], fontheight, fontthickness ) + x: Any + y: Any if isinstance(pos, np.ndarray) and pos.shape[0] == 2: x = pos[0, :] y = pos[1, :] elif isinstance(pos, (tuple, list)): if smb.islistof(pos, (tuple, list)): - pairs = [tuple(z) for z in pos] + pairs = [tuple(z) for z in pos] # type: ignore[arg-type] x = [pair[0] for pair in pairs] y = [pair[1] for pair in pairs] else: @@ -976,7 +979,7 @@ def draw_circle( def mpl_styling( - fmt: str | tuple[str] | None = None, defaults: dict = None, kwargs: dict = None + fmt: str | tuple[str] | None = None, defaults: dict | None = None, kwargs: dict | None = None ): """Merge Matplotlib style options @@ -1041,7 +1044,7 @@ def parse_matplotlib_fmt(fmt: str): # Markers: Standard Matplotlib marker symbols marker_pattern = r"[.,ov^<>12348spP*hH+xXDd|_]" - res = {"color": None, "marker": None, "linestyle": None} + res: dict[str, Any] = {"color": None, "marker": None, "linestyle": None} working_fmt = fmt # 2. Extract Linestyle first (prevents '.' in '-.' being seen as a marker) @@ -1084,15 +1087,13 @@ def parse_matplotlib_fmt(fmt: str): res = parse_matplotlib_fmt(fmt) # Override with defaults if not specified in fmt - options = defaults | res | kwargs + options = (defaults or {}) | res | (kwargs or {}) return {k: v for k, v in options.items() if v is not None} if __name__ == "__main__": import numpy as np - from machinevisiontoolbox.base import draw_box, idisp - # import numpy as np # from machinevisiontoolbox import idisp, iread, Image diff --git a/src/machinevisiontoolbox/base/imageio.py b/src/machinevisiontoolbox/base/imageio.py index 4e45e154..c323819d 100644 --- a/src/machinevisiontoolbox/base/imageio.py +++ b/src/machinevisiontoolbox/base/imageio.py @@ -146,7 +146,7 @@ def _plt(): return plt -def safe_plt_show(block: bool | None = False) -> None: +def safe_plt_show(block: bool | float | None = False) -> None: """Call :func:`matplotlib.pyplot.show` while ignoring benign headless warnings. In documentation builds and other headless contexts Matplotlib can use a @@ -170,12 +170,12 @@ def safe_plt_show(block: bool | None = False) -> None: message=r"FigureCanvas.* is non-interactive, and thus cannot be shown", category=UserWarning, ) - plt.show(block=block) + plt.show(block=bool(block)) def idisp( im: np.ndarray, - colororder: str = "RGB", + colororder: str | None = "RGB", matplotlib: bool = True, block: bool | float | None = None, fps: float | None = None, @@ -499,7 +499,7 @@ def idisp( def _idisp_matplotlib( im: np.ndarray, - colororder: str = "RGB", + colororder: str | None = "RGB", block: bool | float | None = None, fps: float | None = None, fig: Any = None, @@ -660,8 +660,8 @@ def _idisp_matplotlib( fig.set_figheight(height / 25.4) # inches ## Create the colormap and normalizer - norm = None - cmap = None + norm: Any = None + cmap: Any = None if colormap == "invert": cmap = "Greys" elif colormap == "signed": @@ -686,7 +686,7 @@ def _idisp_matplotlib( norm = mpl.colors.CenteredNorm() elif colormap == "invsigned": # inverse signed color map, red is negative, blue is positive, zero is black - cdict = { + cdict: Any = { "red": [(0, 1, 1), (0.5, 0, 0), (1, 0, 0)], "green": [(0, 0, 0), (1, 0, 0)], "blue": [(0, 0, 0), (0.5, 0, 0), (1, 1, 1)], @@ -793,6 +793,7 @@ def _idisp_matplotlib( if len(im.shape) == 3: # color image, display in RGB or RGBA order; imshow handles alpha # natively for both uint8 (0-255) and float (0.0-1.0) arrays + colororder = colororder or "RGB" target = "RGBA" if "A" in colororder else "RGB" idx = [colororder.find(c) for c in target if colororder.find(c) >= 0] im_rgb = im[:, :, idx] # reorder planes → RGB or RGBA @@ -900,12 +901,12 @@ def format_coord(u: float, v: float) -> str: if colororder == "BGR": x = x[::-1] if np.issubdtype(x.dtype, np.integer): - val = [f"{_:d}" for _ in x] + vals = [f"{_:d}" for _ in x] elif np.issubdtype(x.dtype, np.floating): - val = [f"{_:.3f}" for _ in x] + vals = [f"{_:.3f}" for _ in x] else: - val = [str(_) for _ in x] - val = "[" + ", ".join(val) + "]" + vals = [str(_) for _ in x] + val = "[" + ", ".join(vals) + "]" return f"({u}, {v}): {val} {colororder}, {x.dtype}" @@ -966,7 +967,7 @@ def key_press(event: Any) -> None: def _idisp_opencv( im: np.ndarray, - colororder: str = "RGB", + colororder: str | None = "RGB", title: str | None = None, block: bool | float | None = None, fps: float | None = None, @@ -998,6 +999,7 @@ def _idisp_opencv( if im.ndim == 3: # color image, display in BGR order, but allow user to specify color order of input image + colororder = colororder or "RGB" idx = [] for c in "BGR": j = colororder.find(c) @@ -1503,6 +1505,7 @@ def click_event(event: Any, x: int, y: int, flags: Any, params: Any) -> None: # wait for a key to be pressed to exit cv2.waitKey(0) + return None if __name__ == "__main__": diff --git a/src/machinevisiontoolbox/base/shapes.py b/src/machinevisiontoolbox/base/shapes.py index d02b038d..cc121238 100644 --- a/src/machinevisiontoolbox/base/shapes.py +++ b/src/machinevisiontoolbox/base/shapes.py @@ -55,27 +55,27 @@ def mkgrid( ``pose`` argument can be used to transform all the points. """ - side = smb.getvector(side) - if len(side) == 1: - sx = side[0] - sy = side[0] - elif len(side) == 2: - sx = side[0] - sy = side[1] + sidev = smb.getvector(side) + if len(sidev) == 1: + sx = sidev[0] + sy = sidev[0] + elif len(sidev) == 2: + sx = sidev[0] + sy = sidev[1] else: raise ValueError("bad s") - n = smb.getvector(n) - if len(n) == 1: - nx = n[0] - ny = n[0] - elif len(n) == 2: - nx = n[0] - ny = n[1] + nv = smb.getvector(n) + if len(nv) == 1: + nx = nv[0] + ny = nv[0] + elif len(nv) == 2: + nx = nv[0] + ny = nv[1] else: raise ValueError("bad number of points") - if n[0] == 2: + if nv[0] == 2: # special case, we want the points in specific order P = ( np.array( diff --git a/src/machinevisiontoolbox/bin/imtool.py b/src/machinevisiontoolbox/bin/imtool.py index 0ded375d..34cb5617 100755 --- a/src/machinevisiontoolbox/bin/imtool.py +++ b/src/machinevisiontoolbox/bin/imtool.py @@ -19,6 +19,8 @@ # Set by figure keypress callback when the user requests immediate exit. exit_requested = False +cs_image: np.ndarray # image in the display colorspace, set by main() +colorspace: str # name of that colorspace, set by main() def getargs(): @@ -213,12 +215,12 @@ def format_coord(u: float, v: float) -> str: # color image x = cs_image[v, u, :] if np.issubdtype(x.dtype, np.integer): - val = [f"{_:d}" for _ in x] + parts = [f"{_:d}" for _ in x] elif np.issubdtype(x.dtype, np.floating): - val = [f"{_:.3f}" for _ in x] + parts = [f"{_:.3f}" for _ in x] else: - val = [str(_) for _ in x] - val = "[" + ", ".join(val) + "]" + parts = [str(_) for _ in x] + val = "[" + ", ".join(parts) + "]" return f"({u}, {v}): {val} {colorspace}, {x.dtype}" except Exception as e: diff --git a/src/machinevisiontoolbox/bin/mvtbtool.py b/src/machinevisiontoolbox/bin/mvtbtool.py index fa20f1ba..9627013f 100755 --- a/src/machinevisiontoolbox/bin/mvtbtool.py +++ b/src/machinevisiontoolbox/bin/mvtbtool.py @@ -22,6 +22,7 @@ from matplotlib import image import cv2 from spatialmath import * # lgtm [py/polluting-import] +from spatialmath import SE3 from spatialmath.base import * # lgtm [py/polluting-import] from machinevisiontoolbox import * # lgtm [py/unused-import] diff --git a/src/machinevisiontoolbox/blocks/Icons/make_icons.py b/src/machinevisiontoolbox/blocks/Icons/make_icons.py index 3f138c4b..73547493 100644 --- a/src/machinevisiontoolbox/blocks/Icons/make_icons.py +++ b/src/machinevisiontoolbox/blocks/Icons/make_icons.py @@ -69,7 +69,7 @@ def tex_icon( scale = size * (1 - 2 * margin) / max(ink.size) ink = ink.resize( (max(1, round(ink.width * scale)), max(1, round(ink.height * scale))), - Image.LANCZOS, + Image.Resampling.LANCZOS, ) canvas = Image.new("L", (size, size), 0) diff --git a/src/machinevisiontoolbox/py.typed b/src/machinevisiontoolbox/py.typed new file mode 100644 index 00000000..e69de29b