/* Харрис */ private void button5_Click(object sender, EventArgs e) { if (textBox6.Text != "" && textBox5.Text != "") { try { HarrisRadius = Convert.ToInt32(textBox6.Text); HarrisDolya = Convert.ToDouble(textBox5.Text); GrayImg.Draw(pictureBox14); Harris = new Harris(GrayImg, HarrisRadius, HarrisDolya); Harris.ImageWithPoints.Draw(pictureBox13); Img DX = new Img(Harris.MinL, IWidth, IHeight); Img DY = new Img(Harris.MaxL, IWidth, IHeight); DX.Draw(pictureBox15); DY.Draw(pictureBox16); Harris.ImageWithPoints.Save(BasePath + "Lab 3/Harris.png"); label15.Text = "Оператор Харриса (Точек: " + Harris.NPoints + ")"; Harris.ImageWithPoints.Draw(pictureBox18); label20.Text = "Точек: " + Harris.NPoints; label19.Text = "Точек: 0"; Harris.ImageWithPoints.Draw(pictureBox32); Harris.ImageWithPoints.Draw(pictureBox34); Harris.ImageWithPoints.Draw(pictureBox36); Harris.ImageWithPoints.Draw(pictureBox38); Harris.ImageWithPoints.Draw(pictureBox40); Bitmap bmp = new Bitmap(IWidth, IHeight, PixelFormat.Format32bppArgb); pictureBox17.Image = bmp; pictureBox31.Image = bmp; pictureBox33.Image = bmp; pictureBox35.Image = bmp; pictureBox37.Image = bmp; pictureBox39.Image = bmp; button7.Enabled = true; button9.Enabled = true; } catch { MessageBox.Show("Введите данные корректно!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("Введите данные!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/* Дескрипторы (Лаб 5)*/ private void button11_Click(object sender, EventArgs e) { if (textBox14.Text != "" && textBox13.Text != "" && textBox12.Text != "") { try { int HarrisRadius = Convert.ToInt32(textBox14.Text); double HarrisDolya = Convert.ToDouble(textBox13.Text); int Npoints = Convert.ToInt32(textBox12.Text); Img BegImg1 = new Img(GrayImg.GrayMatrix, IWidth, IHeight); /*-------------------------*/ int size = Convert.ToInt32(Math.Ceiling(Math.Sqrt(IWidth * IWidth + IHeight * IHeight))); Bitmap RotateBMP = new Bitmap(size, size, PixelFormat.Format32bppArgb); for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { RotateBMP.SetPixel(x, y, Color.White); } } int o = (size - IHeight) / 2; int oo = (size - IWidth) / 2; for (int y = o; y < IHeight + o; y++) { for (int x = oo; x < IWidth + oo; x++) { RotateBMP.SetPixel(x, y, GrayImg.Bitmap.GetPixel(x - oo, y - o)); } } RotateBMP = F.RotateImage(RotateBMP, 45.0F); Img BegImg2 = new Img(RotateBMP); /* * byte[,] SdvigMtx = F.Sdvig(TempImg.GrayMatrix, TempImg.Width, TempImg.Height, out int nWidth, out int nHeight, 0, -50); * TempImg = new Img(SdvigMtx, nWidth, nHeight); * * byte[,] ContrastMtx = F.Contrast(TempImg.GrayMatrix, TempImg.Width, TempImg.Height, -10); * Img BegImg2 = new Img(ContrastMtx, TempImg.Width, TempImg.Height); */ /*--------------------------*/ //Img BegImg2 = new Img(BasePath + "Temp/GrayImage.png"); BegImg1.Draw(pictureBox45); BegImg2.Draw(pictureBox43); Harris Harris1 = new Harris(BegImg1, HarrisRadius, HarrisDolya); Harris1.ANMS(Npoints); Harris Harris2 = new Harris(BegImg2, HarrisRadius, HarrisDolya); Harris2.ANMS(Npoints); Harris1.ImageWithANMS.Draw(pictureBox47); Harris2.ImageWithANMS.Draw(pictureBox46); Harris1.Descript5(); Harris2.Descript5(); int[] S = F.DescriptSootv5(Harris1, Harris2, 16 * 8, 0.8); int rst = 20; int h = Math.Max(Harris1.ImageWithANMS.Height, Harris2.ImageWithANMS.Height); Bitmap SootvBmp = new Bitmap(Harris1.ImageWithANMS.Width + Harris2.ImageWithANMS.Width + rst, h, PixelFormat.Format32bppArgb); for (int y = 0; y < Harris1.ImageWithANMS.Height; y++) { for (int x = 0; x < Harris1.ImageWithANMS.Width; x++) { SootvBmp.SetPixel(x, y, Harris1.ImageWithANMS.Bitmap.GetPixel(x, y)); } } for (int y = 0; y < h; y++) { for (int x = Harris1.ImageWithANMS.Width; x < Harris1.ImageWithANMS.Width + rst; x++) { SootvBmp.SetPixel(x, y, Color.White); } } for (int y = 0; y < Harris2.ImageWithANMS.Height; y++) { for (int x = Harris1.ImageWithANMS.Width + rst; x < Harris1.ImageWithANMS.Width + rst + Harris2.ImageWithANMS.Width; x++) { SootvBmp.SetPixel(x, y, Harris2.ImageWithANMS.Bitmap.GetPixel(x - (Harris1.ImageWithANMS.Width + rst), y)); } } Random rand = new Random(); Graphics g = Graphics.FromImage(SootvBmp); Pen pen; Point p1, p2; for (int i = 0; i < Harris1.NewPoints; i++) { if (S[i] != -1) { int ra = rand.Next(0, 101); if (ra >= 0 && ra < 20) { pen = new Pen(Brushes.Blue, 2); } else if (ra >= 20 && ra < 40) { pen = new Pen(Brushes.Green, 2); } else if (ra >= 40 && ra < 60) { pen = new Pen(Brushes.Aqua, 2); } else if (ra >= 60 && ra < 80) { pen = new Pen(Brushes.Yellow, 2); } else { pen = new Pen(Brushes.Violet, 2); } p1 = new Point(Harris1.IntPointsCoord[i, 1], Harris1.IntPointsCoord[i, 0]); p2 = new Point(Harris2.IntPointsCoord[S[i], 1] + (Harris1.ImageWithANMS.Width + rst), Harris2.IntPointsCoord[S[i], 0]); g.DrawLine(pen, p1, p2); } } pictureBox48.Image = SootvBmp; Harris1.ImageWithANMS.Save(BasePath + "Lab 5/HarrisANMS1.png"); Harris2.ImageWithANMS.Save(BasePath + "Lab 5/HarrisANMS2.png"); SootvBmp.Save(BasePath + "Lab 5/Result.png"); } catch { MessageBox.Show("Введите данные корректно!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("Введите данные!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/* Дескрипторы (Лаб 4)*/ private void button10_Click(object sender, EventArgs e) { if (textBox11.Text != "" && textBox10.Text != "" && textBox9.Text != "") { try { int HarrisRadius = Convert.ToInt32(textBox11.Text); double HarrisDolya = Convert.ToDouble(textBox10.Text); int Npoints = Convert.ToInt32(textBox9.Text); Img BegImg1 = new Img(GrayImg.GrayMatrix, IWidth, IHeight); /*-------------------------*/ int size = Convert.ToInt32(Math.Ceiling(Math.Sqrt(IWidth * IWidth + IHeight * IHeight))); Bitmap RotateBMP = new Bitmap(size, size, PixelFormat.Format32bppArgb); for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { RotateBMP.SetPixel(x, y, Color.White); } } int o = (size - IHeight) / 2; int oo = (size - IWidth) / 2; for (int y = o; y < IHeight + o; y++) { for (int x = oo; x < IWidth + oo; x++) { RotateBMP.SetPixel(x, y, GrayImg.Bitmap.GetPixel(x - oo, y - o)); } } RotateBMP = F.RotateImage(RotateBMP, 5.0F); Img TempImg = new Img(RotateBMP); byte[,] SdvigMtx = F.Sdvig(TempImg.GrayMatrix, TempImg.Width, TempImg.Height, out int nWidth, out int nHeight, 0, -100); TempImg = new Img(SdvigMtx, nWidth, nHeight); byte[,] ContrastMtx = F.Contrast(TempImg.GrayMatrix, TempImg.Width, TempImg.Height, -10); Img BegImg2 = new Img(ContrastMtx, TempImg.Width, TempImg.Height); /*--------------------------*/ BegImg1.Draw(pictureBox30); BegImg2.Draw(pictureBox29); Harris Harris1 = new Harris(BegImg1, HarrisRadius, HarrisDolya); Harris1.ANMS(Npoints); Harris Harris2 = new Harris(BegImg2, HarrisRadius, HarrisDolya); Harris2.ANMS(Npoints); Harris1.ImageWithANMS.Draw(pictureBox42); Harris2.ImageWithANMS.Draw(pictureBox41); Harris1.Descript4(); Harris2.Descript4(); int[] S = F.DescriptSootv4(Harris1, Harris2, 8 * 16, 0.8); int rst = 20; int h = Math.Max(Harris1.ImageWithANMS.Height, Harris2.ImageWithANMS.Height); Bitmap SootvBmp = new Bitmap(Harris1.ImageWithANMS.Width + Harris2.ImageWithANMS.Width + rst, h, PixelFormat.Format32bppArgb); for (int y = 0; y < Harris1.ImageWithANMS.Height; y++) { for (int x = 0; x < Harris1.ImageWithANMS.Width; x++) { SootvBmp.SetPixel(x, y, Harris1.ImageWithANMS.Bitmap.GetPixel(x, y)); } } for (int y = 0; y < h; y++) { for (int x = Harris1.ImageWithANMS.Width; x < Harris1.ImageWithANMS.Width + rst; x++) { SootvBmp.SetPixel(x, y, Color.White); } } for (int y = 0; y < Harris2.ImageWithANMS.Height; y++) { for (int x = Harris1.ImageWithANMS.Width + rst; x < Harris1.ImageWithANMS.Width + rst + Harris2.ImageWithANMS.Width; x++) { SootvBmp.SetPixel(x, y, Harris2.ImageWithANMS.Bitmap.GetPixel(x - (Harris1.ImageWithANMS.Width + rst), y)); } } Graphics g = Graphics.FromImage(SootvBmp); Pen pen = new Pen(Brushes.Blue, 2); Point p1, p2; for (int i = 0; i < Harris1.NewPoints; i++) { if (S[i] != -1) { p1 = new Point(Harris1.IntPointsCoord[i, 1], Harris1.IntPointsCoord[i, 0]); p2 = new Point(Harris2.IntPointsCoord[S[i], 1] + (Harris1.ImageWithANMS.Width + rst), Harris2.IntPointsCoord[S[i], 0]); g.DrawLine(pen, p1, p2); } } pictureBox44.Image = SootvBmp; Harris1.ImageWithANMS.Save(BasePath + "Lab 4/HarrisANMS1.png"); Harris2.ImageWithANMS.Save(BasePath + "Lab 4/HarrisANMS2.png"); SootvBmp.Save(BasePath + "Lab 4/Result.png"); } catch { MessageBox.Show("Введите данные корректно!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("Введите данные!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/* Повторяемость Харриса */ private void button9_Click(object sender, EventArgs e) { if (radioButton7.Checked || radioButton12.Checked) { /*Сдвиг*/ byte[,] SdvigMtx = F.Sdvig(GrayImg.GrayMatrix, GrayImg.Width, GrayImg.Height, out int nWidth, out int nHeight, 50, 20); Img SdvigImg = new Img(SdvigMtx, nWidth, nHeight); Harris SdvigHarris = new Harris(SdvigImg, HarrisRadius, HarrisDolya); SdvigHarris.ImageWithPoints.Draw(pictureBox31); } if (radioButton8.Checked || radioButton12.Checked) { /*Поворот*/ int size = Convert.ToInt32(Math.Ceiling(Math.Sqrt(IWidth * IWidth + IHeight * IHeight))); Bitmap RotateBMP = new Bitmap(size, size, PixelFormat.Format32bppArgb); for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { RotateBMP.SetPixel(x, y, Color.White); } } int o = (size - IHeight) / 2; int oo = (size - IWidth) / 2; for (int y = o; y < IHeight + o; y++) { for (int x = oo; x < IWidth + oo; x++) { RotateBMP.SetPixel(x, y, GrayImg.Bitmap.GetPixel(x - oo, y - o)); } } RotateBMP = F.RotateImage(RotateBMP, 30.0F); Img RotateImg = new Img(RotateBMP); Harris RotateHarris = new Harris(RotateImg, HarrisRadius, HarrisDolya); RotateHarris.ImageWithPoints.Draw(pictureBox33); } if (radioButton9.Checked || radioButton12.Checked) { /*Шум*/ double[,] NoiseMtx = F.Noise(GrayImg.GrayMatrix, IWidth, IHeight, 30); Img NoiseImg = new Img(NoiseMtx, IWidth, IHeight); Harris NoiseHarris = new Harris(NoiseImg, HarrisRadius, HarrisDolya); NoiseHarris.ImageWithPoints.Draw(pictureBox35); } if (radioButton10.Checked || radioButton12.Checked) { /*Контрастность*/ byte[,] ContrastMtx = F.Contrast(GrayImg.GrayMatrix, IWidth, IHeight, 35); Img ContrastImg = new Img(ContrastMtx, IWidth, IHeight); Harris ContrastHarris = new Harris(ContrastImg, HarrisRadius, HarrisDolya); ContrastHarris.ImageWithPoints.Draw(pictureBox37); } if (radioButton11.Checked || radioButton12.Checked) { /*Яркость*/ byte[,] BrightMtx = F.Brightness(GrayImg.GrayMatrix, IWidth, IHeight, 50); Img BrightImg = new Img(BrightMtx, IWidth, IHeight); Harris BrightHarris = new Harris(BrightImg, HarrisRadius, HarrisDolya); BrightHarris.ImageWithPoints.Draw(pictureBox39); } }
// Ищем соответствия 5 public int[] DescriptSootv5(Harris h1, Harris h2, int GistogramSize, double T) { int[] Res = new int[h1.NewPoints]; for (int d1 = 0; d1 < h1.NewPoints; d1++) { double L2min1 = 999999999; double L2min2 = 999999999; int desc1 = -1; int desc2 = -1; double[] L2D = new double[h2.NewPoints]; double[] L2D_2 = new double[h2.NewPoints]; double[] L2D_3 = new double[h2.NewPoints]; double[] L2D_4 = new double[h2.NewPoints]; for (int d2 = 0; d2 < h2.NewPoints; d2++) { double L2 = 0; for (int i = 0; i < GistogramSize; i++) { L2 += Math.Pow((h1.Descriptors[d1, i] - h2.Descriptors[d2, i]), 2); } L2D[d2] = Math.Sqrt(L2); if (h2.Descriptors2[d2, 0] != -1) { L2 = 0; for (int i = 0; i < GistogramSize; i++) { L2 += Math.Pow((h1.Descriptors[d1, i] - h2.Descriptors2[d2, i]), 2); } L2D_2[d2] = Math.Sqrt(L2); } else { L2D_2[d2] = -1; } if (h1.Descriptors2[d1, 0] != -1) { L2 = 0; for (int i = 0; i < GistogramSize; i++) { L2 += Math.Pow((h1.Descriptors2[d1, i] - h2.Descriptors[d2, i]), 2); } L2D_3[d2] = Math.Sqrt(L2); } else { L2D_3[d2] = -1; } if (h1.Descriptors2[d1, 0] != -1 && h2.Descriptors2[d2, 0] != -1) { L2 = 0; for (int i = 0; i < GistogramSize; i++) { L2 += Math.Pow((h1.Descriptors2[d1, i] - h2.Descriptors2[d2, i]), 2); } L2D_4[d2] = Math.Sqrt(L2); } else { L2D_4[d2] = -1; } } for (int d2 = 0; d2 < h2.NewPoints; d2++) { if (L2D[d2] < L2min1) { L2min1 = L2D[d2]; desc1 = d2; } } for (int d2 = 0; d2 < h2.NewPoints; d2++) { if (L2D_2[d2] != -1) { if (L2D_2[d2] < L2min1) { L2min1 = L2D_2[d2]; desc1 = d2; } } } for (int d2 = 0; d2 < h2.NewPoints; d2++) { if (L2D_3[d2] != -1) { if (L2D_3[d2] < L2min1) { L2min1 = L2D_3[d2]; desc1 = d2; } } } for (int d2 = 0; d2 < h2.NewPoints; d2++) { if (L2D_4[d2] != -1) { if (L2D_4[d2] < L2min1) { L2min1 = L2D_4[d2]; desc1 = d2; } } } for (int d2 = 0; d2 < h2.NewPoints; d2++) { if (desc1 == d2) { continue; } else if (L2D[d2] < L2min2) { L2min2 = L2D[d2]; desc2 = d2; } } for (int d2 = 0; d2 < h2.NewPoints; d2++) { if (desc1 == d2) { continue; } else if (L2D_2[d2] != -1) { if (L2D_2[d2] < L2min2) { L2min2 = L2D_2[d2]; desc2 = d2; } } } for (int d2 = 0; d2 < h2.NewPoints; d2++) { if (desc1 == d2) { continue; } else if (L2D_3[d2] != -1) { if (L2D_3[d2] < L2min2) { L2min2 = L2D_3[d2]; desc2 = d2; } } } for (int d2 = 0; d2 < h2.NewPoints; d2++) { if (desc1 == d2) { continue; } else if (L2D_4[d2] != -1) { if (L2D_4[d2] < L2min2) { L2min2 = L2D_4[d2]; desc2 = d2; } } } double NNDR = L2min1 / L2min2; if (NNDR > T) { Res[d1] = -1; } else { double L2min13 = 999999999; int desc13 = -1; double[] L2D3 = new double[h1.NewPoints]; double[] L2D3_2 = new double[h1.NewPoints]; double[] L2D3_3 = new double[h1.NewPoints]; double[] L2D3_4 = new double[h1.NewPoints]; for (int d3 = 0; d3 < h1.NewPoints; d3++) { double L23 = 0; for (int i = 0; i < GistogramSize; i++) { L23 += Math.Pow((h2.Descriptors[desc1, i] - h1.Descriptors[d3, i]), 2); } L2D3[d3] = Math.Sqrt(L23); if (h1.Descriptors2[d3, 0] != -1) { L23 = 0; for (int i = 0; i < GistogramSize; i++) { L23 += Math.Pow((h2.Descriptors[desc1, i] - h1.Descriptors2[d3, i]), 2); } L2D3_2[d3] = Math.Sqrt(L23); } else { L2D3_2[d3] = -1; } if (h2.Descriptors2[desc1, 0] != -1) { L23 = 0; for (int i = 0; i < GistogramSize; i++) { L23 += Math.Pow((h2.Descriptors2[desc1, i] - h1.Descriptors[d3, i]), 2); } L2D3_3[d3] = Math.Sqrt(L23); } else { L2D3_3[d3] = -1; } if (h2.Descriptors2[desc1, 0] != -1 && h1.Descriptors2[d3, 0] != -1) { L23 = 0; for (int i = 0; i < GistogramSize; i++) { L23 += Math.Pow((h2.Descriptors2[desc1, i] - h1.Descriptors2[d3, i]), 2); } L2D3_4[d3] = Math.Sqrt(L23); } else { L2D3_4[d3] = -1; } } for (int d3 = 0; d3 < h1.NewPoints; d3++) { if (L2D3[d3] < L2min13) { L2min13 = L2D3[d3]; desc13 = d3; } } for (int d3 = 0; d3 < h1.NewPoints; d3++) { if (L2D3_2[d3] != -1) { if (L2D3_2[d3] < L2min13) { L2min13 = L2D3_2[d3]; desc13 = d3; } } } for (int d3 = 0; d3 < h1.NewPoints; d3++) { if (L2D3_3[d3] != -1) { if (L2D3_3[d3] < L2min13) { L2min13 = L2D3_3[d3]; desc13 = d3; } } } for (int d3 = 0; d3 < h1.NewPoints; d3++) { if (L2D3_4[d3] != -1) { if (L2D3_4[d3] < L2min13) { L2min13 = L2D3_4[d3]; desc13 = d3; } } } if (desc13 == d1) { Res[d1] = desc1; } else { Res[d1] = -1; /* * L2min13 = 999999999; * desc13 = -1; * L2D3 = new double[h1.NewPoints]; * L2D3_2 = new double[h1.NewPoints]; * L2D3_3 = new double[h1.NewPoints]; * L2D3_4 = new double[h1.NewPoints]; * for (int d3 = 0; d3 < h1.NewPoints; d3++) * { * double L23 = 0; * for (int i = 0; i < GistogramSize; i++) * { * L23 += Math.Pow((h2.Descriptors[desc2, i] - h1.Descriptors[d3, i]), 2); * } * L2D3[d3] = Math.Sqrt(L23); * * if (h1.Descriptors2[d3, 0] != -1) * { * L23 = 0; * for (int i = 0; i < GistogramSize; i++) * { * L23 += Math.Pow((h2.Descriptors[desc2, i] - h1.Descriptors2[d3, i]), 2); * } * L2D3_2[d3] = Math.Sqrt(L23); * } * else * { * L2D3_2[d3] = -1; * } * * if (h2.Descriptors2[desc2, 0] != -1) * { * L23 = 0; * for (int i = 0; i < GistogramSize; i++) * { * L23 += Math.Pow((h2.Descriptors2[desc2, i] - h1.Descriptors[d3, i]), 2); * } * L2D3_3[d3] = Math.Sqrt(L23); * } * else * { * L2D3_3[d3] = -1; * } * * if (h2.Descriptors2[desc2, 0] != -1 && h1.Descriptors2[d3, 0] != -1) * { * L23 = 0; * for (int i = 0; i < GistogramSize; i++) * { * L23 += Math.Pow((h2.Descriptors2[desc2, i] - h1.Descriptors2[d3, i]), 2); * } * L2D3_4[d3] = Math.Sqrt(L23); * } * else * { * L2D3_4[d3] = -1; * } * } * for (int d3 = 0; d3 < h1.NewPoints; d3++) * { * if (L2D3[d3] < L2min13) * { * L2min13 = L2D3[d3]; * desc13 = d3; * } * } * for (int d3 = 0; d3 < h1.NewPoints; d3++) * { * if (L2D3_2[d3] != -1) * { * if (L2D3_2[d3] < L2min13) * { * L2min13 = L2D3_2[d3]; * desc13 = d3; * } * } * * } * for (int d3 = 0; d3 < h1.NewPoints; d3++) * { * if (L2D3_3[d3] != -1) * { * if (L2D3_3[d3] < L2min13) * { * L2min13 = L2D3_3[d3]; * desc13 = d3; * } * } * * } * for (int d3 = 0; d3 < h1.NewPoints; d3++) * { * if (L2D3_4[d3] != -1) * { * if (L2D3_4[d3] < L2min13) * { * L2min13 = L2D3_4[d3]; * desc13 = d3; * } * } * * } * if (desc13 == d1) * { * Res[d1] = desc2; * } * else * { * Res[d1] = -1; * } */ } } } return(Res); }