Distance Matrix Metrics¶
There ara a few different distance metrics which can generate slightly different mosaics.
In [1]:
Copied!
from phomo import METRICS
for metric, func in METRICS.items():
print(f"{metric}: {func.__doc__}\n")
from phomo import METRICS
for metric, func in METRICS.items():
print(f"{metric}: {func.__doc__}\n")
greyscale: 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. norm: 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. luv_approx: 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.
/home/lcoyle/.cache/pypoetry/virtualenvs/phomo-pX3Qwu7w-py3.12/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html from .autonotebook import tqdm as notebook_tqdm
In [2]:
Copied!
from phomo import Mosaic, Master, Pool
from phomo import Mosaic, Master, Pool
In [3]:
Copied!
pool = Pool.from_dir("faces/", crop_ratio=1, tile_size=(20, 20))
pool = Pool.from_dir("faces/", crop_ratio=1, tile_size=(20, 20))
Loading tiles: 100%|█████████████████████████████████████████████████████████████████| 9780/9780 [00:04<00:00, 2090.45it/s]
In [4]:
Copied!
from pathlib import Path
master_file = list(Path("faces").glob('*'))[3732]
from pathlib import Path
master_file = list(Path("faces").glob('*'))[3732]
In [5]:
Copied!
master = Master.from_file(master_file, img_size=(400, 400))
master.img
master = Master.from_file(master_file, img_size=(400, 400))
master.img
Out[5]:
In [6]:
Copied!
mosaic = Mosaic(master, pool)
mosaic = Mosaic(master, pool)
All the different metrics¶
Let's go through the different distance metrics.
In [7]:
Copied!
mosaic.build(mosaic.d_matrix(metric="norm"))
mosaic.build(mosaic.d_matrix(metric="norm"))
Building distance matrix: 100%|██████████████████████████████████████████████████████████| 400/400 [00:21<00:00, 18.44it/s] Building mosaic: 100%|███████████████████████████████████████████████████████████████| 400/400 [00:00<00:00, 101250.55it/s]
Out[7]:
In [8]:
Copied!
mosaic.build(mosaic.d_matrix(metric="greyscale"))
mosaic.build(mosaic.d_matrix(metric="greyscale"))
Building distance matrix: 100%|██████████████████████████████████████████████████████████| 400/400 [00:30<00:00, 13.13it/s] Building mosaic: 100%|███████████████████████████████████████████████████████████████| 400/400 [00:00<00:00, 249252.95it/s]
Out[8]:
In [9]:
Copied!
mosaic.build(mosaic.d_matrix(metric="luv_approx"))
mosaic.build(mosaic.d_matrix(metric="luv_approx"))
Building distance matrix: 100%|██████████████████████████████████████████████████████████| 400/400 [00:35<00:00, 11.42it/s] Building mosaic: 100%|███████████████████████████████████████████████████████████████| 400/400 [00:00<00:00, 164692.41it/s]
Out[9]:
Changing the parameters of np.linalg.norm
¶
When using the "norm"
we can also control the parameters of the np.linalg.norm
function.
This results in slightly different mosaics.
See the numpy docs: https://numpy.org/doc/stable/reference/generated/numpy.linalg.norm.html
In [10]:
Copied!
mosaic.build(mosaic.d_matrix(metric="norm", ord=1))
mosaic.build(mosaic.d_matrix(metric="norm", ord=1))
Building distance matrix: 100%|██████████████████████████████████████████████████████████| 400/400 [00:34<00:00, 11.55it/s] Building mosaic: 100%|███████████████████████████████████████████████████████████████| 400/400 [00:00<00:00, 240430.15it/s]
Out[10]:
In [11]:
Copied!
mosaic.build(mosaic.d_matrix(metric="norm", ord=2))
mosaic.build(mosaic.d_matrix(metric="norm", ord=2))
Building distance matrix: 100%|██████████████████████████████████████████████████████████| 400/400 [00:43<00:00, 9.21it/s] Building mosaic: 100%|███████████████████████████████████████████████████████████████| 400/400 [00:00<00:00, 254315.84it/s]
Out[11]:
In [12]:
Copied!
mosaic.build(mosaic.d_matrix(metric="norm", ord="fro"))
mosaic.build(mosaic.d_matrix(metric="norm", ord="fro"))
Building distance matrix: 100%|██████████████████████████████████████████████████████████| 400/400 [00:17<00:00, 23.18it/s] Building mosaic: 100%|███████████████████████████████████████████████████████████████| 400/400 [00:00<00:00, 203532.89it/s]
Out[12]:
In [13]:
Copied!
mosaic.build(mosaic.d_matrix(metric="norm", ord="nuc"))
mosaic.build(mosaic.d_matrix(metric="norm", ord="nuc"))
Building distance matrix: 100%|██████████████████████████████████████████████████████████| 400/400 [00:43<00:00, 9.23it/s] Building mosaic: 100%|███████████████████████████████████████████████████████████████| 400/400 [00:00<00:00, 236165.77it/s]
Out[13]:
GPU metrics¶
When using GPU acceleration through Mosaic.d_matrix_cuda
only "norm"
and "greyscale"
are supported.
In [14]:
Copied!
mosaic.build(mosaic.d_matrix_cuda(metric="norm"))
mosaic.build(mosaic.d_matrix_cuda(metric="norm"))
Building mosaic: 100%|███████████████████████████████████████████████████████████████| 400/400 [00:00<00:00, 180886.43it/s]
Out[14]:
In [15]:
Copied!
mosaic.build(mosaic.d_matrix_cuda(metric="greyscale"))
mosaic.build(mosaic.d_matrix_cuda(metric="greyscale"))
Building mosaic: 100%|███████████████████████████████████████████████████████████████| 400/400 [00:00<00:00, 264249.74it/s]
Out[15]: