Pupil or Eyeball Detection and Extraction by C# from eye image
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.
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
发表评论
r3fzbz It as not that I want to duplicate your web site, but I really like the layout. Could you let me know which theme are you using? Or was it tailor made?
jsAiiS make this website yourself or did you hire someone to do it for you?
GpUWRf I value the article.Much thanks again. Really Cool.
The issue is something which too few people are speaking intelligently about.
we like to honor numerous other web web-sites on the web, even if they aren
OqBfDF user in his/her brain that how a user can be aware of it.
4YFvFY I?ve recently started a blog, the information you offer on this web site has helped me tremendously. Thanks for all of your time & work.
Hiya! I simply wish to give a huge thumbs up for the good info you have here on this post. I can be coming back to your weblog for more soon.
Absolutely pent articles , regards for information .
TY for the great info! I would never have gotten this myself!
KkOTOB I truly appreciate this article post.Really looking forward to read more. Keep writing.
QaKdfD Thanks for another excellent article. Where else could anyone get that type of info in such an ideal way of writing? I have a presentation next week, and I am on the look for such information.
7jQZbF Wow, marvelous blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your web site is fantastic, as well as the content!. Thanks For Your article about &.
jrhgsh It as hard to find experienced people about this topic, however, you seem like you know what you are talking about! Thanks
JI2Abl Wonderful issues altogether, you simply won a new reader. What would you suggest in regards to your publish that you made a few days ago? Any certain?
TaCeKi There is definately a great deal to know about this topic. I like all of the points you made.
zMVlE4 Piece of writing writing is also a excitement, if you know afterward you can write if not it is complex to write.|
VB0Q5z Thanks a lot for the post.Really thank you! Cool.
gzGYIc My brother smiled and told me to see your post, and I want to thank you for putting up a very important information here. Thank you!
rggNNv Precisely what I was looking for, thanks for posting.
LE97G8 I think this is a real great blog article.Much thanks again. Will read on
gvLE7h It'аs actually a nice and useful piece of info. I am happy that you shared this useful information with us. Please keep us up to date like this. Thank you for sharing.
XTcCIB That is a good tip especially to those fresh to the blogosphere. Brief but very accurate information Many thanks for sharing this one. A must read article!
KDAx8X It cаА аЂаn bаА аЂа seeen and ju?ged only by watching the
XW5OpZ Just wanna comment that you have a very decent website , I enjoy the layout it really stands out.
NOYpnB This design is steller! You definitely know how to keep
pretty helpful stuff, overall I think this is well worth a bookmark, thanks
oL34Oo pretty handy material, overall I consider this is worthy of a bookmark, thanks
DSHivj There is obviously a lot to know about this. I assume you made various nice points in features also.
gXoCAC Thank you for producing the powerful, dependable, educational and as well as easy tips about your topic.
vWzbl7 I truly appreciate this post. I have been looking everywhere for this! Thank goodness I found it on Google. You have made my day! Thanks again
gDUmBY
B9n0WH Just a smiling visitant here to share the love (:, btw great style and design.
eAxf9X Some times its a pain in the ass to read what website owners wrote but this website is rattling user genial!.
j1g3eh You made some really good points there. I checked on the net for additional information about the issue and found most people will go along with your views on this website.
UweaT0 Thanks a lot for the blog article.Really looking forward to read more. Fantastic.
UGkuRy It is in reality a great and useful piece of info. I am satisfied that you shared this helpful tidbit with us. Please keep us informed like this. Thanks for sharing.
A7zdEb I think this is a real great blog post.Really looking forward to read more. Really Cool.
fxY16M Thanks a lot for the article post.Much thanks again.
ehFWh6 you are really a good webmaster. The site loading speed is amazing. It seems that you are doing any unique trick. Also, The contents are masterpiece. you've done a great job on this topic!
DG8UmO Hi there, I discovered your web site via Google at the same time as searching for a similar subject, your web site got here up, it appears to be like great. I've bookmarked it in my google bookmarks.
rPDx91 You made some decent points there. I looked on the internet for the subject matter and found most persons will consent with your site.
RWdSus great publish, very informative. I wonder why the other specialists of this sector don't realize this. You should continue your writing. I'm sure, you have a huge readers' base already!
9EmELp Hey, thanks for the blog.Really thank you! Will read on...
CaeCgR Major thankies for the article post.Thanks Again. Really Great.
urN7HT Really appreciate you sharing this blog post.Really looking forward to read more. Keep writing.
NyiRXz I cannot thank you enough for the article.Thanks Again. Awesome.
EQ6chV Very neat article.Much thanks again. Much obliged.
nyps4k Enjoyed every bit of your blog post. Keep writing.
7R6tQj Say, you got a nice article post.Much thanks again. Fantastic.