//draws circle given by iris private void DrawCircle(Iris iris, Bitmap img) { HashSet <Coord> circle = new HashSet <Coord>(); double fi = 0; double step = 2 * Math.PI / 10000; while (fi < 2 * Math.PI) { var coord = new Coord(); coord.x = (int)(iris.x + iris.r * Math.Cos(fi)); coord.y = (int)(iris.y + iris.r * Math.Sin(fi)); if ((coord.x < 0) || (coord.y < 0) || (coord.x >= img.Height) || (coord.y >= img.Width)) { continue; } circle.Add(coord); fi += step; } var circleList = circle.ToList(); foreach (var point in circleList) { Color c = img.GetPixel(point.y, point.x); img.SetPixel(point.y, point.x, Color.FromArgb(c.A, 0, 0, 255)); } pictureBox2.Image = img; }
//Daugman algorithm //edgeV, edgeH - vertical and horizontal edges where we assume center point cannot be located //if isiris == false, we are searching for pupil //returns object iris which was found private Iris Daugman(Bitmap img, int edgeV, int edgeH, bool isiris) { int rstart, rchange, rfin; if (isiris) { rstart = 40; rchange = 5; rfin = img.Height / 2; } else { rstart = 7; rchange = 2; rfin = edgeH; } var iris = new Iris(); iris.diff = 0; for (int i = edgeH; i < img.Width - edgeH; i++) { for (int j = edgeV; j < img.Height - edgeV; j++) { var list = new List <double>(); for (int r = rstart; r < rfin; r += rchange) { double sum = GetSum(j, i, r, img); if (sum == 0) { break; } list.Add(sum); } int rmax = 0; double summax = 0; for (int r = 1; r < list.Count - 1; r++) { if ((list.ElementAt(r) - list.ElementAt(r - 1)) > summax) { summax = list.ElementAt(r) - list.ElementAt(r - 1); rmax = (r - 1) * rchange + rstart; } } if (summax > iris.diff) { iris.x = j; //by height iris.y = i; //by width iris.r = rmax; iris.diff = summax; Trace.WriteLine("new max sum found, params updated: x= " + j.ToString() + " y= " + i.ToString() + " r= " + rmax.ToString() + " summax=" + summax.ToString()); } list.Clear(); } } return(iris); }
//image preprocessing, starts Daugman algorithm for iris and pupil private void Iris_Click(object sender, EventArgs e) { try { Bitmap b = new Bitmap(pictureBox1.Image); Bitmap img = HistStretch(b); int[,] Gauss = new int[3, 3] { { 1, 2, 1 }, { 2, 4, 2 }, { 1, 2, 1 } }; ApplyMatrix(Gauss, 16); int edgeV = img.Height / 3; int edgeH = img.Width / 3; Iris iris = Daugman(img, edgeV, edgeH, true); DrawCircle(iris, img); Bitmap bnew = new Bitmap(pictureBox2.Image); Trace.WriteLine("iris found: x= " + iris.x.ToString() + " y= " + iris.y.ToString() + " r= " + iris.r.ToString()); edgeV = iris.y - iris.r; edgeH = iris.x - iris.r; Bitmap bnew1 = Threshold(new Bitmap(bnew), 50); Iris pupil = Daugman(bnew1, edgeV, edgeH, false); DrawCircle(pupil, bnew); Trace.WriteLine("pupil found: x= " + pupil.x.ToString() + " y= " + pupil.y.ToString() + " r= " + pupil.r.ToString()); Trace.WriteLine("done"); using (var db = new ImageContext()) { if (db.MyImages.Find(label1.Text) == null) { var newimg = new MyImage { IrisX = iris.x, IrisY = iris.y, IrisR = iris.r, PupilX = pupil.x, PupilY = pupil.y, PupilR = pupil.r, Name = label1.Text }; newimg.SavePicture(pictureBox1.Image); db.MyImages.Add(newimg); db.SaveChanges(); } } } catch (NullReferenceException) { MessageBox.Show("No initial file was chosen"); } }
//draw circle click private void goToolStripMenuItem_Click(object sender, EventArgs e) { try { var iris = new Iris(); iris.x = Int32.Parse(toolStripTextBox1.Text); iris.y = Int32.Parse(toolStripTextBox2.Text); iris.r = Int32.Parse(toolStripTextBox3.Text); DrawCircle(iris, new Bitmap(pictureBox1.Image)); } catch (System.FormatException) { MessageBox.Show("FormatException"); } catch (NullReferenceException) { MessageBox.Show("No initial file was chosen"); } }
public void DisplayFromDb(string name) { using (var db = new ImageContext()) { var img = db.MyImages.Find(name); pictureBox1.Image = img.GetPicture(); label1.Text = img.Name; Iris iris = new Iris(); iris.x = img.IrisX; iris.y = img.IrisY; iris.r = img.IrisR; DrawCircle(iris, new Bitmap(pictureBox1.Image)); Iris pupil = new Iris(); pupil.x = img.PupilX; pupil.y = img.PupilY; pupil.r = img.PupilR; DrawCircle(pupil, new Bitmap(pictureBox2.Image)); } }