Skip to content

Commit f1455f7

Browse files
authored
demos: add new demo for ssim (#136)
1 parent 8e47565 commit f1455f7

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# ---
2+
# cover: assets/ssim.png
3+
# title: Structural Similarity Index
4+
# ---
5+
6+
# When comparing images, the **Mean Squared Error** (or MSE), though straightforward to calculate,
7+
# may not be a very good indicator of their *perceived* similarity.
8+
9+
# The **Structural Similarity Index** (or SSIM) aims to address this shortcoming by taking texture
10+
# into account, and assigning a higher score to images that may *appear* similar.
11+
12+
13+
using Images, TestImages
14+
using Random
15+
16+
img_orig = float64.(testimage("cameraman"))
17+
18+
# We use a grayscale image out of the `TestImages` package, which provides a
19+
# standard suite of test images. `float`/`float32`/`float64` preserve colorant
20+
# information: thus the image is now composed of pixels of type `Gray{Float64}`.
21+
22+
assess_ssim(img_orig, img_orig)
23+
24+
# The `assess_ssim` function, which takes two images as inputs and returns their
25+
# structural similarity index, is the simplest way to calculate the SSIM of two images.
26+
27+
# An SSIM score of `1.00` indicates perfect structural similarity, as is expected
28+
# out of identical images.
29+
30+
# Now, we create two variations of the original image: `image_const` on the left has the intensity of
31+
# all its pixels increased by `0.2` times the intensity range, while `image_noise` on the right has the
32+
# intensity of some of its pixels increased, and that of the others decreased by the same
33+
# amount. The two images look quite different visually.
34+
35+
noise = ones(size(img_orig)) .* 0.2 .* (maximum(img_orig) - minimum(img_orig))
36+
img_const = img_orig + noise
37+
38+
mask = rand(Float64, size(img_orig)) .< 0.5
39+
noise[mask] = noise[mask] .* -1
40+
img_noise = img_orig + noise
41+
42+
mosaicview(img_const, img_noise; nrow=1)
43+
save("assets/ssim.png", img_noise) #src
44+
45+
# We use the `mse` funtion defined in `ImageDistances` to calculate the mean squared
46+
# error between the original and the two modified images.
47+
48+
mse(img_orig, img_const), mse(img_orig, img_noise)
49+
50+
# Despite their visual differences, both the images have the exact same mean squared error
51+
# of `0.400`, when compared with the original. This demonstrates how in certain cases, MSE
52+
# can fail to capture the *perceived* similarity of images.
53+
54+
assess_ssim(img_orig, img_const), assess_ssim(img_orig, img_noise)
55+
56+
# Their SSIM scores vary significantly, with `image_const` being rated much closer
57+
# to the original image in terms of perceived similarity, which is in line with what
58+
# visually seems to be the case.
59+
60+
# ### Custom Parameters
61+
62+
# While `assess_ssim` is a convenient way to calculate the SSIM of two images, it
63+
# does not allow for custom parameters to be passed to the SSIM algorithm, for which
64+
# we have the following syntax.
65+
66+
iqi = SSIM(KernelFactors.gaussian(2.0, 11), (0.5, 0.5, 0.5))
67+
assess(iqi, img_orig, img_const)
68+
69+
# Here, the first parameter is the kernel used to weight the neighbourhood of each
70+
# pixel while calculating the SSIM locally, and defaults to `KernelFactors.gaussian(1.5, 11)`.
71+
# The second parameter is the set of weights (α, β, γ) given to the *lunimance* (L),
72+
# *contrast* (C) and *structure* (S) terms while calculating the SSIM,
73+
# and defaults to `(1.0, 1.0, 1.0)`.
74+
# Recall that SSIM is defined as Lᵅ × Cᵝ × Sᵞ.
75+
76+
# ### References
77+
# 1. Zhou Wang; Bovik, A.C.; ,”Mean squared error: Love it or leave it? A new look at Signal Fidelity Measures,” Signal Processing Magazine, IEEE, vol. 26, no. 1, pp. 98-117, Jan. 2009.
78+
# 2. Z. Wang, A. C. Bovik, H. R. Sheikh and E. P. Simoncelli, “Image quality assessment: From error visibility to structural similarity,” IEEE Transactions on Image Processing, vol. 13, no. 4, pp. 600-612, Apr. 2004.

0 commit comments

Comments
 (0)