MATLAB: How to robustly find the pupil on an image automatically

binaryimage segmentationMATLAB

I have images like this:
They are from mouse eye reocording. I want to develop a code that can automatically draw the pupil on each image. At frist, I binarize the images like this and use the binarized image to find the pupil:
binaryImage = ~imbinarize(img,0.05);
binaryImage=imfill(binaryImage,'holes');
binaryImage=bwmorph(binaryImage,'open');
However, the method is not robust to corneal reflection (the super bright spot on the image), whisker arosal (the bright line). And when the pupil is large as the image shown above, it can't detect the pupil accurately around the edge. Here's the binarized result of the above image
I think the binarization method is not superior as it doesn't consider a condition that the pupil is round. Is there a better algorithm to solve it?

Best Answer

  • Using the imfindcircles function and giving some additional informations by using the options could help - play with sensitivity option and maybe other options. The following code worked for your example picture:
    I = imread('image.png');
    [c, r] = imfindcircles(I,[20 40],'ObjectPolarity','dark','Sensitivity',0.9)
    imshow(I)
    viscircles(c, r,'EdgeColor','b');