private void CalculateCorrelations() { StatusBarProgressBar.Visible = true; if (!CovarianceWorker.IsBusy) { CovarianceWorker.RunWorkerAsync(); } Cursor.Current = Cursors.WaitCursor; }
private void CovarianceTimer_Tick(object sender, EventArgs e) { if (dataIsInvalid) { if (CovarianceWorker.IsBusy) { CovarianceWorker.CancelAsync(); } sampleSize = (int)CorrelationSampleSize.Value; CalculateCorrelations(); dataIsInvalid = false; } }
private void CovarianceWorker_DoWork(object sender, DoWorkEventArgs e) { int progress = 0; int maxSteps = 3 * sampleSize; int step; x = new int [sampleSize]; y = new int [sampleSize]; xy = new int [sampleSize]; xy_hor = new int [sampleSize]; xy_ver = new int [sampleSize]; Random rand = new Random(); step = 1; for (int i = 0; i < sampleSize && !e.Cancel; ++i) { x[i] = rand.Next(0, input.Width - 1); y[i] = rand.Next(0, input.Height - 1); xy[i] = ArgbToY(input.GetPixel(x[i], y[i])); if (x[i] > 0) { xy_hor[i] = ArgbToY(input.GetPixel(x[i] - 1, y[i])); } else { xy_hor[i] = ArgbToY(input.GetPixel(x[i] + 1, y[i])); } if (y[i] > 0) { xy_ver[i] = ArgbToY(input.GetPixel(x[i], y[i] - 1)); } else { xy_ver[i] = ArgbToY(input.GetPixel(x[i], y[i] + 1)); } if (i % 10 == 0) { progress = (i * step * 100) / maxSteps; CovarianceWorker.ReportProgress(progress); } } if (e.Cancel) { return; } step = 2; expectedValueXY = 0; expectedValueXYHor = 0; expectedValueXYVer = 0; for (int i = 0; i < sampleSize && !e.Cancel; ++i) { expectedValueXY += (double)xy[i]; expectedValueXYHor += (double)xy_hor[i]; expectedValueXYVer += (double)xy_ver[i]; if (i % 10 == 0) { progress = (i * step * 100) / maxSteps; CovarianceWorker.ReportProgress(progress); } } if (e.Cancel) { return; } expectedValueXY /= (double)sampleSize; expectedValueXYHor /= (double)sampleSize; expectedValueXYVer /= (double)sampleSize; step = 3; varianceXY = 0; varianceXYHor = 0; varianceXYVer = 0; covarianceXYHor = 0; covarianceXYVer = 0; for (int i = 0; i < sampleSize && !e.Cancel; ++i) { double residual = (double)xy[i] - expectedValueXY; double residualHor = (double)xy_hor[i] - expectedValueXY; double residualVer = (double)xy_ver[i] - expectedValueXY; varianceXY += Math.Pow(residual, 2.0); varianceXYHor += Math.Pow(residualHor, 2.0); varianceXYVer += Math.Pow(residualVer, 2.0); covarianceXYHor += (residual * residualHor); covarianceXYVer += (residual * residualVer); if (i % 10 == 0) { progress = (i * step * 100) / maxSteps; CovarianceWorker.ReportProgress(progress); } } if (e.Cancel) { return; } varianceXY /= (double)sampleSize; varianceXYHor /= (double)sampleSize; varianceXYVer /= (double)sampleSize; covarianceXYHor /= (double)sampleSize; covarianceXYVer /= (double)sampleSize; correlationCoeffHor = covarianceXYHor / (Math.Sqrt(varianceXY) * Math.Sqrt(varianceXYHor)); correlationCoeffVer = covarianceXYVer / (Math.Sqrt(varianceXY) * Math.Sqrt(varianceXYVer)); } // End method CovarianceWorker_DoWork().