Calculating Similarity between Two Clusters in QGIS?

I am new to GIS and I have a question to ask about how to calculate the similarity between two rasters in QGIS.

I have generated two interpolations of plant water status in the exact same field for 2 years. Then I used KMeans classification to classify the images (Rasters) into two clusters.

The problem I am currently facing is how to determine the similarity between the two clustering maps from these 2 years (the whole field performed the same or not from year to year?).

I have been looking for the solution, but not yet find one applicable for me. I use QGIS, ArcMap and R.

• Can you subtract one raster from the other? That gives you the difference. If you want the total difference over the whole raster, square the pixel differences and sum them. Commented Jan 24, 2018 at 8:15
• Thank you for your reply! I figured out how to calculate the difference by simply subtract one raster from the other. But still having a hard time to calculate the total difference over the whole raster because I do not know how to sum up the squares of the pixel differences when using raster calculator. Commented Jan 29, 2018 at 19:38
• Hi Spacedman, I just calculated the total difference as well. Is there a way to extract the proportion of the identical area between the two rasters? Thank you! Commented Jan 29, 2018 at 19:45

This is a great question and something I have wrestled with quite a bit! The pursuit of pattern similarity metrics leads down a rabbit hole to entire fields of statistical, spatial, and even fractal methods of analyzing patterns. Each of these methods has applications in geoscience and geospatial analysis, but none offer complete measures of pattern similarity. With a little imagination we can construct spatial patterns which have identical means, standard deviations, and probability distributions, but which entirely lack similar structures, or any recognizable structures at all. For example, the patterns below are quite similar statistically in some cases - yet are not similar at all to our human eyes.

We can increase complexity and try spatial metrics (e.g. semivariance, global autocorrelation) but they may also struggle to separate two well organized patterns that are otherwise divergent. Fractal measures of dimension and roughness may tell us something about similarity and scale - but can be difficult to implement and interpret.

We need a more robust pattern analyzing machine, and fortunately Nature has produced such a machine for us to use already: the human visual system. Humans are adept at recognizing and comparing visual patterns. The similarity analysis methods I've been working with derive from research on how the human visual system recognizes the similarity of two patterns. I've been experimenting with 5 methods:

Mean Square Error (MSE) (this is the method suggested by @Spacedman) MSE is a familiar, easy to implement, and easy to interpret metric of pattern similarity. However, it can be a poor measure of perceptual similarity because it is sensitive to geometric distortions. We can fool MSE: take two images, a reference image and a target image. They are almost identical, but the target image has been geometrically translated by a small amount. Such a registration shift will cause high MSE values indicating poor similarity, but a human observer would see little difference in the information content of the images because humans automatically do the translation.

Structural Similarity Index Method (SSIM) SSIM measures the local similarity of three elements: luminance (brightness values), contrast, and structure. These similarities are combined to form SSIM values. SSIM scores range from -1 to +1, with the latter score indicating that the reference and target patterns are identical. While the SSIM index is more nuanced than MSE because it contains perceptually important information like contrast and structure, it is still flawed because it is also sensitive to geometric translations, scalings, and rotations.

Complex Wavelet SSIM (CW-SSIM) The drawbacks of SSIM bring us to a wavelet-domain version of SSIM. The complex wavelet implementation compares sets of coefficients extracted at the same spatial location in the same wavelet subbands of two images. The consistency of phase changes between the coefficients measures how consistent structural information is between the two images. CW-SSIM is robust to translations and small scaling and rotational differences. Note that results can depend on the parameters of the wavelet. Wang and Bovik (2009) provide an excellent summary of MSE, SSIM, and CW-SSIM.

Gradient Magnitude Similarity Deviation (GMSD) GMSD is included in this analysis because it outperforms a variety of SSIM implementations for many test databases and is computationally more efficient. In this implementation, image gradient maps are computed in the x and y directions using a 3x3 Prewitt filter. A gradient magnitude similarity (GMS) map combines the gradients of the reference and target images, and the standard deviation of the GMS map is computed as the overall measure of pattern similarity. A lower GMSD value indicates higher quality and less distortion (i.e., less difference) in the two structures (Xue et al., 2014).

Feature Similarity (FSIM) FSIM is a high performing but computationally expensive method. Phase congruency and gradient magnitude images are created for the reference and target images. The similarities between these products are computed and combined, and then weighted using maximum phase congruency to produce the FSIM score. Higher FSIM values indicate better similarity (Zhang et al., 2011).