# On-the-Fly Olive Tree Counting Using a UAS and Cloud Services
## 3 Materials and Methods
### Data Processing
#### 3.2.3 Registration and Merge  
During the UAV flight, the camera is configured to take a picture at a fixed time period (one every 2 s). Each image separately goes through the segmentation process described above and results in a list of pixel coordinates. When the segmentation algorithm processes the following image, it obtains a new list of pixel coordinates. Given the overlap of the images (80%), many trees located in the first image will appear in the subsequent images. In order to detect which trees are new and which are repeated, we need to project the image pixel coordinates to a global reference system and detect which observations correspond to the same tree. This process is repeated for each new image captured by the camera on board the UAV. The processing flows involved are shown in Figure 7.  
Every new image is paired with its previous image using a classical feature detection and matching algorithm (Figure 7a). For each pair of sequential images (let us name them as \(I_{m}\) and \(I_{n}\)), ORB features are computed and matched to calculate the homography matrix \(H_{n}\). Applying the matrix \(H_{n}\) to the pixels of \(I_{n}\) would project them onto the same plane as image \(I_{m}\). Every new matrix \(H_{n}\) is composed with the previous \(H_{m}\) in order to have all tree locations related to the first image reference system. In this way, the upper-left pixel of the first image is the origin of the global reference system, and applying the same process consecutively to each new image in the stream, all the image pixel coordinates (\(L1\)) are converted to global coordinates (\(L2\)). Notice that the perspective transform does not need to be applied to all the pixels in the image, but only to the locations of the trees.  
Figure 7: Flowcharts of the registration and merge algorithms (L1 is the list with the pixel coordinates of the trees detected in the image; L2 is the same list with pixel coordinates projected to a global reference system; and L3 is the list with the coordinates of the trees detected in all the processed images without duplicates). (**a**) Registration. (**b**) Merge.  
It is expected that the same tree can appear in more than one image, but with slightly displaced coordinates. We use a fixed-radius nearest neighbor search [51] to recognize previous observations of the same tree and avoid duplicates. The list of trees of the first image is used to populate the initial global list (\(L3\)). For each new image, its list of trees \(L2\) is merged into \(L3\) following the processing flow in Figure 7b. First, a kd-tree search structure is built from the set of tree locations in \(L3\). For each tree in the new \(L2\), we search for the nearest neighbor inside a fixed radius bound. If there is no neighbor on that radio, it means that there is no previous observation of that tree, and the location is added to \(L3\) as a new tree. Otherwise, it means that the tree already existed in \(L3\), and the new observation is merged with the already existing global tree, thus avoiding counting it twice or more.