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
compimg.similarity module¶
Module with routines for computing similarity between images
-
class
compimg.similarity.
GSSIM
(k1: float = 0.01, k2: float = 0.03)[source]¶ Bases:
compimg.similarity.SimilarityMetric
Gradient-Based Structural similarity index according to the paper “GRADIENT-BASED STRUCTURAL SIMILARITY FOR IMAGE QUALITY ASSESSMENT” by Chen et al.
-
class
compimg.similarity.
MAE
[source]¶ Bases:
compimg.similarity.SimilarityMetric
Mean absolute error.
-
class
compimg.similarity.
MSE
[source]¶ Bases:
compimg.similarity.SimilarityMetric
Mean squared error.
-
class
compimg.similarity.
PSNR
[source]¶ Bases:
compimg.similarity.SimilarityMetric
Peak signal-to-noise ratio according to https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio.
-
class
compimg.similarity.
RMSE
[source]¶ Bases:
compimg.similarity.SimilarityMetric
Root mean squared error.
-
class
compimg.similarity.
SSIM
(k1: float = 0.01, k2: float = 0.03)[source]¶ Bases:
compimg.similarity.SimilarityMetric
Structural similarity index according to the paper from 2004 “Image Quality Assessment: From Error Visibility to Structural Similarity” by Wang et al.
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.
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.
-
class
compimg.pads.
EdgePad
(amount: int)[source]¶ Bases:
compimg.pads.Pad
Replicates neighbouring pixels at edges.
-
class
compimg.pads.
FromFunctionPad
(function: Callable[[numpy.ndarray], numpy.ndarray])[source]¶ Bases:
compimg.pads.Pad
-
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.
compimg.kernels module¶
Image processing using kernels.
-
compimg.kernels.
convolve
(image: numpy.ndarray, kernel: numpy.ndarray) → numpy.ndarray[source]¶ Performs the convolution using provided kernel.
Attention
In case when image has multiple channels and provided kernel has only one, the kernel values get replicated along every channel.
Parameters: - image – Image on which to perform a convolution.
- kernel – Kernel to be used.
Returns: Convolved image.
Raises: - KernelBiggerThanImageError – When kernel does not fit into image.
- KernelShapeNotOddError – When kernel does not is of even shape.
-
compimg.kernels.
make_guassian_kernel
(shape: Tuple[int, int], sigma: float)[source]¶ Produces two-dimensional Gaussian kernel according to https://en.wikipedia.org/wiki/Gaussian_function.
Parameters: - shape – Shape of the kernel.
- sigma – Sigma to use in the formula.
Returns: Gaussian kernel.