using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace Correlation____Test
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;
for (int y = 0; y < Height; y++)
for (int x = 0; x < Width; x++)
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;
public partial class CorrelationCoefficientForm : Form
public CorrelationCoefficientForm()
string path1 = @"E:\Lenagr.png";
string path2 = @"E:\Lenagr.png";
private void button1_Click(object sender, EventArgs e)
Bitmap lena1 = Bitmap.FromFile(path1) as Bitmap;
Bitmap lena2 = Bitmap.FromFile(path2) as Bitmap;
pictureBox1.Image = lena1;
pictureBox2.Image = lena2;
private void button2_Click(object sender, EventArgs e)
Bitmap lena1 = Grayscale.ToGrayscale(pictureBox1.Image as Bitmap);
Bitmap lena2 = Grayscale.ToGrayscale(pictureBox2.Image as Bitmap);
double [,] image1 = ImageDataConverter.ToDouble(lena1);
double [,] image2 = ImageDataConverter.ToDouble(lena2);
double corr = PearsonCorrelation.GetSimilarityScore(image1, image2);
MessageBox.Show(corr.ToString());
partial class CorrelationCoefficientForm
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
if (disposing && (components != null))
#region Windows Form Designer generated code
private void InitializeComponent()
this.button1 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
this.button1.Location = new System.Drawing.Point(12, 268);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(250, 37);
this.button1.TabIndex = 0;
this.button1.Text = "Load Images";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pictureBox1.Location = new System.Drawing.Point(12, 12);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(250, 250);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.pictureBox1.TabIndex = 1;
this.pictureBox1.TabStop = false;
this.pictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pictureBox2.Location = new System.Drawing.Point(268, 12);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(250, 250);
this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.pictureBox2.TabIndex = 3;
this.pictureBox2.TabStop = false;
this.button2.Location = new System.Drawing.Point(268, 268);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(250, 37);
this.button2.TabIndex = 2;
this.button2.Text = "Calculate Correlations Coefficient";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(529, 318);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.button2);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button1);
this.Name = "CorrelationCoefficientForm";
this.Text = "CorrelationCoefficientForm";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
this.ResumeLayout(false);
private System.Windows.Forms.Button button1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.Button button2;
public partial class Grayscale
public static unsafe Bitmap ToGrayscale(Bitmap colorBitmap)
int Width = colorBitmap.Width;
int Height = colorBitmap.Height;
Bitmap grayscaleBitmap = new Bitmap(Width, Height, PixelFormat.Format8bppIndexed);
grayscaleBitmap.SetResolution(colorBitmap.HorizontalResolution,
colorBitmap.VerticalResolution);
ColorPalette colorPalette = grayscaleBitmap.Palette;
for (int i = 0; i < colorPalette.Entries.Length; i++)
colorPalette.Entries[i] = Color.FromArgb(i, i, i);
grayscaleBitmap.Palette = colorPalette;
BitmapData bitmapData = grayscaleBitmap.LockBits(
new Rectangle(Point.Empty, grayscaleBitmap.Size),
ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
Byte* pPixel = (Byte*)bitmapData.Scan0;
for (int y = 0; y < Height; y++)
for (int x = 0; x < Width; x++)
Color clr = colorBitmap.GetPixel(x, y);
Byte byPixel = (byte)((30 * clr.R + 59 * clr.G + 11 * clr.B) / 100);
pPixel += bitmapData.Stride;
grayscaleBitmap.UnlockBits(bitmapData);
public partial class ImageDataConverter
public static double[,] ToDouble(Bitmap input)
BitmapData bitmapData = input.LockBits(new Rectangle(0, 0, input.Width, input.Height),
PixelFormat.Format8bppIndexed);
int height = input.Height;
int pixelSize = Bitmap.GetPixelFormatSize(input.PixelFormat) / 8;
int offset = bitmapData.Stride - bitmapData.Width * pixelSize;
double[,] output = new double[width, height];
fixed (double* ptrData = output)
byte* src = (byte*)bitmapData.Scan0;
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
byte srscsc = (byte)(*src);
double scaled = Tools.Scale(srscsc, 0, 255, Min, Max);
input.UnlockBits(bitmapData);
public static partial class Tools
public static double Scale(this double value, double fromMin, double fromMax, double toMin, double toMax)
double result = (max - min) * (value - fromMin) / (fromMax - fromMin) + min;
public static class Program
public static void Main()
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new CorrelationCoefficientForm());