# Reduction of Pesticide Use in Fresh-Cut Salad Production through Artificial Intelligence
## 2 Materials and Methods
### Field Tests
#### 2.3.1 Real-Time Measurement of the Plants' Features  
The camera used for the tests is the Intel RealSense(tm) Depth Camera D435i [12]. Apart the relatively low cost (about 200 $), this model allows at the same time to measure distances and to collect images; moreover, this specific model is equipped with an integrated IMU sensor. The algorithm for image processing has been implemented in C++ via the RealSense library [13], as it contains all the functions needed to use the camera. The algorithm structure for image processing implements a classical pipeline. At first, some filters [14] are defined and applied to the data acquired by the camera, to reduce the noise and improve image quality, as follows:  
Figure 1: CAD model of the prototype. * decimation filter: the resolution of the depth scene is reduced to reduce the necessary computation power;
* spatial filter: an edge-preserving filter with a performance that can be used in real-time;
* temporal filter: a filter that uses the information of the previous frames to adjust the current frame. This is a useful function, above all in static conditions. After data acquisition and filtering, color detection and segmentation algorithms were used to find the plants. The OpenCV library [15] has been used to implement a simple algorithm [16] assuming, in a greenhouse scenario, the most common salad color is in the range of green, while the terrain is typically earth brown. Based on this simple assumption, it was necessary to find the right range of greens to detect the plants; although a possible solution could have been to work in the red, green, and blue (RGB) domain [17], since the frames were already in this format, the color values of this domain are extremely sensitive to light intensity, and therefore the best option was to convert each frame into the hue saturation value (HSV) domain [18]. Once the RGB frame is converted into HSV, a binary mask is computed to find where a specified color is. As the tests were not conducted in a greenhouse, the assumption on the contrast between the salads' and the terrain's colors was not valid, and we had to tune the range also considering the background of the images, containing grey (the floor color). Due to some reflection, this grey area in the HSV analysis presented some \"yellow\" components, very close to the \"green\" part of the HSV spectrum; the choice of a proper threshold turned in to exclude some parts of the salads not to detect erroneously the ground. For real applications in greenhouse this should not happen, because of the clearer distinction between the color of the terrain and the salads. Knowing where the plants are, it is possible to extract useful information like the height of the plants, from the depth component of the camera, and the percentage of the covered area. To work in real-time, some additional information on the plants is needed; indeed, to reduce the computational power of the algorithm and increase the throughput, the analysis is performed on a smaller area of the frame chosen in the middle of the frame, with a height which is one eighth of the total height of the image and a width that is two-thirds of the frame width. In Figure 2, the red rectangle identifies the plants detected, while the blue one indicates the area analyzed. Assuming the plants we want to analyze are in the middle of the frame, the algorithm measures the distance of the floor/terrain using a strip to the left and a strip to the right of the area under analysis. In doing this, all the \"green\" pixels are discarded, while the other measures are stored in a vector. The same process is done also for the plants: the algorithm considers all the pixels that are in the intersection of the area under analysis (the blue rectangle) and where the plants are identified; if the pixel is green, the distance is stored in another vector.