0

I have written the following C# code to find the Pearson correlation coefficient between two Images. The complete source code is here in the DotNetFiddle.

Correlation Source Code:

public sealed class PearsonCorrelation 
{
    public static double GetSimilarityScore(double[,] p, double[,] q)
    {
        int Width = p.GetLength(0);
        int Height = p.GetLength(1);

        if (Width != q.GetLength(0) || Height != q.GetLength(1))
        {
            throw new ArgumentException("Input vectors must be of the same dimension.");
        }

        double pSum = 0, qSum = 0, pSumSq = 0, qSumSq = 0, productSum = 0;
        double pValue, qValue;

        for (int y = 0; y < Height; y++)
        {
            for (int x = 0; x < Width; x++)
            {
                pValue = p[y, x];
                qValue = q[y, x];

                pSum += pValue;
                qSum += qValue;
                pSumSq += pValue * pValue;
                qSumSq += qValue * qValue;
                productSum += pValue * qValue;
            }
        }

        double numerator = productSum - ((pSum * qSum) / (double)Height);
        double denominator = Math.Sqrt((pSumSq - (pSum * pSum) / (double)Height) * (qSumSq - (qSum * qSum) / (double)Height));

        return (denominator == 0) ? 0 : numerator / denominator;
    }
}

Result:

enter image description here

Same picture is loaded in two picture boxes.

The value of their correlation coefficient has become, -1.

Is this the correct result?

If no, what should I do to correct it?

3
  • 1
    As far as I can see in the final formula for the numerator and denominator you should divide by n - number of points - which is Height * Width, not just Height Commented Aug 17, 2016 at 20:09
  • It would be very helpful if you could provide us with a reference to the formula you use. However, I believe that the result must be 1 since the two images are the same. Moreover, since the denominator is always positive, there must be something wrong with the numerator. Commented Aug 17, 2016 at 20:11
  • @Mahdi, github.com/cureos/aforge/blob/master/Sources/Math/Metrics/… Commented Aug 17, 2016 at 20:19

1 Answer 1

1

The correlation of the identical sequences must be 1

It seems that you have a problem at the very end of the routine; instead of

 double numerator = productSum - ((pSum * qSum) / (double)Height);
 double denominator = Math.Sqrt((pSumSq - (pSum * pSum) / (double)Height) * (qSumSq - (qSum * qSum) / (double)Height));

you should put

 double n = ((double) Width) * Height;

 double numerator = productSum - ((pSum * qSum) / n);
 double denominator = 
   Math.Sqrt((pSumSq - (pSum * pSum) / n) * (qSumSq - (qSum * qSum) / n));

see

https://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient

Sign up to request clarification or add additional context in comments.

5 Comments

Did you inspect my Fiddle code for any other troubles?
@anonymous: I failed to have executed it: using System.Windows.Forms; seems to prevent it from running
Fiddle doesn't have any mechanism to execute WinForms and unsafe codes.
@anonymous: yes, I see; but your code uses WinForms; more over it obtains test data (arrays) via button1_Click loading them from @"E:\Lenagr.png". It would have been much better if you had made an abstract of the routine which can be executed and debugged.
You can just copy-paste the code to a VS2013 Windows Forms solution. And, use any one of these images: google.com/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.