# Optic Disc Detection from Fundus Photography via Best-Buddies Similarity
## Materials and Methods
### Best-Buddies Similarity  
Best-Buddies Similarity is based on the following assumptions: The target pixels under different backgrounds always follow the same probability distribution. The similarity between them is calculated by counting the number of matching feature points in the template and the target images candidate area.  
Figure 1: The construction of template image. The blue rectangle is the minimum bounding rectangle containing the whole OD region marked by a green circle.  
We first define two point sets: \(R=\left\{r_{i}\right\}_{i=1}^{U}\) and \(S=\left\{s_{j}\right\}_{j=1}^{V}\), where \(r_{i}\), \(s_{j}\in R^{d}\), \(U\), and \(V\) are the number of feature points in the point set of \(R\) and \(S\), respectively. \(R=\left\{r_{i}\right\}_{i=1}^{U}\) and \(S=\left\{s_{j}\right\}_{j=1}^{V}\) represent the feature point set of the template and the candidate region in the target image, respectively. BBS measures the similarity between these two sets. A pair of points \(\left\{r_{i}\in R,s_{j}\in S\right\}\) is a Best-Buddies Pair (BBP) if \(r_{i}\) is the nearest neighbor of \(s_{j}\) in the set \(S\), and vice versa. By discriminating BBP, the matching matrix of \(R\) and \(S\) is obtained. The following is the mathematical expression of BBP.  
\[Seg\left(r_{i},s_{j},R,S\right)=\left\{\begin{array}{ll}1&NN\left(r_{i},S \right)=s_{j}\wedge NN\left(s_{j},R\right)=r_{i}\\ 0&otherwise\end{array}\right. \tag{1}\]  
where \(NN\left(r_{i},S\right)=argmin\ d\left(r_{i},s\right),s\in S\), and \(d\left(r_{i},s\right)\) is some distance measure, \(\wedge\) is the AND operation, and \(NN\left(r_{i},S\right)=s_{j}\) indicates that the nearest neighbor of \(r_{i}\) in point set \(S\) is \(s_{j}\). BBS is then taken to be the fraction of Best-Buddies Pairs (BBPs) between these two sets and the equation of the BBS between the point sets \(R\) and \(S\) is given by  
\[BBS\left(R,S\right)=\frac{1}{\min\left\{V,U\right\}}\cdot\sum_{i=1}^{U}\sum_{j =1}^{V}Seg\left(r_{i},s_{j},R,S\right). \tag{2}\]  
We need to compute the distance between each pair of points to calculate the BBS between two point sets \(R,S\). The distance measure consists of two parts in the original BBS, one part is the difference of the RGB appearance, the other part is the difference of the \(x,y\) location of the central pixel. In the actual situation, the distance measure, which only computes the difference in appearance and location, is usually limited. To improve the efficiency of OD detection, we introduced a gradient algorithm based on a first-order derivative into the distance measure, which makes the algorithm more robust and improves the accuracy of the detection results.  
Mathematically, the image can be regarded as a two-dimensional discrete function, and the image gradient is the derivative of this function:  
\[\delta\left(x,y\right)=dx\left(p,q\right)+dy\left(p,q\right) \tag{3}\]  
where \(dx\left(p,q\right)=l\left(p+1,q\right)-l\left(p,q\right)\), \(dy\left(p,q\right)=l\left(p,q+1\right)-l\left(p,q\right)\), \(l\) is the gray value of the pixel, and \(\left(p,q\right)\) represents the