## Automatic pupil detection and extraction by c#

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

coding steps

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 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 croppedimage 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

Related
```

Graduate Research Assistant at University of Memphis Software Engineer at Silicon Orchard LTD. Former Research Assistant at Lamar University Former Software Engineer at Samsung R&D Institute Bangladesh Studies Ph.D. Computer Science at University of Memphis Studied Masters of Science in Computer Sciences at Lamar University Studied BSC in CSE at Khulna University of Engineering and Technology Studied HSC (completed) at Chittagang college 04-06 Studied High school at ST. Placid's High School'04 Studied Junior Secondary School at Saint Mary's School Lives in Memphis, Tennessee
This entry was posted in C#, Image Processing and tagged , , , , , , , , , . Bookmark the permalink.

### 11 Responses to Automatic pupil detection and extraction by c#

Wow! that is very simple but very effective. Thanks Kishordgupta for this blog. It really helped me a lot.

2. Carry on man. Hope one day this blog will be a great niche blog for c#

3. bikash says:

nice article ….i have done this after ur post
thanks

4. Wow… nice blog and nice works!!
Thanks Kishor for great efforts… best wishes 🙂

5. Jefferson Hohner says:

Hey I just want to let you know, I actually like the piece of writing on your website. But I am employing Flock on a machine running version 8.x of Ubuntu and the design aren’t quite right. Not a strong deal, I can still fundamentally read the articles and research for information, but just wanted to inform you about that. The navigation bar is kind of difficult to apply with the config I’m running. Keep up the superb work!