compimg package

How to use

Here is the simple example of how one can compare one image to another.

>>> import numpy as np
>>> from compimg.similarity import MSE
>>> img = np.ones((20,20), dtype = np.uint8)
>>> reference = np.ones((20,20), dtype = np.uint8)
>>> MSE().compare(img, img)
0.0

All metrics implement single interface so it is easy to use multiple of them for example you could run:

>>> import numpy as np
>>> from compimg.similarity import MSE, PSNR, SSIM
>>> for metric in [MSE(), PSNR(), SSIM()]:
...     img = np.ones((20,20), dtype = np.uint8)
...     reference = np.zeros((20,20), dtype = np.uint8)
...     value = round(metric.compare(img, reference), 2)
...     print(f"{metric.__class__.__name__} = {value}")
MSE = 1.0
PSNR = 48.13
SSIM = 0.87

compimg implicitly converts image to intermediate type (float64) to avoid overflow/underflow when doing calculation. Its advised to leave this type as is, albeit it is possible to change it. For example you could sacrafice precision to improve processing speed by changing it to float32 or even float16.

>>> import numpy as np
>>> import compimg
>>> import compimg.similarity
>>> compimg.config.intermediate_type = np.dtype(np.float32)
>>> # code that uses similarity metrics

Submodules

compimg.exceptions module

compimg exceptions module

exception compimg.exceptions.DifferentDTypesError(dtype1: numpy.dtype, dtype2: numpy.dtype)[source]

Bases: Exception

exception compimg.exceptions.DifferentShapesError(shape1: Sequence[int], shape2: Sequence[int])[source]

Bases: Exception

exception compimg.exceptions.KernelBiggerThanImageError(kernel_shape: Sequence[int], image_shape: Sequence[int])[source]

Bases: Exception

exception compimg.exceptions.KernelNot2DArray(dims: int)[source]

Bases: Exception

exception compimg.exceptions.KernelShapeNotOddError(kernel_shape: Sequence[int])[source]

Bases: Exception

exception compimg.exceptions.NegativePadAmountError(amount)[source]

Bases: Exception

compimg.similarity module

compimg.windows module

Module with SlidingWindow interface and its implementations.

class compimg.windows.IdentitySlidingWindow(shape: Tuple[int, int], stride: Tuple[int, int])[source]

Bases: compimg.windows.SlidingWindow

Slides through the image without making any changes.

slide(image: numpy.ndarray) → Generator[numpy.ndarray, None, None][source]

Using some windows slides over image returning its changed/unchanged fragments.

Parameters:image – Image to slide over.
Returns:Generator that returns views returned by window.
class compimg.windows.SlidingWindow[source]

Bases: abc.ABC

slide(image: numpy.ndarray) → Generator[numpy.ndarray, None, None][source]

Using some windows slides over image returning its changed/unchanged fragments.

Parameters:image – Image to slide over.
Returns:Generator that returns views returned by window.

compimg.pads module

This module defines means to apply padding to images.

class compimg.pads.ConstantPad(value: numbers.Number, amount: int)[source]

Bases: compimg.pads.Pad

Adds rows/columns of chosen value at the edges of an image.

apply(image: numpy.ndarray) → numpy.ndarray[source]

Pads given image.

Parameters:image – Image to pad.
Returns:Padded image.
class compimg.pads.EdgePad(amount: int)[source]

Bases: compimg.pads.Pad

Replicates neighbouring pixels at edges.

apply(image: numpy.ndarray) → numpy.ndarray[source]

Pads given image.

Parameters:image – Image to pad.
Returns:Padded image.
class compimg.pads.FromFunctionPad(function: Callable[[numpy.ndarray], numpy.ndarray])[source]

Bases: compimg.pads.Pad

apply(image: numpy.ndarray) → numpy.ndarray[source]

Pads given image.

Parameters:image – Image to pad.
Returns:Padded image.
class compimg.pads.NoPad[source]

Bases: compimg.pads.Pad

Helper class when one has to pass Pad object but does not want apply any padding.

apply(image: numpy.ndarray) → numpy.ndarray[source]

Pads given image.

Parameters:image – Image to pad.
Returns:Padded image.
class compimg.pads.Pad[source]

Bases: abc.ABC

When performing convolution one needs to decide what to do filter is near border(s). Instances implementing this class address that problem.

apply(image: numpy.ndarray) → numpy.ndarray[source]

Pads given image.

Parameters:image – Image to pad.
Returns:Padded image.

compimg.kernels module