Hough circle doesn't detect eyes iris

2.1k Views Asked by At

I want to detect eyes irises and their centers using Hough Circle algorithm.

I'm using this code:

 private void houghCircle()
    {
        Bitmap obtainedBitmap = imagesList.getFirst();
                 /* convert bitmap to mat */
        Mat mat = new Mat(obtainedBitmap.getWidth(),obtainedBitmap.getHeight(),
                CvType.CV_8UC1);
        Mat grayMat = new Mat(obtainedBitmap.getWidth(), obtainedBitmap.getHeight(),
                CvType.CV_8UC1);


        Utils.bitmapToMat(obtainedBitmap, mat);

/* convert to grayscale */
        int colorChannels = (mat.channels() == 3) ? Imgproc.COLOR_BGR2GRAY : ((mat.channels() == 4) ? Imgproc.COLOR_BGRA2GRAY : 1);

        Imgproc.cvtColor(mat, grayMat, colorChannels);

/* reduce the noise so we avoid false circle detection */
        Imgproc.GaussianBlur(grayMat, grayMat, new Size(9, 9), 2, 2);

// accumulator value
        double dp = 1.2d;
// minimum distance between the center coordinates of detected circles in pixels
        double minDist = 100;

// min and max radii (set these values as you desire)
        int minRadius = 0, maxRadius = 1000;

// param1 = gradient value used to handle edge detection
// param2 = Accumulator threshold value for the
// cv2.CV_HOUGH_GRADIENT method.
// The smaller the threshold is, the more circles will be
// detected (including false circles).
// The larger the threshold is, the more circles will
// potentially be returned.
        double param1 = 70, param2 = 72;

/* create a Mat object to store the circles detected */
        Mat circles = new Mat(obtainedBitmap.getWidth(), obtainedBitmap.getHeight(), CvType.CV_8UC1);

/* find the circle in the image */
        Imgproc.HoughCircles(grayMat, circles, Imgproc.CV_HOUGH_GRADIENT, dp, minDist, param1, param2, minRadius, maxRadius);

/* get the number of circles detected */
        int numberOfCircles = (circles.rows() == 0) ? 0 : circles.cols();

/* draw the circles found on the image */
        for (int i=0; i<numberOfCircles; i++) {


/* get the circle details, circleCoordinates[0, 1, 2] = (x,y,r)
 * (x,y) are the coordinates of the circle's center
 */
            double[] circleCoordinates = circles.get(0, i);


            int x = (int) circleCoordinates[0], y = (int) circleCoordinates[1];

            Point center = new Point(x, y);

            int radius = (int) circleCoordinates[2];

    /* circle's outline */
            Core.circle(mat, center, radius, new Scalar(0,
                    255, 0), 4);

    /* circle's center outline */
            Core.rectangle(mat, new Point(x - 5, y - 5),
                    new Point(x + 5, y + 5),
                    new Scalar(0, 128, 255), -1);
        }

/* convert back to bitmap */
        Utils.matToBitmap(mat, obtainedBitmap);
        MediaStore.Images.Media.insertImage(getContentResolver(),obtainedBitmap, "testgray", "gray" );

    }

But it doesn't detect iris in all images correctly. Specially, if the iris has a dark color like brown. How can I fix this code to detect the irises and their centers correctly?

EDIT: Here are some sample images (which I got from the web) that shows the performance of the algorithm (Please ignore the landmarks which are represented by the red squares):

In these images the algorithm doesn't detect all irises:

enter image description here

enter image description here

This image shows how the algorithm couldn't detect irises at all:

enter image description here

EDIT 2: Here is a code which uses Canny edge detection, but it causes the app to crash:

 private void houghCircle()
    {
        Mat grayMat = new Mat();
        Mat cannyEdges = new Mat();
        Mat circles = new Mat();
        Bitmap obtainedBitmap = imagesList.getFirst();
         /* convert bitmap to mat */
        Mat originalBitmap = new Mat(obtainedBitmap.getWidth(),obtainedBitmap.getHeight(),
                CvType.CV_8UC1);
//Converting the image to grayscale
        Imgproc.cvtColor(originalBitmap,grayMat,Imgproc.COLOR_BGR2GRAY);
        Imgproc.Canny(grayMat, cannyEdges,10, 100);
        Imgproc.HoughCircles(cannyEdges, circles,
                Imgproc.CV_HOUGH_GRADIENT,1, cannyEdges.rows() / 15); //now circles is filled with detected circles.

//, grayMat.rows() / 8);
        Mat houghCircles = new Mat();
        houghCircles.create(cannyEdges.rows(),cannyEdges.cols()
                ,CvType.CV_8UC1);
//Drawing lines on the image
        for(int i = 0 ; i < circles.cols() ; i++)
        {
            double[] parameters = circles.get(0,i);
            double x, y;
            int r;
            x = parameters[0];
            y = parameters[1];
            r = (int)parameters[2];
            Point center = new Point(x, y);
//Drawing circles on an image
            Core.circle(houghCircles,center,r,
                    new Scalar(255,0,0),1);
        }
//Converting Mat back to Bitmap
        Utils.matToBitmap(houghCircles, obtainedBitmap);
        MediaStore.Images.Media.insertImage(getContentResolver(),obtainedBitmap, "testgray", "gray" );

    }

This is the error I get in the log

FATAL EXCEPTION: Thread-28685
    CvException [org.opencv.core.CvException: cv::Exception: /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/imgproc/src/color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int)
    ]
            at org.opencv.imgproc.Imgproc.cvtColor_1(Native Method)
            at org.opencv.imgproc.Imgproc.cvtColor(Imgproc.java:4598)

Which is caused by this line: Imgproc.cvtColor(originalBitmap,grayMat,Imgproc.COLOR_BGR2GRAY);

Can anyone please tell me how this error can solved? Perhaps adding a canny edge detection will improve the results.

3

There are 3 best solutions below

2
On

As you want to detect iris using hough transform (there are others), you had better studying the Canny edge detector and its parameters. cv::HoughCircles takes the Canny-hysteresis threshold in param1. Investigating Canny alone, you get the impression of good threshold range.

Maybe instead of gaussian blur, you apply a better denoising (non local means with say h=32 and window sizes 5 and 15), and also try to harmonize the image contrast, e.g., using contrast limited adaptive histogram equalization (cv::CLAHE).

Harmonization is to make sure all (highlight and shadow) eyes map to similar intensity range.

2
On

I wanted to know if those images are the images you processed or if you like took a cell phone snapshot of your screen to upload them here. Because the irises are bigger than the maximum radius you set in your code. Therefor I don't understand how you could find any iris at all. The irises in the first image have a radius of over 20. So you shouldn't be able to detect them. You should set the radii to the radius range you expect your irises to be.

0
On

Hough circles work better on well defined circles. They are not good with things like iris.

After some thresholding, morphological operations or canny edge detection, feature detection methods like MSER work much better for iris detection.

Here is a similar question with a solution if you are looking for some code.