Skip to content

metrics

greyscale(master_chunk, tile_arrays, **kwargs)

Compute the greyscale distance.

This metric ignores colours and compares greyscale values. Should provide better photomosaics when using few tiles images.

Parameters:

Name Type Description Default
master_chunk ndarray

array containing the RGB pixels with values between 0 and 255.

required
tile_arrays ndarray

array tile pixel arrays, values between 0 and 255.

required
**kwargs

passed to np.linalg.norm.

{}

Returns:

Type Description
ndarray

Colour distance approximation between the master chunk and all the tiles arrays.

Source code in phomo/metrics.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@register_metric
def greyscale(
    master_chunk: np.ndarray, tile_arrays: np.ndarray, **kwargs
) -> np.ndarray:
    """Compute the greyscale distance.

    This metric ignores colours and compares greyscale values. Should provide better
    photomosaics when using few tiles images.


    Args:
        master_chunk: array containing the RGB pixels with values between 0 and 255.
        tile_arrays: array tile pixel arrays, values between 0 and 255.
        **kwargs: passed to ``np.linalg.norm``.

    Returns:
        Colour distance approximation between the master chunk and all the tiles
            arrays.
    """
    delta = np.subtract(
        master_chunk.sum(axis=-1), tile_arrays.sum(axis=-1), dtype=float
    )
    return np.linalg.norm(delta.reshape(delta.shape[0], -1), axis=-1, **kwargs)

luv_approx(master_chunk, tile_arrays, **kwargs)

Distance metric using a LUV space approximation.

This metric should provide more accurate colour matching.

Reference

https://www.compuphase.com/cmetric.htm

Parameters:

Name Type Description Default
master_chunk ndarray

array containing the RGB pixels with values between and 255.

required
tile_arrays ndarray

array containing the RGB pixels with values between 0 and 255.

required
**kwargs

passed to np.linalg.norm.

{}

Returns:

Type Description
ndarray

Colour distance approximation between the master chunk and all the tiles arrays.

Source code in phomo/metrics.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
@register_metric
def luv_approx(
    master_chunk: np.ndarray, tile_arrays: np.ndarray, **kwargs
) -> np.ndarray:
    """Distance metric using a L*U*V space approximation.

    This metric should provide more accurate colour matching.

    Reference:
        https://www.compuphase.com/cmetric.htm

    Args:
        master_chunk: array containing the RGB pixels with values between and 255.
        tile_arrays: array containing the RGB pixels with values between 0 and 255.
        **kwargs: passed to ``np.linalg.norm``.

    Returns:
        Colour distance approximation between the master chunk and all the tiles
            arrays.
    """
    r = (master_chunk[:, :, 0] + tile_arrays[:, :, :, 0]) // 2
    r = r.astype(float)
    d = np.subtract(master_chunk, tile_arrays, dtype=float)
    return np.linalg.norm(
        (
            ((512 + r) * d[:, :, :, 0] ** 2)
            + 1024 * d[:, :, :, 1] ** 2
            + ((767 - r) * d[:, :, :, 2] ** 2)
        ).reshape(d.shape[0], -1),
        axis=-1,
        **kwargs,
    )

norm(master_chunk, tile_arrays, **kwargs)

Distance metric using np.linalg.norm.

Quick distance metric in RGB space.

Parameters:

Name Type Description Default
master_chunk ndarray

array containing the RGB pixels with values between 0 and 255.

required
tile_arrays ndarray

list of tile pixel arrays, values between 0 and 255.

required
**kwargs

passed to np.linalg.norm.

{}

Returns:

Type Description
ndarray

Colour distance approximation between the master chunk and all the tiles arrays.

Source code in phomo/metrics.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
@register_metric
def norm(master_chunk: np.ndarray, tile_arrays: np.ndarray, **kwargs) -> np.ndarray:
    """Distance metric using ``np.linalg.norm``.

    Quick distance metric in RGB space.

    Args:
        master_chunk: array containing the RGB pixels with values between 0 and 255.
        tile_arrays: list of tile pixel arrays, values between 0 and 255.
        **kwargs: passed to ``np.linalg.norm``.

    Returns:
        Colour distance approximation between the master chunk and all the tiles
            arrays.
    """
    return np.linalg.norm(
        np.subtract(master_chunk, tile_arrays, dtype=float).reshape(
            tile_arrays.shape[0], -1, tile_arrays.shape[-1]
        ),
        axis=(1, 2),
        **kwargs,
    )