## 2 Materials and Methods
### Image Processing
For binary images, erosion [8] shrinking or thinning the white regions (foreground) while expanding the black regions (background) can be used to separate objects that are touching or overlapping; dilation [8] expanding or thickening the white regions (foreground) while shrinking or eroding the black regions (background) can be used to close gaps between objects or make them touch; opening [8] is a sequence of erosion followed by dilation to remove noise and small objects from binary images; and closing [8] is a sequence of dilation followed by erosion to close small gaps or holes in binary images. The mentioned morphological operations are applied interactively in further image processing. The first main step in image processing is to remove image background noise and create a mask that covers the regions of interest (ROIs) occupied by coffee granules and the reference coin in images. Then, we intersect the binary mask of ROIs, and the original image according to three color channels so that a color image will be obtained and both the coffee granules and the reference coin are extracted. The necessary steps are described in detail as follows. First, we resize the original color image to a 3024 \(\times\) 4032 resolution, converting it to an 8-bit grayscale image for further image processing. In a grayscale image, both the reference coin and the coffee granules are much darker than background. In other words, the pixel values of both the reference coin and coffee granules are much smaller than the pixel values of the background. The further image processing starts using a 500 \(\times\) 500 round structuring element, which is larger than the scope of the reference coin and coffee granules, to perform grayscale dilation first followed by grayscale erosion so that both reference coin and coffee granules are eliminated and image background is extracted, as shown in Figure 2.  
Figure 2: Shadow-reducing image background extraction. The main purpose of this operation is to average the background gray value of the original image so that the shadows are reduced, making it easier to extract the coffee granules and reference coin later. The original grayscale image is then subtracted from the extracted background grayscale image, and a grayscale bias is added. The bias is adjustable; in this case, the value is set to 110, aiming to isolate the regions occupied by the coffee granules and reference coin, as depicted in Figure 3. If an overflow or an underflow occurs, the pixel values are clipped. Further processing includes performing a histogram equalization on the obtained image to increase the contrast between the coffee granules and the background, followed by reducing the noise using averaging filtering with a 5 \(\times\) 5 mask on the image. Then, we use an \(11\times 11\) octagonal mask to successively perform grayscale closing to removes dark spots of tiny coffee powders isolated in the bright background and perform grayscale opening to fill bright spots of tiny holes isolated in dark coffee granules. In addition, coffee granule boundaries are smoothed. By conducting these tasks, more accurate shapes of coffee granules are obtained. To create the mask of the ROIs (the coffee granules and reference coin), the image is segmented using a global threshold with minimum value of 105 and a maximum value of 255. The process is illustrated in Figure 4.  
The obtained binary mask of the ROIs is then used to intersect with the original image according to three color channels so that a color image is obtained. Both the coffee granules  
Figure 4: Process to create the mask of the ROIs. Figure 3: Isolating the regions occupied by the coffee granules and reference coin. and reference coin are extracted in black, and the bright background is now free of noise, as shown in Figure 5.  
The second main step is to apply distance transformations [50] on coffee granule images to estimate coffee granule sizes. Distance images represent the distance of each pixel in the image to the nearest object boundary.