# From Iris Image to Embedded Code: System of Methods
## 3 Eye Segmentation Methods
### Pupil Refinement  
_Circular shortest path_ method constructs a closed contour in a circular ring [30]. The ring is centered at a given point and has inner and outer radii. CSP method is a kind of optimal path method, i.e., it optimized the functional, which is the cost of the path. We take the ring concentric to the approximate pupil circle and spread 30% of its radius inside and outside. In order to ease calculations polar transformation is carried out. The ring shape in the source image is unwrapped to a rectilinear raster. Radial and angular coordinates of the ring are mapped to abscissa and ordinate. Thus, the problem of locating the circular shortest path is reduced to a problem of detecting the optimal path from the left to the right side of the rectangle such that terminal points of the path have the same ordinate. Contour is represented as a function \(\rho(\phi)\), \(\phi\in[0;2\pi]\), \(\rho(0)=\rho(2\pi)\) with limited derivative \(d\rho/d\phi<1\). In a discrete rectilinear raster of size \(W\times H\) the contour is turns to a sequence of points: \(\{(n,\rho_{n})\}\), \(n\in[0;W-1]\). Limitations to the derivative transforms to \(|\rho_{n+1}-\rho_{n}|\leqslant 1\), edge condition is set as \(|\rho_{W-1}-\rho_{0}|\leqslant 1\). Consider points \((n,\rho^{\prime})\) and \((n+1,\rho^{\prime\prime})\) from adjacent columns of the raster. Denote the cost of passing between them as  
\[C((n,\rho^{\prime}),(n+1,\rho^{\prime\prime}))\equiv C_{n}(\rho^{\prime},\rho ^{\prime\prime})=C_{n}^{(I)}(\rho^{\prime},\rho^{\prime\prime})+C_{n}^{(O)}( \rho^{\prime},\rho^{\prime\prime}). \tag{14}\]  
This cost is a sum of inner and outer parts. Inner cost is a function of contour shape, designed in a way to promote its smoothness:  
\[C_{n}^{(I)}(\rho^{\prime},\rho^{\prime\prime})= \begin{cases}0\,\ \rho^{\prime}= \rho^{\prime\prime}\,\\ \tau_{i}\,\ |\rho^{\prime}-\rho^{\prime\prime}|=1\ \,\\ \infty\,\ |\rho^{\prime}-\rho^{\prime\prime}|>1\ \.\end{cases}  \tag{15}\]  
Value of \(\tau_{i}>0\) is a parameter defining the magnitude of a \"force\", which pulls the contour towards a straight line. Optimizing the inner part alone would give horizontal lines in polar raster, i.e., ideal circles with the given center in source image. Figure 3: Four circular projections, their maxima positions and correct position of borders. Outer cost is designed to make the contour pass through border pixels. So it is low in boundary points (the gradient vector is big and perpendicular to the local direction of the contour) and is high otherwise. The outer part is the cost of passing the point \((n,\rho^{\prime})\):  
\[C^{(O)}(n,\rho)= \begin{cases}0\,\ \ (x,y)\in\Omega_{3}\,\\ \tau_{o},\ \ \ (x,y)\
otin\Omega_{3}\,\end{cases}  \tag{16}\]  
where \(\Omega_{3}\) is the set of points defined by (5), \(x\) and \(y\) are the coordinates of the source image point, which was mapped to \((n,\rho)\). Optimal contour \(S=\{\rho_{n}\}_{n=1}^{W}\) is the one minimizing the total cost:  
\[S^{*}=arg\min_{S}\sum_{n=1}^{W}C_{n}(\rho_{n},\rho_{n+1}). \tag{17}\]  
This discrete optimization problem can be solved by various methods. Here the method works in quite a narrow ring and the exhaustive search is faster due to small overhead. Denote sum in (17) as \(\Sigma\). In the best case \(\Sigma=0\), in the worst case \(\Sigma=W(\tau_{i}+\tau_{o})\). Mapping this into the range \([0;1]\) where value 1 stands for best we obtain the quality  
\[Q_{ref}=1-\frac{\Sigma}{W(\tau_{i}+\tau_{o})}. \tag{18}\]  
The contour is considered acceptable if \(Q_{ref}>0.5\), otherwise the decision is made that the pupil border cannot be detected with the required precision and the segmentation is terminated. The algorithm runs in five steps. Step 1. Candidates selection. The same gradient calculation as in the first step of previous methods is used. Then the conditions (1), (4) are imposed as in Step 2 of base radii detection. However, a smaller angular threshold \(\tau_{\phi}=30^{0}\) is set since the center position is known with better precision. Step 2. Polar transform.