65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 C# 从眼睛图像中检测和提取瞳孔或眼球

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.83/5 (33投票s)

2010年12月22日

CPOL

1分钟阅读

viewsIcon

136072

downloadIcon

8238

使用 C# 从眼部图像中提取瞳孔。

引言

使用 C# 和 Aforge 库,我们可以轻松地检测和提取眼部图像中的瞳孔。

算法

  • 步骤 1 - 首先获取眼部图像。
  • 步骤 2 - 进行反转。
  • 步骤 3 - 转换为灰度。
  • 步骤 4 - 使用二值化滤波器,阈值为 220。
  • 步骤 5 - 找到最大的对象。
  • 步骤 6 - 找到该对象的中点和高度。
  • 步骤 7 - 从该点切割一个圆,半径为高度的 2.5 倍。

eyeaaaaaaaaaaaa1.jpg - Click to enlarge image

Using the Code

代码非常简单,使用了 Aforge 库的反转、灰度化和二值化滤波器。首先查看完整代码,然后我将对其进行描述。

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

首先,获取眼部图像

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

然后进行反转

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

现在我们将其转换为灰度

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

现在我们使用阈值 220 进行二值化

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

现在我们需要在二值化图像中找到最大的对象

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

接下来我们将找到最大对象/瞳孔的起始位置和高度

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);

裁剪图像函数的代码是

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;
}

希望这能帮助任何眼球或瞳孔提取代码。

关注点

对于人脸识别或检测,眼部识别可能有所帮助。

历史

  • 2010年12月22日:初始发布。
© . All rights reserved.