# Diagnosis by SAM Linked to Machine Vision Systems in Olive Fitting Machines
## 2 Materials and Methods
### Computer Vision Softwares
#### 2.3.1 Industrial Applications
(**a**) Segmentation into masks via SAM, (**b**) selection of the correct mask, (**c**) anomaly correction, (**d**) binarized olive, red color highlights a common error after segmentation (**e**) morphological analysis and (**f**) statistical analysis, blue ellipse synthesizes olive shape, the red and green lines represents major and minor axes, respectively (**g**) files with data analyses. Segmentation. The script p15.txt, developed in Google Colab, integrates SAM (Segment Anything Model) by META AI to perform zero-shot image segmentation (without additional training) using the \"Everything\" or unsupervised mode [16] (Figure 6a). This script automatically processes the sample folders gordal_olives (.png) and BN_olives (.bmp) and connects via the web to the available SAM platform, executing segmentation without specific prompts. For each image, SAM generates a set of binary masks representing the detected elements (Figure 6b), where the detected object appears in white and the background in black. The masks are automatically stored in output subfolders generated by the script itself for later selection. Both the color images of Gordal and the grayscale images of Hoijblanca were subjected to SAM segmentation without any preprocessing, such as region of interest (ROI) adjustments or RGB channel separation. Mask selection. Using the \"Everything\" mode in SAM requires post-processing operations. For this purpose, the selector_BN_m script was developed in Matlab to select the correct olive mask by binarization (Figure 6c) within the coordinates where the olive is located on the transport chain (Example for Hoijblanca: \(x=140\), \(y=79\)). This operation ensures that only the binarized olive mask is retained within the segmented image folders, reducing the dataset to images containing a single binarized olive while simultaneously discarding other cases from the processed folder (pixel values different from grayscale level = 255). Mask corrections. The segmentation process performed by SAM occasionally presents two types of anomalies, which are corrected using the blob_and_filling.m script developed in Matlab. These errors include background noise, which must be removed, and gaps inside the olive blob, which need to be filled (Figure 6d). The script scans each folder resulting from the previous segmentation and selection process and, if necessary, converts the image to grayscale. It then identifies the largest blob and applies a filtering process to eliminate background noise or fill voids within the selected blob (Figure 6d). These operations are similar to erosion or filling techniques. The final result is a single binarized segmentation mask per olive image captured during the pitting process (Figure 6e). The processed images are stored in two output subfolders, one for each variety, as the final segmentation output. Morphological analysis. The ellipse_3.ipynb script was developed to perform a morphological analysis of the olives, extracting key parameters, such as size, shape and orientation (Figure 6f). This Google Colab application processes the segmented images and first analyzes pixel values in a binary space to detect the olive's contour (pixel values: black = 0, white = 255). Due to the symmetry and regularity of the olives, they can be represented as an ellipse, which is fitted to the contour using the cv2.findContours/cv2.fitEllipse functions from the OpenCV library (Figure 6f). The superimposed ellipse provides an alternative method for measuring the area by counting white pixels. Additionally, the script locates the geometric center, detects the endpoints of the axes, and overlays the major axis (red) and minor axis (green). This enables the determination of the minor axis orientation relative to the horizontal axis, set from the ellipse's coordinate center. The output includes values for area, angle, major axis length, minor axis length and the axis ratio for each binarized image from the pitting process.