## 2 Materials and Methods
### Detection Algorithm of Grape Clusters
#### 2.2.4 Images Pixels Classification and Targets Extraction  
After finishing the training of the final strong classifier, we used it to distinguish the pixels of the captured images and identify if it is grapes or not. A sub-window (7 \(\times\) 7 pixels) for every pixel was \begin{table}
\begin{tabular}{c c c c} \hline \hline
**Classifiers** & **Classifiers of Equation** & **Errors/\({}^{\prime\prime}\)\({}_{t}\)** & **Weight of Weak Classifiers/ff\({}_{t}\)** \\ \hline \(h_{1}(x_{i})\) & \(H(x_{i})-0.57=0\) & 0.044 & 1.537 \\ \(h_{2}(x_{i})\) & \(Cb(x_{i})-0.86=0\) & 0.119 & 1.001 \\ \(h_{3}(x_{i})\) & \(0.31-Lab.\)\(b(x_{i})=0\) & 0.225 & 0.620 \\ \(h_{4}(x_{i})\) & \(10\times B(x_{i})-6.16\times R(x_{i})-1.3=0\) & 0.341 & 0.331 \\ \hline \(H(x_{i})\) & \(sign\{1.537\times h_{1}(x_{i})+1.001\times h_{2}(x_{i})+0.620\times h_{3}(x_{i })+0.331\times h_{4}(x_{i})\}\) \\ \hline \hline \end{tabular}
\end{table} 
Table 1: The error rate of weak classifiers and their weights in the final strong classifier. allocated in a captured image using a sliding window approach, and then calculated out the average pixel values of each sub-window in the images of the elected color components, and input the values into the strong classifier to verify whether the pixels were classified as grape. If the result of the final strong classifier was positive, the pixel would be considered to be grape (white), and if the result was negative, the pixel would be considered to be background (black). Figure 6a is the result image of Figure 4a obtained by using the strong classifier. However, in an actual situation, there would still be a small amount of pixels whose colors are very similar to grapes that would not be correctly classified. Although the misclassified pixels were less than 10% of all pixels in most situations, they have a great influence on the correct detection. Therefore, the small discrete regions of the background pixels were removed by the region threshold method, which was based on finding the biggest region of neighboring white pixels in the image and eliminating all regions which were smaller than 1/10 of the biggest region [13]. In addition, for the adhesion noise, by choosing a radius of 5 of the disc structure element, an open filtering was implemented to discard these noises, and subsequently a close operator was executed to recover the contour of grape cluster. Finally, the morphological region was used to fill the hole in the image and obtained the pixel regions of the targets. The detection processes of the grape clusters in Figure 4a were shown in Figure 6. The pseudo code of image segmentation and targets extraction was as follows (in Algorithm 2):  
```
Input: Captured vineyard images
1. For all pixels in the image
2. If (H(\(\cdot\)) \(>\) 0)
3. Pixel binary value=1
4. Else
5. Pixel binary value =0
6. End
7. End
8. For all pixel regions(value for 1) in the binary image
9. Eliminate the regions which are smaller than 1/4 of the biggest region
10. Perform open and close operator to delete the adhesion noise
11. Fill the hole for every pixel regions
12. Draw the enclosing rectangle for every pixel regions
13. End Output: Binary image that contains only the grape clusters
```  
**Algorithm 2** The pseudo code of image segmentation and targets extraction. Figure 6: _Cont._  
After obtaining the pixel regions of grape clusters, the targets were marked by using the enclosing rectangle method. In this method, the bounding box of the grape cluster was computed by the regionprops function of Matlab, and then the rectangle for the corresponding grape clusters was drawn. When there are two adjoining grape clusters, the barycenter of the pixel region of grape clusters was firstly calculated, and then the region was split into two parts by using the dividing line that passed through the barycenter from top to bottom, and the enclosing rectangle of each region was calculated, respectively.