## 3 Algorithm
### Template Matching  
Heretofore, for a given image, we have found its \(N\) fingers and calculated its finger angles \(A[i]\), \(\mathrm{A_{L}}[i]\), and \(\mathrm{A_{R}}[i]\), where \(i=1\)-\(N\). Based on these angles and the template's finger angles \(A_{T}\), we have predicted all the possible positions of the rest (5-\(N\)) fingers to form a set of complete hands with five fingers and five extended angles \(A_{Ej}[i]\), where \(j\) corresponds to the \(j\)th possible gesture \(Y_{j}\), \(i=1\)-\(5\), and \(j=1\)-\(M\). All notations are shown in Figure 7.  
Now we need to decide which gesture in \([Y_{j}]\) is most correct. Thus, we defined a metric \(L(Y_{j})\), also known as cost function, to assess the dissimilarity between a candidate \(Y_{j}\) and the template. Further, we also add two terms to evaluate the rationality of the gesture \(Y_{j}\) itself. Finally, the gesture with a minimal error is considered to be the true case. The metric is given below:  
Figure 6: (**a**) The template image of gesture “Five”. (**b–d**) Three possible gestures and their five extended fingers, including three known ones (red) and two predicted ones (orange). Figure 7: All finger angles and their notations used in the algorithm. \(A[i]\) represents a known angle. \(A_{L}[i]\) and \(A_{R}[i]\) are the finger left and right angles. \(A_{T}\) are the angles of the template, and \(A_{E}\) are the five extended angles. Each type of angle is indexed from left to right as 1, 2, etc. \[Y=\underset{Y\in\{Y_{j}\}}{argmin}L(Y),\qquad L(Y)=L_{1}+L_{2}+L_{3}+L_{4},\qquad j= 1,2,\ldots,M \tag{5}\]  
\[L1=\sum_{i=1}^{5}|A_{E}[i]-A_{T}[i]| \tag{6}\]  
\[L2=\sum_{1}^{4}|(A_{E}[i]-A_{E}[i+1])-(A_{T}[i]-A_{T}[i+1])|^{Exp_{L2}},Exp_{L2 }=2 \tag{7}\]  
\[L3=\sum_{i=1}^{4}B[i]\times max(0,\;A_{EL}[i+1]-A_{ER}[i])^{Exp_{L3}},\;Exp_{L3 }=4 \tag{8}\]  
\[L4=Inf\times\left\{ \begin{array}{c}(Y_{j}[5]==0,A_{R}[N]\leq A_{BR}+5)\\ Or\left(Y_{j}[5]==1,A_{R}[N]>A_{BR}+5\right)\\ Or\left(Y_{j}[1]==0,Y_{j}[2]==0,A_{L}[1]\geq A_{BL}-5\right)\end{array} \right\} \tag{9}\]  
The cost value consists of four parts:  
\(L_{1}\) is the dissimilarity of finger angles between the extended fingers and the template. It is the primary term for matching two gestures. \(L_{2}\) is the dissimilarity of the differential of finger angles. The angle interval between two adjacent fingers is supposed to remain constant. When a predicted finger is mistakenly inserted between two adjacent fingers, \(L_{2}\) will be very large. After the experiment, we set the exponent of this equation \(Exp_{L2}=2\). \(L_{3}\) is the penalty for two adjacent fingers who stay too close to each other. If the right corner of the \(i\)th finger is on the right of the left corner of the \((i+1)\)th finger, then a large penalty is produced. This term is only considered for those fingers inserted between the known two fingers. In the equation, \(B[i]\) equals 1 if the \(i\)th finger is inserted between the two. Otherwise, \(B[i]\) equals 0. Since \(L_{3}\) is a penalty, its exponent \(Exp_{L3}\) should be larger than \(L_{1}\) and \(L_{2}\) which is set to 4. \(L_{4}\) is the penalty when some fingers are outside the boundary. Under the right circumstances, the right boundary must be on the right of the little finger, and the left boundary must be on the left of the index finger. Considering that the detection of boundary points is not very accurate, three looser criteria are applied here: (1) if the \(N\)th finger is not the little finger, then its right angle \(A_{R}[N]\) should be on the left of (larger than) the right boundary \(A_{BR}\); (2) if the \(N\)th finger is the little finger, then its right angle \(A_{R}[N]\) should be near the right boundary \(A_{BR}\); (3) if the 1 st finger is neither the thumb nor the index finger, then its left angle should be on the right of (smaller than) the left boundary \(A_{BL}\). In the calculation, a tolerance of \(5^{\circ}\) is considered, as shown in Equation (9).