Click here to Skip to main content
Click here to Skip to main content

Pupil or Eyeball Detection and Extraction by C# from eye image

By , 27 Dec 2010
 

Introduction

Using C# code and 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 objects 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 filters invert, grayscale and threshold filters. First see the full code and then I will describe it.

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:

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

Then make it invert:

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

Now we make it grayscale:

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

Now we make it binary using threshold 220:

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

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

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

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

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 have to cut the pupil part from the image so we can find the image:

To cut the image, we use the following code:

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:

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)

About the Author

kdgupta87
Software Developer Rymes
Bangladesh Bangladesh
Kishor is an undergraduate Computer Science and Engineering graduate from Khulna University Of Engineering and Technology. Currently working as a software developer in Rymes. Before joining Rymes Nascenia IT he worked as a software developer at Nascenia IT and as a trainee software developer at Technocrats BD, where he worked in .Net technologies. He developed few softwares in .Net framework. A few of the interesting ones were about face replacement, and GUI based requirement elicitation tool, image converter, image processing and LAN monitoring system.he worked on Ruby on Rails. Currently working on android
http://kishordgupta.wordpress.com/
Follow on   Twitter

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberjfriedman9-Jan-13 12:09 
QuestionGood articlememberNycdude7775-Jan-13 8:19 
QuestionVote 4memberchayangkul4-Mar-12 1:10 
Generalquestionmemberbessso22-May-11 0:00 
GeneralMy vote of 5membersajib522-Jan-11 6:30 
GeneralSome QuestionsmemberDiamonddrake28-Dec-10 3:34 
GeneralRe: Some Questionsmemberkdgupta8728-Dec-10 4:07 
GeneralMy vote of 4memberHiren Solanki28-Dec-10 1:18 
GeneralMy vote of 4memberLibin Jose Chemperi27-Dec-10 22:26 
GeneralMy vote of 4memberRaviRanjankr27-Dec-10 17:53 
GeneralMy vote of 2memberpt140124-Dec-10 6:53 
GeneralRe: My vote of 2memberkdgupta8724-Dec-10 8:35 
GeneralRe: My vote of 2memberRedDK27-Dec-10 6:23 
GeneralRe: My vote of 2memberkdgupta8727-Dec-10 8:20 
General5 out of 5 (nice article)memberBikash_coder23-Dec-10 9:24 
GeneralMy vote of 5membermuminbahar22-Dec-10 20:41 
GeneralMy vote of 4memberTony Richards22-Dec-10 11:39 
GeneralRe: My vote of 4memberkdgupta8722-Dec-10 13:50 
GeneralMy vote of 4memberSledgeHammer0122-Dec-10 9:58 
QuestionThreshold value?mentorTrollslayer22-Dec-10 5:03 
AnswerRe: Threshold value?memberkdgupta8722-Dec-10 8:17 
GeneralMy vote of 5memberprasad0222-Dec-10 4:23 
GeneralRe: My vote of 5memberMember 88723842-Oct-12 11:55 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130619.1 | Last Updated 27 Dec 2010
Article Copyright 2010 by kdgupta87
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid