Click here to Skip to main content
15,867,686 members
Articles / General Programming / Algorithms

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

Rate me:
Please Sign up or sign in to vote.
4.83/5 (34 votes)
27 Dec 2010CPOL1 min read 130.5K   8.2K   84   28
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.

eyeaaaaaaaaaaaa1.jpg - Click to enlarge image

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:

Image 2

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

Then make it invert:

Image 3

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

Now we make it grayscale:

Image 4

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

Now we make it binary using threshold 220:

Image 5

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:

Image 6

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student University of Memphis
United States United States
Kishor Datta Gupta

Research assistant of University of Memphis

http://kishordgupta.com/

Comments and Discussions

 
Questionproject Pin
Member 1373425030-Mar-18 0:00
Member 1373425030-Mar-18 0:00 
Questionto detect the eyes in android?? Pin
Member 114912183-Jun-15 21:05
Member 114912183-Jun-15 21:05 
GeneralSource code project Pin
Member 808473614-Sep-14 18:04
Member 808473614-Sep-14 18:04 
Questionmore question Pin
uyungfauziyah16-Apr-14 15:52
uyungfauziyah16-Apr-14 15:52 
QuestionType or namespace name not available Pin
laiza ramal14-Sep-13 1:26
laiza ramal14-Sep-13 1:26 
GeneralMy vote of 5 Pin
jfriedman9-Jan-13 12:09
jfriedman9-Jan-13 12:09 
QuestionGood article Pin
Nycdude7775-Jan-13 8:19
Nycdude7775-Jan-13 8:19 
QuestionVote 4 Pin
chayangkul4-Mar-12 1:10
chayangkul4-Mar-12 1:10 
Generalquestion Pin
bessso22-May-11 0:00
bessso22-May-11 0:00 
GeneralMy vote of 5 Pin
sajib522-Jan-11 6:30
sajib522-Jan-11 6:30 
GeneralSome Questions Pin
Diamonddrake28-Dec-10 3:34
Diamonddrake28-Dec-10 3:34 
GeneralRe: Some Questions Pin
kdgupta8728-Dec-10 4:07
kdgupta8728-Dec-10 4:07 
GeneralMy vote of 4 Pin
Hiren solanki28-Dec-10 1:18
Hiren solanki28-Dec-10 1:18 
GeneralMy vote of 4 Pin
Libin Jose Chemperi27-Dec-10 22:26
Libin Jose Chemperi27-Dec-10 22:26 
Good Article..
GeneralMy vote of 4 Pin
RaviRanjanKr27-Dec-10 17:53
professionalRaviRanjanKr27-Dec-10 17:53 
GeneralMy vote of 2 Pin
pt140124-Dec-10 6:53
pt140124-Dec-10 6:53 
GeneralRe: My vote of 2 Pin
kdgupta8724-Dec-10 8:35
kdgupta8724-Dec-10 8:35 
GeneralRe: My vote of 2 Pin
RedDk27-Dec-10 6:23
RedDk27-Dec-10 6:23 
GeneralRe: My vote of 2 Pin
kdgupta8727-Dec-10 8:20
kdgupta8727-Dec-10 8:20 
General5 out of 5 (nice article) Pin
Bikash Karmokar23-Dec-10 9:24
Bikash Karmokar23-Dec-10 9:24 
GeneralMy vote of 5 Pin
muminbahar22-Dec-10 20:41
muminbahar22-Dec-10 20:41 
GeneralMy vote of 4 Pin
Tony Richards22-Dec-10 11:39
Tony Richards22-Dec-10 11:39 
GeneralRe: My vote of 4 Pin
kdgupta8722-Dec-10 13:50
kdgupta8722-Dec-10 13:50 
GeneralMy vote of 4 Pin
SledgeHammer0122-Dec-10 9:58
SledgeHammer0122-Dec-10 9:58 
QuestionThreshold value? Pin
Trollslayer22-Dec-10 5:03
mentorTrollslayer22-Dec-10 5:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.