StructuralSimilarity¶
- class mmeval.metrics.StructuralSimilarity(crop_border: int = 0, input_order: str = 'CHW', convert_to: Optional[str] = None, channel_order: str = 'rgb', **kwargs)[source]¶
Calculate StructuralSimilarity (structural similarity).
Ref: Image quality assessment: From error visibility to structural similarity
The results are the same as that of the official released MATLAB code in https://ece.uwaterloo.ca/~z70wang/research/ssim/.
For three-channel images, StructuralSimilarity is calculated for each channel and then averaged.
- Parameters
crop_border (int) – Cropped pixels in each edges of an image. These pixels are not involved in the PeakSignalNoiseRatio calculation. Defaults to 0.
input_order (str) – Whether the input order is ‘HWC’ or ‘CHW’. Defaults to ‘HWC’.
convert_to (str, optional) – Whether to convert the images to other color models. If None, the images are not altered. When computing for ‘Y’, the images are assumed to be in BGR order. Options are ‘Y’ and None. Defaults to None.
channel_order (str) – The channel order of image. Choices are ‘rgb’ and ‘bgr’. Defaults to ‘rgb’.
**kwargs – Keyword parameters passed to
BaseMetric
.
Examples
>>> from mmeval import StructuralSimilarity as SSIM >>> import numpy as np >>> >>> ssim = SSIM(input_order='CHW', convert_to='Y', channel_order='rgb') >>> gts = np.random.randint(0, 255, size=(3, 32, 32)) >>> preds = np.random.randint(0, 255, size=(3, 32, 32)) >>> ssim(preds, gts) {'ssim': ...}
Calculate StructuralSimilarity between 2 single channel images:
>>> img1 = np.ones((32, 32)) * 2 >>> img2 = np.ones((32, 32)) >>> SSIM.compute_ssim(img1, img2) 0.913062377743969
- add(predictions: Sequence[numpy.ndarray], groundtruths: Sequence[numpy.ndarray], channel_order: Optional[str] = None) → None[source]¶
Add the StructuralSimilarity score of the batch to
self._results
.For three-channel images, StructuralSimilarity is calculated for each channel and then averaged.
- Parameters
predictions (Sequence[np.ndarray]) – Predictions of the model.
groundtruths (Sequence[np.ndarray]) – The ground truth images.
channel_order (Optional[str]) – The channel order of the input samples. If not passed, will set as
self.channel_order
. Defaults to None.
- compute_metric(results: List[numpy.float64]) → Dict[str, float][source]¶
Compute the StructuralSimilarity metric.
This method would be invoked in
BaseMetric.compute
after distributed synchronization.- Parameters
results (List[np.float64]) – A list that consisting the StructuralSimilarity scores. This list has already been synced across all ranks.
- Returns
The computed StructuralSimilarity metric.
- Return type
Dict[str, float]
- static compute_ssim(img1: numpy.ndarray, img2: numpy.ndarray) → numpy.float64[source]¶
Calculate StructuralSimilarity (structural similarity) between two single channel image.
Ref: Image quality assessment: From error visibility to structural similarity
The results are the same as that of the official released MATLAB code in https://ece.uwaterloo.ca/~z70wang/research/ssim/.
- Parameters
img1 (np.ndarray) – Single channels Images with range [0, 255].
img2 (np.ndarray) – Single channels Images with range [0, 255].
- Returns
StructuralSimilarity result.
- Return type
np.float64