皮尔逊相关系数
Pearson correlation coefficient
我编写了以下 C# 代码来查找两个图像之间的 Pearson 相关系数。完整源码here in the DotNetFiddle.
关联源代码:
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;
}
}
结果:
同一张图片被加载到两个图片框中。
他们的相关系数的值变成了,-1
.
这是正确的结果吗?
如果不是,我应该如何纠正?
相同序列的相关性必须是1
看来你在例程的最后有问题;而不是
double numerator = productSum - ((pSum * qSum) / (double)Height);
double denominator = Math.Sqrt((pSumSq - (pSum * pSum) / (double)Height) * (qSumSq - (qSum * qSum) / (double)Height));
你应该把
double n = ((double) Width) * Height;
double numerator = productSum - ((pSum * qSum) / n);
double denominator =
Math.Sqrt((pSumSq - (pSum * pSum) / n) * (qSumSq - (qSum * qSum) / n));
见
https://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient
我编写了以下 C# 代码来查找两个图像之间的 Pearson 相关系数。完整源码here in the DotNetFiddle.
关联源代码:
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;
}
}
结果:
同一张图片被加载到两个图片框中。
他们的相关系数的值变成了,-1
.
这是正确的结果吗?
如果不是,我应该如何纠正?
相同序列的相关性必须是1
看来你在例程的最后有问题;而不是
double numerator = productSum - ((pSum * qSum) / (double)Height);
double denominator = Math.Sqrt((pSumSq - (pSum * pSum) / (double)Height) * (qSumSq - (qSum * qSum) / (double)Height));
你应该把
double n = ((double) Width) * Height;
double numerator = productSum - ((pSum * qSum) / n);
double denominator =
Math.Sqrt((pSumSq - (pSum * pSum) / n) * (qSumSq - (qSum * qSum) / n));
见
https://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient