# On-the-Fly Olive Tree Counting Using a UAS and Cloud Services
## 3 Materials and Methods
### Data Processing
#### 3.2.1 Color-Based Segmentation  
Figure 4 shows the flowchart of the color-based segmentation algorithm. The detail of the processing at intermediate steps for a specific image is given in Figure 5. The image is first scaled both to reduce computational efforts and to put all images to the same scale. Spatial parameters, such as the kernel size in morphological operations or the template size, have been calculated experimentally for an image taken at 100 m of altitude and scaled to 25% of the original size. For each image, the scale factor \(s\) is calculated depending on its flight altitude \(h\) above ground level (AGL) (\(s=0.25\cdot h/100\)).  
Figure 4: Flowchart of the color-based segmentation algorithm (x, y, and r are the center pixel coordinates and the radius of the circle enclosing the detected trees; L1 is the output list with the image pixel coordinates of the detected trees).  
Figure 3: Tree counting data processing flow (L1 is a list with the pixel coordinates of the trees detected in one image; L2 is the same list with pixel coordinates projected to a global reference system; and L3 is a global list with the coordinates of the trees detected in all the processed images without duplicates).  
The images are next converted from the original RGB (red/green/blue) to the CIE Lab color space. Previous literature [44] has shown that the RGB images are not the most convenient representation for the soil/plant classification problem. After comparing 11 different color spaces on a set of images, the CIE Lab color space was shown to achieve superior classification with the \(a\) channel producing a 99.2% accuracy. The CIE Lab color space consists of one luminance channel (\(L\)) and two chrominance channels (\(a\) and \(b\)). The \(a\) axis extends from green to red and the \(b\) axis from blue to yellow. Minimum (\(L_{min}=0,a_{min}=120,b_{min}=0\)) and maximum (\(L_{max}=97,a_{max}=127,b_{max}=130\)) thresholds have been defined for the olive trees in the CIE Lab color space based on the values of the pixels in the tree areas and compared to the background and to other vegetation that could be found in the images.  
After CIE Lab channels, thresholding, closing, and opening morphological operations with a kernel of size 3 \(\times\) 3 are applied on the resulting binary image to remove noise (Figure 5a). Considering that we are looking for circular shapes, we compute the distance transform, where the value of each pixel is replaced by its distance to the nearest background pixel (Figure 5b), and use template matching to search for matches between the distance image and a disk template (Figure 5c). The template has been designed taking into account the usual values of size and separation between olive trees. The used template is the result of applying the distance transform on a white circle of 30 pixels in diameter centered on a black square of size 80 \(\times\) 80 (top left corner in Figure 5b). The resulting image is filtered using Otsu thresholding [45] to obtain the peak regions, each corresponding to an olive tree. Finally, the minimal enclosing circle of each region is computed to obtain the center coordinates and the radius (Figure 5d). Only circles with a radius between 5 and  
Figure 5: Images corresponding to the intermediate steps when applying the color-based segmentation algorithm to the image DJI_0225. (**a**) Color-based thresholding. (**b**) Distance transform and template (top left). (**c**) Template matching. (**d**) Detected trees.  
45 pixels size are returned as trees. The algorithm returns a list containing the pixel coordinates of the center of the trees.