5

I'm trying to use OpenCV, more specifically its HoughCircles to detect and measure the pupil and iris, currently I've been playing with some of the variables in the function, because it either returns 0 circles, or an excessive amount. Below is the code and test image I'm using.

Code for measuring iris:

 eye1 = [self increaseIn:eye1 Contrast:2 andBrightness:0];

cv::cvtColor(eye1, eye1, CV_RGBA2RGB);
cv::bilateralFilter(eye1, eye2, 75, 100, 100);

cv::vector<cv::Vec3f> circles;

cv::cvtColor(eye2, eye1, CV_RGBA2GRAY);

cv::morphologyEx(eye1, eye1, 4, cv::getStructuringElement(cv::MORPH_RECT,cv::Size(3, 3)));
cv::threshold(eye1, eye1, 0, 255, cv::THRESH_OTSU);

eye1 = [self circleCutOut:eye1 Size:50];

cv::GaussianBlur(eye1, eye1, cv::Size(7, 7), 0);

cv::HoughCircles(eye1, circles, CV_HOUGH_GRADIENT, 2, eye1.rows/4);

Code for measuring pupil:

eye1 = [self increaseBlackPupil:eye1];
    cv::Mat eye2 = cv::Mat::zeros(eye1.rows, eye1.cols, CV_8UC3);

    eye1 = [self increaseIn:eye1 Contrast:2 andBrightness:0];

    cv::cvtColor(eye1, eye1, CV_RGBA2RGB);
    cv::bilateralFilter(eye1, eye2, 75, 100, 100);

    cv::threshold(eye2, eye1, 25, 255, CV_THRESH_BINARY);

    cv::SimpleBlobDetector::Params params;
    params.minDistBetweenBlobs = 75.0f;
    params.filterByInertia = false;
    params.filterByConvexity = false;
    params.filterByCircularity = false;
    params.filterByArea = true;
    params.minArea = 50;
    params.maxArea = 500;

    cv::Ptr<cv::FeatureDetector> blob_detector = new cv::SimpleBlobDetector(params);
    blob_detector->create("SimpleBlob");
    cv::vector<cv::KeyPoint> keypoints;
    blob_detector->detect(eye1, keypoints);

enter image description here

I know the image is rough, I've been also trying to find a way to clean it up and make the edges clearer.

So my question to put it plainly: What can I do to adjust the parameters in the function HoughCircles or changes to the images to make the iris and pupil detected?

Thanks

shim
  • 9,289
  • 12
  • 69
  • 108
G_Money
  • 318
  • 7
  • 22

1 Answers1

2

Ok, without experimenting too much, what I understand is that you've only applied a bilateral filter to the image before using the Hough circle detector.

In my opinion, you need to include a thresholding step into the process.

I took your sample image that you provided in the post and made it undergo the following steps:

  1. conversion to greyscale

  2. morphological gradient

  3. thresholding

  4. hough circle detection.

after the thresholding step, I got the following image for the left eye only:

thresholded image

here's the code:

greyscale:

   cvCvtColor(im_rgb,im_rgb,CV_RGB2GRAY);

morphology:

      cv::morphologyEx(im_rgb,im_rgb,4,cv::getStructuringElement(cv::MORPH_RECT,cv::Size(size,size)));

thresholding:

    cv::threshold(im_rgb, im_rgb, low, high, cv::THRESH_OTSU);

hough circle detection:

    cv::vector<cv::Vec3f> circles;
    cv::HoughCircles(im_rgb, circles, CV_HOUGH_GRADIENT, 2, im_rgb.rows/4); 

Now when I print:

    NSLog(@"Found %ld cirlces", circles.size());

I get:

     "Found 1 cirlces"

Hope this helps.

metsburg
  • 2,021
  • 1
  • 20
  • 32
  • 'im_rgb' is 'eye1' in your case. – metsburg Jul 27 '13 at 05:17
  • 1
    for the structuring element's 'size' in the morphology part, I used a static value of 5 for the size parameter. – metsburg Jul 27 '13 at 05:21
  • Well couple things with your code; First, you dont seem to use 'im_gray' once you make it, i figure your trying to use it as the input source for the morphology. Secondly, what are the values of the parameters 'low' and 'high'? – G_Money Jul 29 '13 at 16:40
  • Also im not aquainted with `cv::THRESH_OTSU` type of threshold, i read in the documentation that it is not supported yet it works in the code. Can you explain it for me please? or a link to where i can read about it – G_Money Jul 29 '13 at 19:05
  • 1
    low is 0, high is 255. read this for otsu thresholding: http://www.math.tau.ac.il/~turkel/notes/otsu.pdf (also see the wiki article on otsu). I used im_gray, sorry for bad naming convention, ignore that... I will edit the post. – metsburg Jul 30 '13 at 04:53
  • Ok well i changed up my code to match what you suggested and for the most part, it works pretty good detecting some of the iris, other times it gives a wrong circle. But the other thing which is kind of the more important one is that this way is not detecting the pupil at all. This method makes the insides of the iris (i.e the pupil) into scrambled eggs, any way to avoid this? or another route to take? – G_Money Jul 30 '13 at 15:06
  • I gave you a +1 for helping with the iris, but its not accepted since i haven't found the pupil. :) – G_Money Jul 30 '13 at 15:06
  • Going by this definition of pupil: http://webvision.med.utah.edu/imageswv/pupil.jpeg ....the pupil of the sample eye provided in the question doesn't look circular. Most likely, this condition came up due to low resolution of the camera. No wonder hough circle detection is thrown off track while trying to detect it. When it does actually detect '1 circle', it is detecting the circle of the edge between the iris and the sclera, and not the pupil. Perhaps, it is better to use some other strategy to detect the pupil. – metsburg Jul 31 '13 at 04:52
  • If you can position the pupil right in the centre of the image (perhaps using a crosshair etc.), you can detect the largest connected blob that lies within the iris (circle detected for iris) and approximate a circle around this blob. That might give you the pupil. You'll have to tinker around a bit, I guess. Cheers! – metsburg Jul 31 '13 at 04:56
  • Well the pupil is always off center, i would say within about a 25 pixel radius of the center. And how would you go about finding the largest connected blob? finding pixels that have near black values (say rgb > 50)? And/Or values close to each other until their is change in color (pixel representing the iris or glare/reflection)? – G_Money Jul 31 '13 at 17:59
  • would reducing the image size to reduce the extra noise help? in sense crop out more of the unwanted image? – G_Money Jul 31 '13 at 18:03
  • 1
    there is a specific algorithm for finding connected component, check this: http://en.wikipedia.org/wiki/Connected-component_labeling, it is very easy to code this from scratch. alternatively, you can use openCV as a workaround, see this example for how to use the flood fill algorithm for CCL in openCV: http://areshopencv.blogspot.in/2011/12/blob-detection-connected-component-pure.html – metsburg Aug 01 '13 at 04:55
  • 1
    The approach I suggest is: find the iris (hough circle) -> find the largest blob within the iris. – metsburg Aug 01 '13 at 05:06