Skip to content

modosaic.services.edge

modosaic.services.edge

EdgeService

Edge-map utilities used by cross-modality validators.

rgb_edge_map staticmethod

rgb_edge_map(rgb_uint8, q=0.9)

Compute a boolean edge map from an RGB uint8 image.

Parameters:

Name Type Description Default
rgb_uint8 ndarray

RGB image with shape HxWx3.

required
q float

Quantile threshold applied to gradient magnitudes.

0.9

Returns:

Type Description
ndarray

Boolean edge map.

Source code in modosaic/services/edge.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
@staticmethod
def rgb_edge_map(
        rgb_uint8: np.ndarray,
        q: float = 0.90,
) -> np.ndarray:
    """Compute a boolean edge map from an RGB uint8 image.

    Args:
        rgb_uint8: RGB image with shape `HxWx3`.
        q: Quantile threshold applied to gradient magnitudes.

    Returns:
        Boolean edge map.
    """
    gray = cv2.cvtColor(rgb_uint8, cv2.COLOR_RGB2GRAY).astype(np.float32)
    return EdgeService.sobel_edge_map_float(gray, blur_ksize=3, q=q)

sobel_edge_map_float staticmethod

sobel_edge_map_float(img, blur_ksize=3, q=0.9, valid_mask=None)

Compute a boolean Sobel edge map from a scalar image.

Parameters:

Name Type Description Default
img ndarray

Scalar image or dense map.

required
blur_ksize int

Gaussian blur kernel size before Sobel filtering.

3
q float

Quantile threshold applied to gradient magnitudes.

0.9
valid_mask ndarray | None

Optional mask limiting where edges can be detected.

None

Returns:

Type Description
ndarray

Boolean edge map.

Source code in modosaic/services/edge.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
@staticmethod
def sobel_edge_map_float(
        img: np.ndarray,
        blur_ksize: int = 3,
        q: float = 0.90,
        valid_mask: np.ndarray | None = None,
) -> np.ndarray:
    """Compute a boolean Sobel edge map from a scalar image.

    Args:
        img: Scalar image or dense map.
        blur_ksize: Gaussian blur kernel size before Sobel filtering.
        q: Quantile threshold applied to gradient magnitudes.
        valid_mask: Optional mask limiting where edges can be detected.

    Returns:
        Boolean edge map.
    """
    x = np.asarray(img).astype(np.float32, copy=False)
    finite = np.isfinite(x)
    if valid_mask is not None:
        finite &= valid_mask.astype(bool, copy=False)
    x = np.where(finite, x, 0.0)

    if blur_ksize and blur_ksize >= 3:
        x = cv2.GaussianBlur(x, (blur_ksize, blur_ksize), 0)

    gx = cv2.Sobel(x, cv2.CV_32F, 1, 0, ksize=3)
    gy = cv2.Sobel(x, cv2.CV_32F, 0, 1, ksize=3)
    mag = np.sqrt(gx * gx + gy * gy)

    grad_valid = finite
    if np.any(finite):
        margin = 1 + max(0, blur_ksize // 2)
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2 * margin + 1, 2 * margin + 1))
        grad_valid = cv2.erode(finite.astype(np.uint8), kernel, iterations=1).astype(bool)

    thr = EdgeService._adaptive_threshold(mag, q, grad_valid)
    return ((mag >= thr) & grad_valid).astype(bool)