internal static OutputWrapper CompareWithTolerance(Bitmap img1, Bitmap img2, int areaRadius, float percentageOfTruth) { int differentPixels = 0; int allPixels = 0; OutputWrapper reducedDifferences = new OutputWrapper(); using (OutputWrapper originalDifferences = Compare(img1, img2, areaRadius)) { reducedDifferences.Bitmap = new Bitmap(img1); reducedDifferences.BoolArr = new bool[originalDifferences.BoolArr.GetLength(0), originalDifferences.BoolArr.GetLength(1)]; reducedDifferences.PointsList = new List <Point>(); Parallel.ForEach(originalDifferences.PointsList, point => { lock (reducedDifferences.Bitmap) { int i = point.X; int j = point.Y; float innerDifferentPixelsAggregated = 0; float innerAllPixelsAggregated = 0; Parallel.For(0 - areaRadius, areaRadius, k => { Parallel.For(0 - areaRadius, areaRadius, l => { if (originalDifferences.BoolArr[i + k, j + l] == true) { innerDifferentPixelsAggregated++; } innerAllPixelsAggregated++; }); }); if ((innerDifferentPixelsAggregated / innerAllPixelsAggregated) * innerAllPixelsAggregated >= (percentageOfTruth * innerAllPixelsAggregated)) { differentPixels++; reducedDifferences.PointsList.Add(new Point(i, j)); reducedDifferences.Bitmap.SetPixel(i, j, Color.Red); reducedDifferences.BoolArr[i, j] = true; } else { reducedDifferences.BoolArr[i, j] = false; } } }); allPixels = originalDifferences.AllPixels; } decimal a = Decimal.Divide(allPixels - differentPixels, allPixels) * 100; reducedDifferences.Percentage = Decimal.Round(a, 4); reducedDifferences.DifferentPixels = differentPixels; reducedDifferences.AllPixels = allPixels; return(reducedDifferences); }
internal static OutputWrapper ReturnSortedColors(OutputWrapper _outputWrapper) { List <string> lista = new List <string>(); foreach (var item in _outputWrapper.Colors) { lista.Add($"{item.Name}"); } lista.Sort(); OutputWrapper outputWrapper = _outputWrapper; outputWrapper.ColorsSorted = lista; return(outputWrapper); }
public OutputWrapper Compare(Bitmap img1, Bitmap img2) { int differentPixels = 0; int allPixels = 0; OutputWrapper outputWrapper = new OutputWrapper(); outputWrapper.BoolArr = new bool[img1.Width, img1.Height]; //outputWrapper.Bitmap = img1; if (img1.Width == img2.Width && img1.Height == img2.Height) { ParallelLoopResult X = Parallel.For(0, img1.Width, i => { lock (img1) { for (int j = 0; j < img1.Height; j++) { if (img1.GetPixel(i, j) != img2.GetPixel(i, j)) { outputWrapper.BoolArr[i, j] = true; //img1.SetPixel(i, j, Color.Red); differentPixels++; break; } else { outputWrapper.BoolArr[i, j] = false; } allPixels++; } } }); } //outputWrapper.Bitmap = img1; int a = allPixels - differentPixels; decimal b = Decimal.Divide(a, allPixels); decimal c = b * 100; outputWrapper.Percentage = Decimal.Round(c, 4); outputWrapper.DifferentPixels = differentPixels; outputWrapper.AllPixels = allPixels; return(outputWrapper); }
private void button5_Click(object sender, EventArgs e) { dataGridView1.ColumnCount = 0; Bitmap img1 = new Bitmap(filePath1); Bitmap img2 = new Bitmap(filePath2); int radius = 0; if (comboBox1.SelectedItem == null) { radius = 1; } else { radius = Convert.ToInt32(comboBox1.SelectedItem); } OutputWrapper outputWrapper = ConverterImg.Compare(img1, img2, radius); var rowCountY = outputWrapper.BoolArr.GetLength(1); var rowLengthX = outputWrapper.BoolArr.GetLength(0); for (int i = 0; i < rowLengthX; i++) { dataGridView1.Columns.Add($"{i}", $"{i}"); dataGridView1.Columns[i].Width = i.ToString().Length * 10; } for (int i = 0; i < outputWrapper.BoolArr.GetLength(1); i++) { var row = new DataGridViewRow(); for (int j = 0; j < outputWrapper.BoolArr.GetLength(0); j++) { row.Cells.Add(new DataGridViewTextBoxCell() { Value = Convert.ToInt16(outputWrapper.BoolArr[j, i]) }); } dataGridView1.Rows.Add(row); } dataGridView1.Refresh(); }
internal static OutputWrapper Compare(Bitmap img1, Bitmap img2, int areaRadius) { int differentPixels = 0; int allPixels = 0; OutputWrapper outputWrapper = new OutputWrapper(); outputWrapper.BoolArr = new bool[img1.Width, img1.Height]; outputWrapper.Bitmap = new Bitmap(img1); outputWrapper.PointsList = new List <Point>(); if (img1.Width == img2.Width && img1.Height == img2.Height) { ParallelLoopResult X = Parallel.For(0, img1.Width, i => { lock (img1) { for (int j = 0 + areaRadius; j < img1.Height - areaRadius; j++) { Color pixel1 = img1.GetPixel(i, j); Color pixel2 = img2.GetPixel(i, j); if (!(pixel1.Equals(pixel2))) { outputWrapper.BoolArr[i, j] = true; outputWrapper.Bitmap.SetPixel(i, j, Color.Red); differentPixels++; outputWrapper.PointsList.Add(new Point(i, j)); } else { outputWrapper.BoolArr[i, j] = false; } allPixels++; } } }); } decimal a = Decimal.Divide(allPixels - differentPixels, allPixels) * 100; outputWrapper.Percentage = Decimal.Round(a, 4); outputWrapper.DifferentPixels = differentPixels; outputWrapper.AllPixels = allPixels; return(outputWrapper); }
public OutputWrapper CompareWithTolerance(Bitmap img1, Bitmap img2, int areaRadius, int percentageOfTruth) { if (areaRadius == 0) { areaRadius = 1; } int differentPixels = 0; int allPixels = 0; OutputWrapper reducedDifferences = new OutputWrapper(); using (OutputWrapper originalDifferences = new OutputWrapper()) using (ConverterDependency converterDependency = new ConverterDependency()) { converterDependency.Compare(img1, img2); reducedDifferences.Bitmap = new Bitmap(img1); reducedDifferences.BoolArr = new bool[originalDifferences.BoolArr.GetLength(0), originalDifferences.BoolArr.GetLength(1)]; reducedDifferences.Colors = new List <Color>(); ParallelLoopResult Y = Parallel.For(areaRadius, originalDifferences.BoolArr.GetLength(0) - areaRadius, i => { Parallel.For(areaRadius, (originalDifferences.BoolArr.GetLength(1) - areaRadius), j => { int innerDifferentPixelsAggregated = 0; int innerAllPixelsAggregated = 0; lock (reducedDifferences.Bitmap) { if (!(reducedDifferences.Colors.Contains(reducedDifferences.Bitmap.GetPixel(i, j)))) { Color color = reducedDifferences.Bitmap.GetPixel(i, j); reducedDifferences.Colors.Add(color); } Parallel.For(0 - areaRadius, areaRadius, k => { for (int l = 0 - areaRadius; l < areaRadius; l++) { if (originalDifferences.BoolArr[i + k, j + l] == true) { innerDifferentPixelsAggregated++; } innerAllPixelsAggregated++; } }); decimal areaRadiusD = areaRadius; decimal percentageOfTruthD = percentageOfTruth / 10; percentageOfTruthD = percentageOfTruthD / 10; decimal x = areaRadiusD * areaRadiusD * percentageOfTruthD; if (Convert.ToDecimal(innerDifferentPixelsAggregated) > x) { differentPixels++; reducedDifferences.BoolArr[i, j] = true; reducedDifferences.Bitmap.SetPixel(i, j, Color.Red); } else { reducedDifferences.BoolArr[i, j] = false; } } allPixels++; }); }); } int a = allPixels - differentPixels; decimal b = Decimal.Divide(a, allPixels); decimal c = b * 100; reducedDifferences.Percentage = Decimal.Round(c, 4); reducedDifferences.DifferentPixels = differentPixels; reducedDifferences.AllPixels = allPixels; return(reducedDifferences); }
public OutputWrapper ReturnSortedColors(OutputWrapper _outputWrapper) { return(comparer2.ReturnSortedColors(_outputWrapper)); }
private void button6_Click(object sender, EventArgs e) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); int radius = 0; float percentageOfTruth = 0; if (comboBox1.SelectedItem == null) { radius = 1; } else { radius = Convert.ToInt32(comboBox1.SelectedItem); } if (comboBox2.SelectedItem == null) { percentageOfTruth = 0.01f; } else { percentageOfTruth = float.Parse(comboBox2.SelectedItem.ToString()); } dataGridView1.ColumnCount = 0; Bitmap img1 = new Bitmap(filePath1); Bitmap img2 = new Bitmap(filePath2); OutputWrapper outputWrapper = ConverterImg.CompareWithTolerance( img1: img1, img2: img2, areaRadius: radius, percentageOfTruth: percentageOfTruth / 100); pictureBox3.Image = outputWrapper.Bitmap; var rowCountY = outputWrapper.BoolArr.GetLength(1); var rowLengthX = outputWrapper.BoolArr.GetLength(0); for (int i = 0; i < rowLengthX; i++) { dataGridView1.Columns.Add($"{i}", $"{i}"); dataGridView1.Columns[i].Width = i.ToString().Length * 10; } for (int i = 0; i < outputWrapper.BoolArr.GetLength(1); i++) { var row = new DataGridViewRow(); for (int j = 0; j < outputWrapper.BoolArr.GetLength(0); j++) { row.Cells.Add(new DataGridViewTextBoxCell() { Value = Convert.ToInt16(outputWrapper.BoolArr[j, i]) }); } dataGridView1.Rows.Add(row); } dataGridView1.Refresh(); textBox1.AppendText($"Pixel radius = {radius} with percentage of true valued cells:{percentageOfTruth}%{Environment.NewLine}"); //textBox1.AppendText($"{outputWrapper.Numeric1} % simmilarity{Environment.NewLine}"); textBox1.AppendText($"{outputWrapper.Percentage} % simmilarity{Environment.NewLine}out of {outputWrapper.AllPixels} pixels {outputWrapper.DifferentPixels} are different{Environment.NewLine}"); //textBox1.AppendText($"{outputWrapper.Colors.Count} different colors detected in picture{Environment.NewLine}"); //outputWrapper.ColorsSorted = Converter.ReturnSortedColors(outputWrapper).ColorsSorted; //outputWrapper.ColorsSorted = OutputWrapper.ReturnSortedColors(outputWrapper).ColorsSorted; //outputWrapper = new ConverterDependency().ReturnSortedColors(outputWrapper); //foreach (var item in outputWrapper.ColorsSorted) //{ // textBox1.AppendText($"{item}{Environment.NewLine}"); //} textBox1.AppendText($"Parallel execution in {stopwatch.Elapsed}{Environment.NewLine}"); }