## 5 Image Processing
### Detection  
The objective of this stage is to classify the different detections into good ones and bad ones. Mainly, the purpose is to differentiate between detections that are brown leaves and those that are ground. For that, a condition of maximum size of the detection is imposed. This value must be also relative to the distance from which the image is taken, and therefore have been fixed relative to the leaf size previously obtained. After this condition is applied a mask with only the significant detections is obtained. A ratio of the _number of bad leaves_ (NBL) in the image is then calculated as it is shown in Equation (8):  
\[\textit{NBL}\text{=}\frac{\text{\% pixels in the mask}}{\text{size of leaf}} \tag{8}\]  
This value takes into account both the size of the detection mask and the size of the leaf, and gives an idea of how bad the plants in the image are. An important question to take into account is the image resolution needed in order to get good results. We expect that as the resolution of the vines is lower, the results will be worse. For this purpose a study have been done processing a set of images, for which the NBL ratio have been obtained. Also, the good detections in this images have been manually marked in order to get a truth-mask to compare the results. Images have been taken from different distances and with different perspectives, including or not including ground, sky, \(\ldots\) For each one, an average leaf size in pixels have been manually assigned, and since all the images have the same size, this is directly related with their resolution. In Figure 15, the results for the different images are shown, showing both NBL, the estimated and the obtained from hand-marked mask. They are sorted by the leaf size (showed as dash-dotted line), that is, by increasing resolution. Figure 14: Image of Figure 12(b) after morphological operations with a squared structuring. **(a)** After the closing operation. **(b)** After the opening operation. The error in the estimation of the NBL compared with the hand-marked mask, calculated as the difference in NBL obtained in both cases, is shown in Figure 16, as a function of the leaf size. The error is clearly decreasing as the size raises, that is, as the resolution is higher because the images are taken from a closer distance. This last result means that this technique is only useful with a certain resolution of the plants. However, if the resolution is not enough, the cameras can make a zoom at the vines (detected by the green mask) and perform a better processing. Finally, we show several examples of the processing in Figure 17. There are involved images taken from different distances, and therefore different leaf sizes. In Figure 17(a) there is an example of an image where all the leaves are good. The leaves are large since the image has been taken from a short distance with a good resolution. We can see two detections that should not be detected, but they are very small compared to the leaf size, leading to a small value of NBL. The NBL for this image is 0.02. Figure 17(b,c) shows examples of images with medium leaf size, taken from a distance higher than the previous case. The resolution is medium also. Bad leaves are correctly detected and at the same time we can see that the sky in the second example is discarded. Also, there are small detections that could be interpreted as erroneous, but it seems that the second image have a more quantity of leaves with problems than the first one. This is confirmed by the NBL estimated, that is 0.85 and 1.42, respectively. Figure 16: Error in the estimated NBL compared with the leaf size. Figure 15: Number of brown leaves (NBL), for different images, estimated with our algorithm (solid line) and hand-marked (dashed line) for comparison. Images are sorted by the leaf size, also showed (dash-dotted line). Finally, in Figure 17(d), there is an example of an image taken from a far distance, and therefore we have a low resolution. As can be appreciated, the leaves are small compared with previous examples.