7

I want to compare the similarity of two images by looking at its structure and colors. I need a measure that takes both structural and color fidelity into account. When I checked the formulation of original paper, it is said only x and y mean and variance is measured. Does SSIM account for structure only or does it take color similarity into account too?

Royi
  • 19,608
  • 4
  • 197
  • 238
Lyrk
  • 194
  • 2
  • 9

3 Answers3

6

What usually can be done is calculate it per channel and have the average (Or weighted average).

So if you have $ {R}_{1}, {G}_{1}, {B}_{1} $ and $ {R}_{2}, {G}_{2}, {B}_{2} $ you would do:

$$ {SSIM}_{r} \left( {R}_{1}, {R}_{2} \right), {SSIM}_{g} \left( {G}_{1}, {G}_{2} \right), {SSIM}_{b} \left( {B}_{1}, {B}_{2} \right) $$

Then $ {SSIM}_{rgb} = {w}_{r} {SSIM}_{r} + {w}_{g} {SSIM}_{g} + {w}_{b} {SSIM}_{b} $.

For instance, MATLAB's ssim() does the above with $ {w}_{r} = {w}_{g} = {w}_{b} = \frac{1}{3} $.

SciKit Image's compare_ssim() also apply it channel wise using the multichannel flag:

enter image description here

Royi
  • 19,608
  • 4
  • 197
  • 238
  • I just made a 512*512 all orange jpeg and the same size yellow jpeg file. Then I measured the similarity by using SSIM and result is 0.60. Then I made a red one and the similarity between orange and red is 0.8. It compares colors too I suppose. At first when I checked the name, because there is "Structural", it only measured structure of the images but my little experiment shows that it measures colors too. Do you confirm this? – Lyrk May 16 '21 at 08:53
  • Do you really need to separate the image into its RGB components to get the exact similarity? Can't I use the original image and the image to be measured directly without separating into 3 components? – Lyrk May 16 '21 at 09:11
  • By design it only applied to a single channel image. What do you use to apply the comparison? If you use MATLAB, what it does is exactly as above with $ {w}{r} = {w}{g} = {w}_{b} = \frac{1}{3} $. – Royi May 16 '21 at 09:12
  • You don't need to separate as in 3 different arrays, just implement it in such way. – Royi May 16 '21 at 09:13
  • I use Scikit image in Python. https://github.com/scikit-image/scikit-image/blob/main/skimage/metrics/_structural_similarity.py There is a "multichannel" argument there. If I use it I hope I do not have to measure channels separately. – Lyrk May 16 '21 at 09:24
  • @Lyrk, You asked how to implement this. Both SciKit Image and MATLAB apply it as I wrote above with uniform weights. – Royi May 16 '21 at 09:36
1

I don't see a commonly used standard when implementing it; the original paper [1] and reference code [2] only provide the definition for grayscale images.

This means the SSIM result for color images may vary from implementation to implementation. The $\frac{1}{3}SSIM_R + \frac{1}{3}SSIM_G + \frac{1}{3} SSIM_B$ answer @Royi gives is how ImageQualityIndexes.jl (Julia version) is implemented.

As for MATLAB's built-in ssim() function, it treats the RGB image as 3D volumic data instead of three independent 2D slices. This is quite different because SSIM internally applies a few filters. If it is 3D volumic data, the filter kernel is 3D, and if it is 2D slices, then the filter kernel is still 2D.

edit ssim in MATLAB and you'll notice this note

A and REF can be arrays of up to three dimensions. All 3D arrays are considered 3D volumetric images. RGB images will also be processed as 3D volumetric images.

Unconditionally treating N-dimensional RGB images as N+1 dimensional gray images is not a good idea because

  1. filtering a length-3 dimension almost certainly introduces artifacts due to the padding strategy. For instance, SSIM by default applies a gaussian kernel of size 11, and thus the image is padded to at least size (h+10, w+10, 3+10) before applying the filter.
  2. the channel contains quite different information from height/width, and if you try to mix them together, you usually don't get a good result.

This is why ImageQualityIndexes.jl deliberately diverges from MATLAB's numeric results.


To simply verify that MATLAB uses 3D volumic definition and ImageQualityIndexes use the mean of channelwise SSIM definition, I use the "peppers_color" and "mandrill" color image from TestImages.jl.

% MATLAB
img1 = imread("peppers_color.tif");
img2 = imread("mandrill.tiff");

% weighted sum of the channelwise ssim ssim_r = ssim(img1(:,:,1), img2(:,:,1)) % 0.1468 ssim_g = ssim(img1(:,:,2), img2(:,:,2)) % 0.1037 ssim_b = ssim(img1(:,:,3), img2(:,:,3)) % 0.0911 wssim = 1/3 * (ssim_r + ssim_g + ssim_b) % 0.1139

% MATLAB's built-in ssim ssim(img1, img2) % 0.1219

# julia
using ImageQualityIndexes, TestImages
img1 = testimage("peppers_color")
img2 = testimage("mandrill")
assess_ssim(img1, img2) # 0.11387

References:

  • [1] Wang, Z., Bovik, A. C., Sheikh, H. R., & Simoncelli, E. P. (2004). Image quality assessment: from error visibility to structural similarity. IEEE transactions on image processing, 13(4), 600-612.
  • [2] Wang, Z., Bovik, A. C., Sheikh, H. R., & Simoncelli, E. P. (2003). The SSIM Index for Image Quality Assessment. Retrived May 30, 2019, from http://www.cns.nyu.edu/~lcv/ssim/
-1

It's not indicated for colors, it only accounts for Structure. You could check more info on this paper Understanding SSIM.

An alternative presented by the original authors on the paper Video Quality Assessment Based on Structural Distortion Measurement is to take a weighted average of SSIM results per channel on the YCrCb space like $SSIM_{final} = 0.8 \times SSIM_y + 0.1 \times SSIM_{Cr} + 0.1 \times SSIM_{Cb}$.

  • I just checked SSIM between two images which are same size and single color. SSIM is 1 for same color images. Differs when colors are far. So this shows color is taken into account. I think you are mistaken. – Lyrk Mar 11 '22 at 19:35
  • SSIM does not compare colors, what you're doing is to compare 3 different images, one for each channel; in which the algorithm evaluates structure, luminance, and contrast information with a reference image. – Rafael Toledo Mar 11 '22 at 22:24