/// <summary> /// Realiza leitura dos pontos de um arquivo texto. /// </summary> private void btnLerPontosArquivo_Click(object sender, EventArgs e) { // desabilita botão this.btnProximo.Enabled = false; // mostra caixa de diálogo if (this.dlgArquivoPontos.ShowDialog(this) == DialogResult.Cancel) { return; } try { // limpa pontos if (pointsList != null) { this.pointsList.Clear(); } else { this.pointsList = new List <PointCorrelation>(); } using (StreamReader sr = new StreamReader(this.dlgArquivoPontos.FileName)) { // leitura dos pontos for (int i = 0; i < 6; ++i) { string[] coords = sr.ReadLine().Trim().Split(new char[] { ',' }); // cria ponto PointCorrelation point = new PointCorrelation(int.Parse(coords[0]), int.Parse(coords[1]), this.fatorCorrelacao); // adiciona na lista this.pointsList.Add(point); } } // cópia do bitmap Bitmap drawBitmap = (Bitmap)this.selectedBitmap.Clone(); // desenha pontos DesenharPontos(drawBitmap); // atualiza PictureBox this.pctImagem.Image = drawBitmap; // habilita botão this.btnProximo.Enabled = true; } catch { MessageBox.Show(this, this.resourceMgr.GetString("MSG0013"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Desenha as linhas que formam os ângulos. /// </summary> /// <param name="bitmap">Bitmap onde serão desenhados os pontos.</param> private void DesenharLinhas(Bitmap bitmap, PontoDs pontoDs, float xmin, float ymin, float xmax, float ymax) { // pontos transformados para o sistema do range List <PointCorrelation> transfPoints = new List <PointCorrelation>(); foreach (PontoDs.PontoRow pontoRow in pontoDs.Ponto) { PointCorrelation point = new PointCorrelation(); point.X = pontoRow.XImagem - (int)xmin; point.Y = pontoRow.YImagem - (int)ymin; transfPoints.Add(point); } // tamanho da linha e texto float lineWidth = (2f * bitmap.Width) / (float)this.pctImagem.Width; float lineHeight = (10f * bitmap.Height) / (float)this.pctImagem.Height; float textSize = (10f * bitmap.Height) / (float)this.pctImagem.Height; // desenha pontos que foram identificados Graphics g = Graphics.FromImage(bitmap); // linhas g.DrawLine(new Pen(Brushes.Red, lineWidth), new Point(transfPoints[1].X, transfPoints[1].Y), new Point(transfPoints[2].X, transfPoints[2].Y)); g.DrawLine(new Pen(Brushes.Red, lineWidth), new Point(transfPoints[14].X, transfPoints[14].Y), new Point(transfPoints[15].X, transfPoints[15].Y)); g.DrawLine(new Pen(Brushes.Red, lineWidth), new Point(transfPoints[13].X, transfPoints[13].Y), new Point(transfPoints[12].X, transfPoints[12].Y)); g.DrawLine(new Pen(Brushes.Red, lineWidth), new Point(transfPoints[12].X, transfPoints[12].Y), new Point(transfPoints[11].X, transfPoints[11].Y)); g.DrawLine(new Pen(Brushes.Red, lineWidth), new Point(transfPoints[10].X, transfPoints[10].Y), new Point(transfPoints[9].X, transfPoints[9].Y)); g.DrawLine(new Pen(Brushes.Red, lineWidth), new Point(transfPoints[9].X, transfPoints[9].Y), new Point(transfPoints[8].X, transfPoints[8].Y)); g.DrawLine(new Pen(Brushes.Red, lineWidth), new Point(transfPoints[7].X, transfPoints[7].Y), new Point(transfPoints[6].X, transfPoints[6].Y)); g.DrawLine(new Pen(Brushes.Red, lineWidth), new Point(transfPoints[6].X, transfPoints[6].Y), new Point(transfPoints[5].X, transfPoints[5].Y)); g.DrawLine(new Pen(Brushes.Red, lineWidth), new Point(transfPoints[4].X, transfPoints[4].Y), new Point(transfPoints[3].X, transfPoints[3].Y)); g.DrawLine(new Pen(Brushes.Red, lineWidth), new Point(transfPoints[3].X, transfPoints[3].Y), new Point(transfPoints[0].X, transfPoints[0].Y)); // textos int count = 1; foreach (PointCorrelation p in transfPoints) { g.DrawString(count.ToString(), new Font("Sans Serif", textSize, FontStyle.Bold, GraphicsUnit.Pixel), Brushes.Black, new PointF(p.X, p.Y - lineHeight - lineWidth * 1.05f)); count++; } // libera memória g.Dispose(); }
/// <summary> /// Desenha os pontos na imagem. /// </summary> /// <param name="bitmap">Bitmap onde serão desenhados os pontos.</param> private void DesenharPontos(Bitmap bitmap, float xmin, float ymin, float xmax, float ymax) { // pontos transformados para o sistema do range List <PointCorrelation> transfPoints = new List <PointCorrelation>(); foreach (PointCorrelation p in this.pointsList) { PointCorrelation point = new PointCorrelation(); point.X = p.X - (int)xmin; point.Y = p.Y - (int)ymin; transfPoints.Add(point); } // tamanho da linha e texto float lineWidth = (1f * bitmap.Width) / (float)this.pctImagem.Width; float lineHeight = (10f * bitmap.Height) / (float)this.pctImagem.Height; float textSize = (10f * bitmap.Height) / (float)this.pctImagem.Height; // contador int count = 1; // desenha pontos que foram identificados Graphics g = Graphics.FromImage(bitmap); foreach (PointCorrelation p in transfPoints) { // linha horizontal g.DrawLine(new Pen(Brushes.Red, lineWidth), new Point(p.X - (int)lineHeight, p.Y), new Point(p.X + (int)lineHeight, p.Y)); // linha vertical g.DrawLine(new Pen(Brushes.Red, lineWidth), new Point(p.X, p.Y - (int)lineHeight), new Point(p.X, p.Y + (int)lineHeight)); // texto com o nome do ponto g.DrawString(count.ToString(), new Font("Sans Serif", textSize, FontStyle.Bold, GraphicsUnit.Pixel), Brushes.Black, new PointF(p.X, p.Y - lineHeight - lineWidth * 1.05f)); // incrementa contador de pontos count++; } // libera memória g.Dispose(); }
/// <summary> /// Realiza identificação dos pontos na imagem. /// </summary> private void btnIdentificarPontos_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; // desabilita botão this.btnProximo.Enabled = false; // bitmap a ser processado Bitmap drawBitmap = (Bitmap)this.selectedBitmap.Clone(); // dados do bitmap byte[, ,] bmpData; BitmapTools.GetBitmapData(drawBitmap, out bmpData); // saturação BitmapTools.Saturation(ref bmpData, 4.5); //Bitmap teste1 = (Bitmap)this.selectedBitmap.Clone(); //BitmapTools.SetBitmapData(teste1, ref bmpData); //teste1.Save("c:\\temp\\testeSat.jpg"); // limiarização BitmapTools.Thresholding(ref bmpData, PointType.Green, 0, 255); //Bitmap teste2 = (Bitmap)this.selectedBitmap.Clone(); //BitmapTools.SetBitmapData(teste2, ref bmpData); //teste2.Save("c:\\temp\\testeThreshold.jpg"); // redução de ruído BitmapTools.BlackAndWhiteNoiseReduction(ref bmpData, 127); // template matching this.pointsList = BitmapTools.FindImagePoints(ref bmpData, this.fatorCorrelacao); if (this.pointsList.Count != 6 && this.pointsList.Count != 12) { MessageBox.Show(this, this.resourceMgr.GetString("MSG0012"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } else { // habilita botão this.btnProximo.Enabled = true; // reordena pontos considerando a ordem de scan if (this.pointsList[0].X > this.pointsList[1].X) { PointCorrelation p = this.pointsList[0]; this.pointsList[0] = this.pointsList[1]; this.pointsList[1] = p; } if (this.pointsList[2].X > this.pointsList[3].X) { PointCorrelation p = this.pointsList[2]; this.pointsList[2] = this.pointsList[3]; this.pointsList[3] = p; } if (this.pointsList[4].X > this.pointsList[5].X) { PointCorrelation p = this.pointsList[4]; this.pointsList[4] = pointsList[5]; this.pointsList[5] = p; } if (this.pointsList.Count == 12) { // reordena pontos considerando a ordem de scan if (this.pointsList[6].X > this.pointsList[7].X) { PointCorrelation p = this.pointsList[6]; this.pointsList[6] = this.pointsList[7]; this.pointsList[7] = p; } if (this.pointsList[8].X > this.pointsList[9].X) { PointCorrelation p = this.pointsList[8]; this.pointsList[8] = this.pointsList[9]; this.pointsList[9] = p; } if (this.pointsList[10].X > this.pointsList[11].X) { PointCorrelation p = this.pointsList[10]; this.pointsList[10] = pointsList[11]; this.pointsList[11] = p; } } } // desenha pontos DesenharPontos(drawBitmap); // atualiza PictureBox this.pctImagem.Image = drawBitmap; Cursor.Current = Cursors.Default; }
/// <summary> /// Realiza identificação dos pontos na imagem. /// </summary> private void btnIdentificarPontos_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; // desabilita botão this.btnProximo.Enabled = false; // bitmap a ser processado Bitmap drawBitmap = (Bitmap)this.selectedBitmap.Clone(); // dados do bitmap byte[, ,] bmpData; BitmapTools.GetBitmapData(drawBitmap, out bmpData); // saturação BitmapTools.Saturation(ref bmpData, 4.5); //Bitmap teste1 = (Bitmap)this.selectedBitmap.Clone(); //BitmapTools.SetBitmapData(teste1, ref bmpData); //teste1.Save("c:\\temp\\ImagensTeste\\testeSat.jpg"); // limiarização BitmapTools.Thresholding(ref bmpData, PointType.Blue, 0, 255); //Bitmap teste2 = (Bitmap)this.selectedBitmap.Clone(); //BitmapTools.SetBitmapData(teste2, ref bmpData); //teste2.Save("c:\\temp\\ImagensTeste\\testeThreshold.jpg"); // redução de ruído BitmapTools.BlackAndWhiteNoiseReduction(ref bmpData, 127); //Bitmap teste3 = (Bitmap)this.selectedBitmap.Clone(); //BitmapTools.SetBitmapData(teste3, ref bmpData); //teste3.Save("c:\\temp\\ImagensTeste\\testeNoiseReduction.jpg"); // template matching this.pointsList = BitmapTools.FindImagePoints(ref bmpData, this.fatorCorrelacao); if (this.pointsList.Count != 16) { MessageBox.Show(this, this.resourceMgr.GetString("MSG0012"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); //StreamWriter sw = new StreamWriter(@"c:\\temp\\pontos.txt"); //foreach (PointCorrelation pt in this.pointsList) //{ // sw.WriteLine("{0},{1}", pt.X, pt.Y); //} //sw.Close(); } else { // troca pontos 15 e 16 (se necessário) if (this.pointsList[14].X < this.pointsList[15].X) { PointCorrelation p = this.pointsList[15]; this.pointsList[15] = this.pointsList[14]; this.pointsList[14] = p; } // troca pontos 2 e 3 (se necessário) if (this.pointsList[1].X < this.pointsList[2].X) { PointCorrelation p = this.pointsList[1]; this.pointsList[1] = this.pointsList[2]; this.pointsList[2] = p; } // habilita botão this.btnProximo.Enabled = true; } // desenha pontos DesenharPontos(drawBitmap); //drawBitmap.Save("c:\\temp\\ImagensTeste\\testeDrawPoints.jpg"); // atualiza PictureBox this.pctImagem.Image = drawBitmap; Cursor.Current = Cursors.Default; }