## 2 Materials and Methods
### Tomato Detection Algorithm  
As can be observed in Figures 3 and 4, the mature tomatoes are usually located in the lower part of the plant, where there are practically no leaves. Figure 3: Greenhouse work environment. The system performs a series of operations to detect those ripe tomatoes that are in the foreground (not occluded) and segment them from the rest of the image elements. At the end of this stage, each ripe tomato is represented by a single region. The flowchart of the operations performed to detect the ripe tomato is shown in Figure 5.  
During this stage, several operations were carried out simultaneously on different copies of the original image, chosen for its characteristics to show the results of each sequence:  
* Tomato-Edge Detection  
Figure 6a illustrates a typical situation in tomato greenhouses. First of all, the green container in the image measures the amount of drip irrigation water for the plants; this is usually present in many of the greenhouse corridors. As shown in Figures 3 and 4, the tomatoes begin to ripen at the bottom of the plant, where there are few leaves. Nevertheless, there are smaller leaves and tomatoes that have been removed by segmentation and other processes on the right and left sides. In addition, in greenhouse horticulture, the leaves are usually removed from the bottom (the standard cultivation  
Figure 4: Lower part of the crop with mature tomatoes. Figure 5: Flowchart of the ripe tomato detection stage. technique), so only conditions that are normal for greenhouses in this area are being reproduced in the article. First, we choose the R component of the RGB image (Figure 6b). To enhance the contrast, we apply a power law transform \(s=c\,r^{\Upsilon}\), where \(r\) are the initial gray levels of the R image, \(c\) is constant (usually 1), \(\Upsilon\) 1 lightens the image and \(\Upsilon\) 1 darkens the image, and \(s\) are the final gray levels of the image after the contrast enhancement (Figures 6c and 7a). The parameters \(\Upsilon\) and \(c\) vary depending on the type of tomato in the image because the color and reflectance are not the same for all types of tomatoes. The image's histogram analysis allows one to obtain sufficiently good values for \(\Upsilon\) and \(c\). Figure 6: (**a**) Original image; (**b**) R plane; and (**c**) power transform and contrast enhancement. Figure 7: (**a**) Power transform and contrast enhancement; (**b**) Sobel operator; (**c**) gray-scale segmentation; (**d**) size segmentation; (**e**) dilation; and (**f**) size segmentation 2. After increasing the contrast, the tomato-edge detection could be carried out with one of several operators (Sobel, [PERSON], Prewitt, etc. ); however, after conducting an exhaustive study applying the different types of operators, we decided to use Sobel because it provided a more precise positioning of the tomatoes and peduncles (Figure 7b). The noise and the outline of the shadows that appear on the fruit surface make it difficult to capture their exact contour. To keep only what interests us, a series of operations were carried out on the image in Figure 7b. The first was a segmentation based on grayscale or intensity, which allows us to eliminate a large part of the image noise and the effects of the shadows (Figure 7c); this was followed by a segmentation based on size (regions of connected pixels that do not exceed a certain number are eliminated) (Figure 7d). Following the previous functions, the morphological operation of dilation was applied (Figure 7e). The dilation objective is to be able to join all the dashed lines to form a contour without discontinuities, or at least, with many less than those presented at the beginning. Finally, we again performed segmentation based on size (Figure 7f) to eliminate the elements that continue appearing on the fruit surface but that are not part of its contour.