Instalador: http://easycomtec.com/magno/pi.rar
Código Fonte: http://easycomtec.com/magno/pi%20source.rar
Imagem: http://easycomtec.com/magno/image1.jpg
string filepath;
int limiteInferiorX = -1;
int limiteInferiorY = -1;
int limiteSuperiorX = -1;
int limiteSuperiorY = -1;
int pontoX1 = -1;
int pontoY1 = -1;
int pontoX2 = -1;
int pontoY2 = -1;
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
var imagemTratada = Pupila(new Bitmap(filepath));
var maiorDistancia = MaiorDistancia(imagemTratada);
var raio = maiorDistancia / 2;
var centroX = (pontoX2 + pontoX1) / 2;
var centroY = (pontoY2 + pontoY1) / 2;
label1.Content = String.Format("Raio:{0} Centro (X,Y):({1},{2})", raio, centroX, centroY);
var originalImage = new Bitmap(filepath);
for (int x = centroX - 2; x < centroX + 2; x++)
{
for (int y = centroY - 2; y < centroY + 2; y++)
{
imagemTratada.SetPixel(x, y, System.Drawing.Color.Red);
originalImage.SetPixel(x, y, System.Drawing.Color.Red);
}
}
image1.Source = ConvertImage(imagemTratada);
image2.Source = ConvertImage(originalImage);
}
private Bitmap Pupila(Bitmap image)
{
for (int x = 0; x < image.Width; x++)
{
for (int y = 0; y < image.Height; y++)
{
var color = image.GetPixel(x,y);
var value = (color.R + color.G + color.B) /3;
if (value < 50)
{
if (limiteInferiorX < 0)
{
limiteInferiorX = x;
limiteInferiorY = y;
}
if (limiteSuperiorX < x)
{
limiteSuperiorX = x;
}
if (limiteSuperiorY < y)
{
limiteSuperiorY = y;
}
image.SetPixel(x, y, System.Drawing.Color.Black);
}
else
{
image.SetPixel(x, y, System.Drawing.Color.White);
}
}
}
return image;
}
private double MaiorDistancia(Bitmap image)
{
double maiorValor = 0;
double aux;
for (int x1 = limiteInferiorX; x1 < limiteSuperiorX; x1++)
{
for (int y1 = limiteInferiorY; y1 < limiteSuperiorY; y1++)
{
if (image.GetPixel(x1, y1).R == 0)
{
for (int x2 = x1; x2 < limiteSuperiorX; x2++)
{
for (int y2 = y1; y2 < limiteSuperiorY; y2++)
{
if (image.GetPixel(x2, y2).R == 0)
{
aux = EuclidianaDistancia(x1, y1, x2, y2);
if (aux > maiorValor)
{
maiorValor = aux;
pontoX1 = x1;
pontoX2 = x2;
pontoY1 = y1;
pontoY2 = y2;
}
}
}
}
}
}
}
return maiorValor;
}
private double EuclidianaDistancia(int x1, int y1, int x2 , int y2)
{
return Math.Sqrt((Math.Pow((x1 - x2), 2) + Math.Pow((y1 - y2), 2)));
}
private System.Drawing.Color VerifyColor(int x, double factor,ref System.Drawing.Color color)
{
if (x % factor == 0)
{
if (color == System.Drawing.Color.Black)
{
color = System.Drawing.Color.White;
}
else
{
color = System.Drawing.Color.Black;
}
}
return color;
}
private ImageSource ConvertImage(Bitmap image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
System.Windows.Media.Imaging.BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage();
bImg.BeginInit();
bImg.StreamSource = new MemoryStream(ms.ToArray());
bImg.EndInit();
return bImg;
}
private void buttonLoad_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
filepath = openFileDialog1.FileName;
image1.Source = ConvertImage(new Bitmap(filepath));
}
}
Nenhum comentário:
Postar um comentário