% segmentiris - peforms automatic segmentation of the iris region
% from an eye image. Also isolates noise areas such as occluding
% eyelids and eyelashes.
%
% Usage: 
% [circleiris, circlepupil, imagewithnoise] = segmentiris(image)
%
% Arguments:
%	eyeimage		- the input eye image
%	
% Output:
%	circleiris	    - centre coordinates and radius
%			          of the detected iris boundary
%	circlepupil	    - centre coordinates and radius
%			          of the detected pupil boundary
%	imagewithnoise	- original eye image, but with
%			          location of noise marked with
%			          NaN values
%
% Author: 
% Libor Masek
% masekl01@csse.uwa.edu.au
% School of Computer Science & Software Engineering
% The University of Western Australia
% November 2003

function [circleiris, circlepupil, imagewithnoise] = segmentiris(eyeimage)

% define range of pupil & iris radii

%CASIA
lpupilradius = 28;
upupilradius = 75;
lirisradius = 80;
uirisradius = 150;

%    %LIONS
%    lpupilradius = 32;
%    upupilradius = 85;
%    lirisradius = 145;
%    uirisradius = 169;


% define scaling factor to speed up Hough transform
scaling = 0.4;

reflecthres = 240;

% find the iris boundary by Daugman's intefrodifferential operaror

        [rowp, colp, rp] = SearchInnerBoundary(double(eyeimage));
       
        [row, col, r] = SearchOuterBoundary(double(eyeimage), rowp, colp, rp); 
        circleiris = [row, col, r];
        rowd = double(row);
        cold = double(col);
        rd = double(r);

        irl = round(rowd-rd);
        iru = round(rowd+rd);
        icl = round(cold-rd);
        icu = round(cold+rd);

        imgsize = size(eyeimage);

        if irl < 1 
            irl = 1;
        end

        if icl < 1
            icl = 1;
        end

        if iru > imgsize(1)
            iru = imgsize(1);
        end

        if icu > imgsize(2)
            icu = imgsize(2);
        end

        % to find the inner pupil, use just the region within the previously
        % detected iris boundary
        imagepupil = double(eyeimage( irl:iru,icl:icu));
        
        rowp = double(rowp)-irl;
        colp = double(colp)-icl;
        r = double(rp);
        
     

        row = double(irl) + rowp;
        col = double(icl) + colp;

        row = round(row);
        col = round(col);

        circlepupil = [row col r];      
    

% set up array for recording noise regions
% noise pixels will have NaN values
imagewithnoise = double(eyeimage);

%find top eyelid
topeyelid = uint8(imagepupil(1:(rowp-r),:));
lines = findline(topeyelid);

if size(lines,1) > 0
    [xl yl] = linecoords(lines, size(topeyelid));
    yl = double(yl) + irl-1;
    xl = double(xl) + icl-1;
    
    yla = max(yl);
    
    y2 = 1:yla;
    
    ind3 = sub2ind(size(eyeimage),yl,xl);
    imagewithnoise(uint32(ind3)) = NaN;   
    imagewithnoise(round(y2), round(xl)) = NaN;
end

%find bottom eyelid
bottomeyelid = uint8(imagepupil((rowp+r):size(imagepupil,1),:));
lines = findline(bottomeyelid);

if size(lines,1) > 0
    
    [xl yl] = linecoords(lines, size(bottomeyelid));
    yl = double(yl)+ irl+rowp+r-2;
    xl = double(xl) + icl-1;
    
    yla = min(yl);
    
    y2 = yla:size(eyeimage,1);
    
    ind4 = sub2ind(size(eyeimage),yl,xl);
    imagewithnoise(uint32(ind4)) = NaN;
    imagewithnoise(round(y2), round(xl)) = NaN;
    
end

% %For CASIA, eliminate eyelashes by thresholding
ref = eyeimage < 80;
coords = find(ref==1);
imagewithnoise(coords) = NaN;
