15,505,667 members
Articles / General Programming / Algorithms
Article
Posted 22 Dec 2010

123.4K views
84 bookmarked

Pupil or eyeball detection and extraction from eye image using C#

Rate me:
Pupil extraction from eye image using C#.

Introduction

Using C# and the Aforge library, we can easily detect and extract the pupil from an eye image.

Algorithm

• Step 1 - First take the eye image.
• Step 2 - Make it invert.
• Step 3 - Convert it to gray scale.
• Step 4 - Use binary filter taking threshold value 220.
• Step 5 - Find the biggest object.
• Step 6 - Find that object's center point and height.
• Step 7 - Cut a circle from that point taking radius as 2.5 multiply of height.

Using the Code

The code is quite easy, using the Aforge library invert, grayscale, and threshold filters. First see the full code and then I will describe it.

C#
```System.Drawing.Bitmap aq = (Bitmap)pictureBox1.Image; //take the image
//////inverting the image
Invert a = new Invert();
aq=    a.Apply(aq);
AForge.Imaging.Image.FormatImage(ref aq);

/// apply grayscale
IFilter  filter =  Grayscale.CommonAlgorithms.BT709;
aq = filter.Apply(aq);

Threshold th = new Threshold(220);
aq = th.Apply(aq);

///find the biggest object
BlobCounter bl = new BlobCounter(aq);
int i = bl.ObjectsCount;
ExtractBiggestBlob fil2 = new ExtractBiggestBlob();

/// find the eye pupils start position and height

int x = 0;
int y = 0;
int h = 0;
if (i > 0)
{
fil2.Apply(aq);
x  = fil2.BlobPosition.X;
y = fil2.BlobPosition.Y;

h = fil2.Apply(aq).Height;
}

System.Drawing.Bitmap Bitmapsource = (Bitmap)pictureBox1.Image;
Rectangle section = new Rectangle(new Point(x -  h, y - h), new Size(3 * h, 3 *h));

Bitmap CroppedImage = CropImage(Bitmapsource, section);
pictureBox6.Image = CroppedImage;//get the eye pupil image```

First, take the eye image:

C#
`System.Drawing.Bitmap aq = (Bitmap)pictureBox1.Image;`

Then make it invert:

C#
```Invert a = new Invert();
aq=    a.Apply(aq);
AForge.Imaging.Image.FormatImage(ref aq);```

Now we make it grayscale:

C#
```IFilter  filter =  Grayscale.CommonAlgorithms.BT709;
aq = filter.Apply(aq);```

Now we make it binary using threshold 220:

C#
```Threshold th = new Threshold(220);
aq = th.Apply(aq);```

Now we have to find the biggest object in the binary image:

C#
```BlobCounter bl = new BlobCounter(aq);
int i = bl.ObjectsCount;
ExtractBiggestBlob fil2 = new ExtractBiggestBlob();
fil2.Apply(aq);```

Next we will find the start position and height of the biggest object/eye pupil:

C#
```int x = 0;
int y = 0;
int h = 0;
if (i > 0)
{
fil2.Apply(aq);
x  = fil2.BlobPosition.X;
y = fil2.BlobPosition.Y;

h = fil2.Apply(aq).Height;
}```

Now we cut the pupil part from the image:

To cut the image, we use the following code:

C#
```System.Drawing.Bitmap Bitmapsource = (Bitmap)pictureBox1.Image;
Rectangle section = new Rectangle(new Point(x -  h, y - h), new< Size(3 * h, 3 *h));
Bitmap CroppedImage = CropImage(Bitmapsource, section);```

The cropped image function code is:

C#
```public Bitmap CropImage(Bitmap source, Rectangle section)  {
Bitmap bmp = new Bitmap(section.Width, section.Height);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
return bmp;
}```

Hope that will help for any eyeball or pupil extraction code.

Points of Interest

For face recognition or detection, eye recognition can be helpful.

History

• 22nd December, 2010: Initial post.

Written By
Student University of Memphis
United States
Kishor Datta Gupta

Research assistant of University of Memphis

http://kishordgupta.com/

 First PrevNext
 project Member 1373425030-Mar-18 1:00 Member 13734250 30-Mar-18 1:00
 to detect the eyes in android?? Member 114912183-Jun-15 22:05 Member 11491218 3-Jun-15 22:05
 Source code project Member 808473614-Sep-14 19:04 Member 8084736 14-Sep-14 19:04
 more question uyungfauziyah16-Apr-14 16:52 uyungfauziyah 16-Apr-14 16:52
 Type or namespace name not available laiza ramal14-Sep-13 2:26 laiza ramal 14-Sep-13 2:26
 My vote of 5 jfriedman9-Jan-13 13:09 jfriedman 9-Jan-13 13:09
 Good article Nycdude7775-Jan-13 9:19 Nycdude777 5-Jan-13 9:19
 Vote 4 chayangkul4-Mar-12 2:10 chayangkul 4-Mar-12 2:10
 question bessso22-May-11 1:00 bessso 22-May-11 1:00
 My vote of 5 sajib522-Jan-11 7:30 sajib52 2-Jan-11 7:30
 Some Questions Diamonddrake28-Dec-10 4:34 Diamonddrake 28-Dec-10 4:34
 I have a passive application that I work on when I get bored that is a photo suite. My current implementation requires that you draw a circle around redeye to fix it. But if I could have the user just draw a box around the entire eye. That would be much easier for the user. This is an interesting way of doing it but it seems that this method depends on the pupil being the darkest part of the image... How dose this work on people of different skin colors? How well does it work on small images? or images where subject's eyes are small, say only a dozen pixels wide?
 Re: Some Questions kdgupta8728-Dec-10 5:07 kdgupta87 28-Dec-10 5:07
 My vote of 4 Hiren solanki28-Dec-10 2:18 Hiren solanki 28-Dec-10 2:18
 My vote of 4 Libin Jose Chemperi27-Dec-10 23:26 Libin Jose Chemperi 27-Dec-10 23:26
 My vote of 4 RaviRanjanKr27-Dec-10 18:53 RaviRanjanKr 27-Dec-10 18:53
 My vote of 2 pt140124-Dec-10 7:53 pt1401 24-Dec-10 7:53
 Re: My vote of 2 kdgupta8724-Dec-10 9:35 kdgupta87 24-Dec-10 9:35
 Re: My vote of 2 RedDk27-Dec-10 7:23 RedDk 27-Dec-10 7:23
 Re: My vote of 2 kdgupta8727-Dec-10 9:20 kdgupta87 27-Dec-10 9:20
 5 out of 5 (nice article) Bikash Karmokar23-Dec-10 10:24 Bikash Karmokar 23-Dec-10 10:24
 My vote of 5 muminbahar22-Dec-10 21:41 muminbahar 22-Dec-10 21:41
 My vote of 4 Tony Richards22-Dec-10 12:39 Tony Richards 22-Dec-10 12:39
 Re: My vote of 4 kdgupta8722-Dec-10 14:50 kdgupta87 22-Dec-10 14:50
 My vote of 4 SledgeHammer0122-Dec-10 10:58 SledgeHammer01 22-Dec-10 10:58
 Threshold value? Trollslayer22-Dec-10 6:03 Trollslayer 22-Dec-10 6:03
 Last Visit: 31-Dec-99 19:00     Last Update: 2-Dec-22 8:36 Refresh 12 Next ᐅ