private void повернутиЕталонНаКутToolStripMenuItem_Click(object sender, EventArgs e) { byte[,,] rgb = RGB.BitmapToByteRgbNaive(Im[comboBox1.SelectedIndex]); int width = Im[comboBox1.SelectedIndex].Width, height = Im[comboBox1.SelectedIndex].Height; byte[,,] rgbN = new byte[3, height + 2, width + 2]; byte[,] tmp = new byte[height + 2, width + 2]; double alpha = CalcOrientashion(rgb, width, height); double phi = Convert.ToDouble(toolStripTextBox6.Text); List <double> nY = new List <double>(); List <double> nX = new List <double>(); double dx = 0, dy = 0; int count = 0; int pidizri = 0; int nevirno = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (rgb[0, y, x] == 255 && rgb[1, y, x] == 255 && rgb[2, y, x] == 255) { ; } else { nX.Add(Math.Round((x - (int)(width / 2d)) * Math.Cos(phi * Math.PI / 180) + (y - (int)(height / 2d)) * Math.Sin(phi * Math.PI / 180), 4)); nY.Add(Math.Round(-1.0 * (x - (int)(width / 2d)) * Math.Sin(phi * Math.PI / 180) + (y - (int)(height / 2d)) * Math.Cos(phi * Math.PI / 180), 4)); //dx = Math.Abs(); count++; if (tmp[(int)(height / 2d) + (int)nY[nY.Count - 1], (int)(width / 2d) + (int)nX[nX.Count - 1]] == 0) { tmp[(int)(height / 2d) + (int)nY[nY.Count - 1], (int)(width / 2d) + (int)nX[nX.Count - 1]] = 1; } else { pidizri++; if ((int)(height / 2d) + (int)nY[nY.Count - 1] - 1 >= 0 && (int)(width / 2d) + (int)nX[nX.Count - 1] - 1 >= 0 && (int)(height / 2d) + (int)nY[nY.Count - 1] + 1 < height && (int)(width / 2d) + (int)nX[nY.Count - 1] + 1 < width) { if (tmp[(int)(height / 2d) + (int)nY[nY.Count - 1] - 1, (int)(width / 2d) + (int)nX[nX.Count - 1]] == 0)//-1,0 { tmp[(int)(height / 2d) + (int)nY[nY.Count - 1] - 1, (int)(width / 2d) + (int)nX[nX.Count - 1]] = 1; } else if (tmp[(int)(height / 2d) + (int)nY[nY.Count - 1], (int)(width / 2d) + (int)nX[nX.Count - 1] - 1] == 0)//0,-1 { tmp[(int)(height / 2d) + (int)nY[nY.Count - 1], (int)(width / 2d) + (int)nX[nX.Count - 1] - 1] = 1; } else if (tmp[(int)(height / 2d) + (int)nY[nY.Count - 1], (int)(width / 2d) + (int)nX[nY.Count - 1] + 1] == 0)//0,+1 { tmp[(int)(height / 2d) + (int)nY[nY.Count - 1], (int)(width / 2d) + (int)nX[nY.Count - 1] + 1] = 1; } else if (tmp[(int)(height / 2d) + (int)nY[nY.Count - 1] + 1, (int)(width / 2d) + (int)nX[nY.Count - 1]] == 0)//+1,0 { tmp[(int)(height / 2d) + (int)nY[nY.Count - 1] + 1, (int)(width / 2d) + (int)nX[nY.Count - 1]] = 1; } else if (tmp[(int)(height / 2d) + (int)nY[nY.Count - 1] - 1, (int)(width / 2d) + (int)nX[nY.Count - 1] - 1] == 0)//-1,-1 { tmp[(int)(height / 2d) + (int)nY[nY.Count - 1] - 1, (int)(width / 2d) + (int)nX[nY.Count - 1] - 1] = 1; } else if (tmp[(int)(height / 2d) + (int)nY[nY.Count - 1] - 1, (int)(width / 2d) + (int)nX[nY.Count - 1] + 1] == 0)//-1,+1 { tmp[(int)(height / 2d) + (int)nY[nY.Count - 1] - 1, (int)(width / 2d) + (int)nX[nY.Count - 1] + 1] = 1; } else if (tmp[(int)(height / 2d) + (int)nY[nY.Count - 1] + 1, (int)(width / 2d) + (int)nX[nY.Count - 1] - 1] == 0)//+1,-1 { tmp[(int)(height / 2d) + (int)nY[nY.Count - 1] + 1, (int)(width / 2d) + (int)nX[nY.Count - 1] - 1] = 1; } else if (tmp[(int)(height / 2d) + (int)nY[nY.Count - 1] + 1, (int)(width / 2d) + (int)nX[nY.Count - 1] + 1] == 0)//+1,+1 { tmp[(int)(height / 2d) + (int)nY[nY.Count - 1] + 1, (int)(width / 2d) + (int)nX[nY.Count - 1] + 1] = 1; } else { nevirno++; } } } } } } MessageBox.Show("Кіл. точок не фону = " + count.ToString()); MessageBox.Show("Pidozri= " + pidizri.ToString()); MessageBox.Show("Nevorno= " + nevirno.ToString()); count = 0; for (int y = 0; y < height + 2; y++) { for (int x = 0; x < width + 2; x++) { if (tmp[y, x] == 1) { rgbN[0, y, x] = 255; rgbN[1, y, x] = 255; rgbN[2, y, x] = 255; count++; } else { rgbN[0, y, x] = 0; rgbN[1, y, x] = 0; rgbN[2, y, x] = 0; } } } MessageBox.Show("Кіл. точок без пропусків на зобр = " + count.ToString()); Im.Add(RGB.RgbToBitmapNaive(rgbN)); comboBox1.Items.Add(comboBox1.Text + "+Povorot"); comboBox1.Text = comboBox1.Text + "+Povorot"; label2.Text = "Povorot"; }
private void однаГраницяToolStripMenuItem_Click(object sender, EventArgs e) { List <byte> ColorGREY = new List <byte>(); byte[,,] rgb = RGB.BitmapToByteRgbNaive(Im[comboBox1.SelectedIndex]); int width = Im[comboBox1.SelectedIndex].Width, height = Im[comboBox1.SelectedIndex].Height; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { ColorGREY.Add(RGB.Limit(0.2125 * rgb[0, y, x] + 0.7154 * rgb[1, y, x] + 0.0721 * rgb[2, y, x])); } } int Porig1GREY = otsuThreshold(ColorGREY, 4); byte[,,] rgbN = new byte[3, height, width]; double buf = 0; PersentBin = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { buf = 0.2125 * rgb[0, y, x] + 0.7154 * rgb[1, y, x] + 0.0721 * rgb[2, y, x]; if (buf <= Porig1GREY) { PersentBin++; rgbN[0, y, x] = RGB.Limit(255); rgbN[1, y, x] = RGB.Limit(255); rgbN[2, y, x] = RGB.Limit(255); } else { rgbN[0, y, x] = RGB.Limit(0); rgbN[1, y, x] = RGB.Limit(0); rgbN[2, y, x] = RGB.Limit(0); } //if (buf <= Porig1GREY) //{ // PersentBin++; // rgbN[0, y, x] = RGB.Limit(0); // rgbN[1, y, x] = RGB.Limit(0); // rgbN[2, y, x] = RGB.Limit(0); //} //else //{ // rgbN[0, y, x] = RGB.Limit(255); // rgbN[1, y, x] = RGB.Limit(255); // rgbN[2, y, x] = RGB.Limit(255); //} } } PersentBin /= (width * height); Im.Add(RGB.RgbToBitmapNaive(rgbN)); comboBox1.Items.Add(comboBox1.Text + "+Bin"); comboBox1.Text = comboBox1.Text + "+Bin"; label2.Text = "Bin"; label3.Text = Math.Round(PersentBin, 4).ToString(); }
private void розмиканняToolStripMenuItem_Click(object sender, EventArgs e) { byte[,,] rgb = RGB.BitmapToByteRgbNaive(Im[comboBox1.SelectedIndex]); int width = Im[comboBox1.SelectedIndex].Width, height = Im[comboBox1.SelectedIndex].Height; byte[,,] rgbE = new byte[3, height, width]; byte[,,] rgbEN = new byte[3, height, width]; int Rozmir_maski_S = 1; int[,] S = StructElement(toolStripComboBox1.SelectedIndex); bool nullPoint = false; for (int y = Rozmir_maski_S; y < height - Rozmir_maski_S; y++) { for (int x = Rozmir_maski_S; x < width - Rozmir_maski_S; x++) { if (rgb[0, y, x] == 255) { nullPoint = false; for (int i = -Rozmir_maski_S; i <= Rozmir_maski_S; i++) { for (int j = -Rozmir_maski_S; j <= Rozmir_maski_S; j++) { if (S[1 + i, 1 + j] == 1) { if (rgb[0, y + i, x + j] == 0) { nullPoint = true; } } } } if (nullPoint == true) { rgbE[0, y, x] = RGB.Limit(0); rgbE[1, y, x] = RGB.Limit(0); rgbE[2, y, x] = RGB.Limit(0); } else { rgbE[0, y, x] = RGB.Limit(255); rgbE[1, y, x] = RGB.Limit(255); rgbE[2, y, x] = RGB.Limit(255); } } } } for (int y = Rozmir_maski_S; y < height - Rozmir_maski_S; y++) { for (int x = Rozmir_maski_S; x < width - Rozmir_maski_S; x++) { if (rgbE[0, y, x] == 255) { for (int i = -Rozmir_maski_S; i <= Rozmir_maski_S; i++) { for (int j = -Rozmir_maski_S; j <= Rozmir_maski_S; j++) { if (S[1 + i, 1 + j] == 1) { rgbEN[0, y + i, x + j] = RGB.Limit(255); rgbEN[1, y + i, x + j] = RGB.Limit(255); rgbEN[2, y + i, x + j] = RGB.Limit(255); } } } } } } Im.Add(RGB.RgbToBitmapNaive(rgbEN)); comboBox1.Items.Add(comboBox1.Text + "+R" + toolStripComboBox1.SelectedIndex); comboBox1.Text = comboBox1.Text + "+R" + toolStripComboBox1.SelectedIndex; label2.Text = "R" + toolStripComboBox1.SelectedIndex; }
private void korrToolStripMenuItem_Click(object sender, EventArgs e) { byte[,,] test = RGB.BitmapToByteRgbNaive(Test); byte[,,] etal = RGB.BitmapToByteRgbNaive(Etalon); double ScalarEtal = 0; double sumEtal = 0; double EPR_Etal = 0; double Power_Etal = 0; double ScalarTest = 0; double sumTest = 0; double EPR_Test = 0; double Power_Test = 0; double KorrMax = 0; double Korr = 0; int IndexMaxKorr = 0; int width = 0, height = 0; // Задать рамеры // лучше сначала делать выбор обьектов из базы данных по эпру, находить максимальные рамеры и сравнивать с тестом и приводить к //одному размеру if (Test.Height == Etalon.Height && Test.Width == Etalon.Width) { width = Test.Width; height = Test.Height; } else { width = Math.Max(Test.Width, Etalon.Width); height = Math.Max(Test.Height, Etalon.Height); } //Считывание ЕПР или Мощьности для эталона и теста Zadat_EPR EPR = new Zadat_EPR(); EPR.ShowDialog(); if (EPR.DialogResult == DialogResult.OK) { EPR_Etal = EPR.EPR_Etalon; EPR_Test = EPR.EPR_Test; } // Sum RGB for (int y = 0; y < Etalon.Height; y++) { for (int x = 0; x < Etalon.Width; x++) { sumEtal += 255 - etal[0, y, x]; } } for (int y = 0; y < Etalon.Height; y++) { for (int x = 0; x < Etalon.Width; x++) { //ScalarEtal += Math.Pow(255 - etal[0, y, x], 2) + Math.Pow(255 - etal[1, y, x], 2) + Math.Pow(255 - etal[2, y, x], 2); ScalarEtal += Math.Pow((255 - etal[0, y, x]) * EPR_Etal / sumEtal, 2); } } // Scalar RBG to EPR // Если задан EPR if (EPR_Etal != 0 && EPR_Test != 0) { for (int y = 0; y < Test.Height; y++) { for (int x = 0; x < Test.Width; x++) { sumTest += 255 - test[0, y, x]; } } for (int y = 0; y < Test.Height; y++) { for (int x = 0; x < Test.Width; x++) { //ScalarTest += Math.Pow(255 - test[0, y, x], 2) + Math.Pow(255 - test[1, y, x], 2) + Math.Pow(255 - test[2, y, x], 2); ScalarTest += Math.Pow((255 - test[0, y, x]) * EPR_Test / sumTest, 2); } } } //!!!! Проверка по ЕПР, но без учета размера if (ScalarEtal < ScalarTest) { MessageBox.Show("Порівняння не доцільне! Скаляр Еталона меньше чем у теста."); } // Изменить размеры изображений чтобы были один к одному if (Test.Height == height && Test.Width == width) { test = RGB.BitmapToByteRgbNaive(Test); } else { Test = privedenie_k_odnomu_razmeru(Test, width, height); test = RGB.BitmapToByteRgbNaive(Test); } if (Etalon.Height == height && Etalon.Width == width) { etal = RGB.BitmapToByteRgbNaive(Etalon); } else { Etalon = privedenie_k_odnomu_razmeru(Etalon, width, height); etal = RGB.BitmapToByteRgbNaive(Etalon); } //Создание матрицы поворота и выбор максимальной корреляции for (int i = 0; i <= 360; i++) { etal = RGB.BitmapToByteRgbNaive(rotIm(Etalon, (float)i)); Korr = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { //Korr +=(255 - etal[0, y, x])* (255 - test[0, y, x]) + (255 - etal[1, y, x]) * (255 - test[1, y, x]) + (255 - etal[2, y, x]) * (255 - test[2, y, x]); Korr += ((255 - etal[0, y, x]) * EPR_Etal / sumEtal) * ((255 - test[0, y, x]) * EPR_Test / sumTest); } } if (Korr > KorrMax) { IndexMaxKorr = i; KorrMax = Korr; } } MessageBox.Show("Angle = " + IndexMaxKorr.ToString() + " korr = " + (KorrMax / ScalarEtal).ToString()); }