-
Notifications
You must be signed in to change notification settings - Fork 10
/
detect_pupil_and_corneal_reflection.m
81 lines (69 loc) · 3.1 KB
/
detect_pupil_and_corneal_reflection.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
% Starburst Algorithm
%
% This source code is part of the starburst algorithm.
% Starburst algorithm is free; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% Starburst algorithm is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with cvEyeTracker; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
%
%
% Starburst Algorithm - Version 1.0.0
% Part of the openEyes ToolKit -- http://hcvl.hci.iastate.edu/openEyes
% Release Date:
% Authors : Dongheng Li <[email protected]>
% Derrick Parkhurst <[email protected]>
% Copyright (c) 2005
% All Rights Reserved.
function [pupil_ellipse, cr_circle] = detect_pupil_and_corneal_reflection(I, sx, sy, edge_thresh)
% This function detects pupil and corneal reflection in the eye image
%
% Input:
% I = input image
% [sx sy] = start point for starburst algorithm
% edge_thresh = threshold for pupil edge detection
%
% Output:
% pupil_ellipse = 5-vector of the ellipse parameters of pupil
% [a b cx cy theta]
% a - the ellipse axis of x direction
% b - the ellipse axis of y direction
% cx - the x coordinate of ellipse center
% cy - the y coordinate of ellipse center
% theta - the orientation of ellipse
% cr_circle = 3-vector of the circle parameters of the corneal reflection
% [crx cry crr]
% crx - the x coordinate of circle center
% cry - the y coordinate of circle center
% crr - the radius of circle
sigma = 2; % Standard deviation of image smoothing
angle_delta = 1*pi/180; % discretization step size (radians)
cr_window_size=301; % corneal reflection search window size (about [sx,sy] center)
min_feature_candidates=10; % minimum number of pupil feature candidates
max_ransac_iterations=10000; % maximum number of ransac iterations
rays=18; % number of rays to use to detect feature points
I = gaussian_smooth_image(I, sigma);
[crx, cry, crar] = locate_corneal_reflection(I, sx, sy, cr_window_size);
crr = fit_circle_radius_to_corneal_reflection(I, crx, cry, crar, angle_delta);
crr = ceil(crr*2.5);
I = remove_corneal_reflection(I, crx, cry, crr, angle_delta);
cr_circle = [crx cry crr];
[epx, epy] = starburst_pupil_contour_detection(I, sx, sy, edge_thresh, rays, min_feature_candidates);
if isempty(epx) || isempty(epy)
pupil_ellipse = [0 0 0 0 0]';
return;
end
[ellipse, inliers] = fit_ellipse_ransac(epx, epy, max_ransac_iterations);
[pupil_ellipse] = fit_ellipse_model(I, ellipse, angle_delta);
if ellipse(3) < 1 || ellipse(3) > size(I,2) || ellipse(4) < 1 || ellipse(4) > size(I,1)
fprintf(1, 'Error! The ellipse center lies out of the image\n');
pupil_ellipse = [0 0 0 0 0]';
end