Ask Your Question

Iris normalization and segmentation in opencv?

asked 2015-05-21 03:54:38 -0600

valentine gravatar image

updated 2020-09-17 05:51:43 -0600

Hello All,

I am currently working on multibiometrics based on Face and Iris. However, i am having issues with Iris segmentation and normalization with opencv c++. Please can anyone throw more light on this. Thanks in advance.

edit retag flag offensive close merge delete



what have you tried so far ?

berak gravatar imageberak ( 2015-05-21 04:20:08 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-05-21 06:38:23 -0600

valentine gravatar image

updated 2015-05-21 07:26:18 -0600

Hi Berek,

In iris recognition, you locate the pupil and then the Iris using canny edge and Hough transform.

First we calculte the pupil centriods using concept of contours:

static Mat getpupil(Mat& frame,Point& centroids){
Mat pupilImg = Mat(frame.size(), CV_8UC1);
Mat grayImg = Mat(frame.size(), CV_8UC1);
Mat tframe = frame;
Mat resImg;
inRange(frame, Scalar(28,28,28), Scalar(75,75,75), pupilImg);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
RNG rng(12345);
findContours(pupilImg, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
pupilImg = frame.clone();
for (int i = 0; i < contours.size(); i++){
    Moments mom = moments(contours[i]);
    double area = mom.m00;
    if (area > 50){
        double pupilArea = area;
        double x = mom.m10/area;
        double y = mom.m01/area;
        vector<vector<Point>> pupils = contours;
        centroids = Point((int)x, (int)y);
        Scalar color = Scalar(0, 0, 0);
        drawContours(pupilImg, pupils,hierarchy[i][3], CV_RGB(0,0,0),CV_FILLED,8,hierarchy);
return pupilImg;


Then we use Hough transform to find the Iris

static Mat getIris(Mat& frame,Point& centroid,int& rad){
Mat copyImg = frame.clone();
Mat Cf;
Mat resImg = frame.clone();
Mat grayImg = Mat(frame.size(), CV_8UC1);
Mat mask = Mat(frame.size(),CV_8UC1);
Mat Storage = Mat(frame.cols, 1, CV_32FC3);
int x1, y1;
Canny(frame, grayImg, 5, 70, 3);
GaussianBlur(grayImg, grayImg, Size(7, 7), 0);
vector<Vec3f> circles = getCircles(grayImg);

for (int i = 0; i < circles.size(); i++){
    rad = cvRound(circles[i][2]);
    x1 = cvRound(circles[i][0]);
    y1 = cvRound(circles[i][1]);
    circle(mask, centroid, rad, CV_RGB(255, 255, 255), CV_FILLED);
    //circle(mask, Point(x1,y1), rad, CV_RGB(255, 255, 255), CV_FILLED);
    bitwise_not(mask, mask);
    int x = centroid.x - rad;
    int y = centroid.y - rad;
    int w = rad * 2;
    int h = w;
           34    resImg = resImg(Rect(x,y,w,h));
    Mat cropImg = Mat(Size(w,h), CV_8UC1);
    resImg = resImg(Rect(0, 0, resImg.cols, resImg.rows));
    return cropImg;
return resImg;


Then i get error in line 34 above. OpencvError: Assertion failed (0 <=roi.x && 0 <= roi.width && ro.x + roi.width < m.cols && 0<=roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat

edit flag offensive delete link more


i can't see, where your centroid is initialized, but it complains, that the Rect is partly outside the image.

besides that, re-assigning a ROI to the original Mat might be a bad idea in a loop. maybe:

  Mat cropImg = Mat(Size(w,h), CV_8UC1);
  resImg(Rect(x,y,w,h)).copyTo(cropImg); // leave resImg intact !


  resImg = resImg(Rect(0, 0, resImg.cols, resImg.rows));

lt looks, like you wanted to restore the original size here, but it won't work, since you re-assigned resImg in line 34, again, - don't do that. avoid those re-assignments, and allow for a few more copies to be safe.

berak gravatar imageberak ( 2015-05-21 08:00:09 -0600 )edit

Hi Berak,

Thanks for contribution, i am really grateful. However i still got the same error after some few steps. My centroids were computed in first method using contours (getPupil function). Please will really appreciate your response.

valentine gravatar imagevalentine ( 2015-05-21 09:36:57 -0600 )edit

Question Tools

1 follower


Asked: 2015-05-21 03:54:38 -0600

Seen: 1,589 times

Last updated: May 21 '15