## 2 Materials and Methods
### Image Processing
These distance values are used to identify and segment coffee granule objects in the images. The steps to obtain the distance image include transforming the obtained color image of the ROIs in the previous step into a grayscale image, inverting the grayscale image, brightening the coffee grains and coin, and darkening the background, using an \(11\times 11\) octagonal mask to perform dilation to further close tiny gaps on the coffee granules, applying a global threshold with a minimum value of 100 and a maximum value of 255 to segment the image to obtain regions of interest (ROIs) for the coffee granules and reference coin, and performing distance transformations to change the pixel values inside the coffee granules or reference coin to their distances to the closest boundary. The complete processes for obtaining the distance image are illustrated in Figure 6.  
Figure 1:0 Color image of the ROIs. Figure 1:1 Process to obtain the distance image. The third main step is to apply a watershed transformation to the obtained distance images to further refine coffee granule boundaries for better segmentations. In the watershed transformation theory, an image is metaphorically treated as a topographic landscape, where the pixel intensities represent elevations. Low-intensity regions correspond to valleys, while high-intensity regions correspond to hills or peaks. The gradient of the distance image can help to identify the potential regions where watershed lines can be applied. Areas of large gradients with rapidly changing distance values usually correspond to object boundaries. The flooding typically starts from the local minima in the gradient image, corresponding to potential watershed lines. The threshold, which is the minimum height of the watersheds, should be set. Applying the watershed transform using the distance image and threshold as inputs can prevent over-segmentation, resulting in better segmentation results for images with complex structures and varied intensities. The algorithm simulates a flooding process, where it starts from the local minima and fills in regions based on the gradient information. Watershed lines separate different regions, and one can obtain segmented objects as distinct regions in the results. The detailed procedure of applying a watershed transform to segment coffee granules based on distance image is composed of performing a histogram equalization on the distance image to the full range of 0-255 to further enhance the size contrast of coffee granules, thereby obtaining a more accurate size distribution of coffee granules; inverting the distance image; performing a gray value closing with a small \(3\times 3\) rectangular mask on the inverted distance image to remove small holes so that the inverted distance image is clean, then performing a watershed transform to extract watershed basins from an image using a threshold of three for coffee granule segmentations; and finally calculating the intersection of the regions obtained in the watershed transform with the regions of interest (ROIs) in Figure 6 for the coffee granules and reference coin so that the coffee granules and reference coin are properly separated from the background. The above procedure is illustrated in Figure 1:2  
In the resulting image with the separated coffee granules and reference coin in Figure 7, calculating the pixel number within the scope of a coffee granule obtains the size of that coffee granule in the unit of pixels. The same method can also be used to obtain the size of the reference coin in the unit of pixels. As the sizes of those coffee granules are proportional  
Figure 1:3 Watershed transform for coffee granule size distribution analysis. to the size of the reference coin and the actual size of the reference coin is known, the size of every coffee granule is then calculated. Additionally, the total number of each coffee granule size is also calculated. Thus, a size distribution of coffee particles is obtained.