# Automatic System for the Detection of Defects on Olive Fruits in an Oil Mill
## 2 Materials and Methods
### Image Processing
#### 2.2.1 Detection of Olives  
As a previous step to the iterative application of the active contours technique for the detection of the olives, it is necessary to have the models that should be adjusted to the image data for the global contour. These models are obtained in the preprocessing step using classical segmentation procedures based on global and locally adaptative thresholding. The obtainment of these models and the olive contours are presented next. * Obtainment of the models The global thresholding method applied for obtaining the global model or mask is by [PERSON] [25], which employs the entropy of the histogram for the computation of the optimal threshold. The method considers that the optimal threshold \(U_{opt}\) has been obtained when the sum of the entropy of the background \(H_{f}(T)\) and the entropy of the object \(H_{obj}(T)\) are maximized, i.e., \[U_{opt}=arg\ max[H_{obj}(T)+H_{f}(T)].\] (1) The entropies are defined as: \[H_{obj}(T) = -\ \sum_{g=T+1}^{G}p_{obj}(g)\cdot log\ p_{obj}(g)\] (2) \[H_{f}(T) = -\ \sum_{g=0}^{T}p_{f}(g)\cdot log\ p_{f}(g),\] where \(p_{obj}(g)\) and \(p_{f}(g)\) correspond to the mass probability functions for the object and background pixels, defined, respectively, as: \[p_{obj}(g) T+1\leq g\leq G\] (3) \[p_{f}(g) 0\leq g\leq T,\] being \(g\) a specific gray level, \(G\) the maximum gray level and \(T\) the threshold chosen to segment the image. The adaptive umbralization method employed for the obtainment of the adaptive model or mask is by [PERSON] [26]. This method consists of the the selection of a different segmentation threshold for each pixel of the image, according to some local feature in a given neighbourhood area that is centered in the pixel. The local feature employed by this method is the mean between the higher and lower values of the pixels included in the neighbourhood area (\(w\)). This way, the optimal threshold for each pixel of the image is obtained as:  
\[U_{opt}(i,j)=0.5\cdot\{max_{w}[I(i+m,j+n)]+min_{w}[I(i+m,j+n)]\}, \tag{4}\]  
where \(I(i,j)\) is the intensity level of the pixel in the original image, while \(m\) and \(n\) represent, respectively, the number of rows and columns of \(w\), which defines the neighbourhood area. The results that are obtained with this method are totally dependent on the size of this neighbourhood. [PERSON] established the following correspondence between the appropriate dimension of the neighbourhood area and the contrast between defects and background:  
\[w=31\ \ \rightarrow\ C=I_{max}(x,y)-I_{min}(x,y)\geq 15 \tag{5}\]  
being \(I_{max}\) and \(I_{min}\) the maximum and minimum gray levels of the original image, respectively. These values have been used in this article. * Detection of the contours As commented above, the detection of the contours of the olives is based on the application of the active contours, or snakes, method. This method starts with a contour that is relatively close to the final solution, which has been called the model above, and this contour evolves to a local minimum of an energy functional. Mathematically, a snake is defined as a parametric curve \(\mathbf{r}(s)=(x(s),y(s)),\ with\ s\in(0,1)\). The energy functional can be represented as \[E^{*}_{snake}(\mathbf{r})=\int_{0}^{1}E_{internal}(\mathbf{r}(s))ds+\int_{0}^{ 1}E_{external}(\mathbf{r}(s))ds,\] (6) where \(E_{internal}\) is the energy of the curve that defines the capacity of the contour to adapt itself to the frontier of interest and \(E_{external}\) is the energy that guides the snake towards this frontier. The energy of the curve \(E_{internal}\) is composed of the sum of the elastic energy (\(E_{elastic}\)) and the rigidity energy (\(E_{rigidity}\)), being: \[E_{internal}(\mathbf{r}(s))=\frac{\vartheta_{1}||\mathbf{r}^{\prime}(s)||^{2}+ \vartheta_{2}||\mathbf{r}^{\prime\prime}(s)||^{2}}{2},\] (7) where, by adjusting the parameters \(\vartheta_{1}\) and \(\vartheta_{2}\), it is possible to control the relative weight of \(E_{elastic}\) and \(E_{rigidity}\).