## 2 Materials and Methods
### Image Processing
In the proposed image processing flow, morphological operations such as erosion, dilation, opening (erosion and dilation), and closing (dilation and erosion) can cause numerical propagation errors in the sizes of both the coffee granules and the reference coin in a preprocessed image and its processed image. The mean squared error (MSE) [8] and peak signal-to-noise ratio (PSNR) [8; 51; 52; 53; 28] can be used to calculate the errors between preprocessed images and processed images. The formula to calculate the MSE is as follows. Figure 1:4  
where:  
\(M\) is the number of rows in the images. \(N\) is the number of columns in the images. \(A(i,j)\) and \(B(i,j)\) are the pixel intensities at the corresponding locations in preprocessed images A and processed B. A lower MSE indicates better similarity between the preprocessed image and processed image, as it implies that the pixel intensities are closer to each other. For an 8-bit grayscale image, its MSE ranges from 0 to \(255^{2}(65025)\), where the lower, the better. In addition to the MSE, the peak signal-to-noise ratio (PSNR) is another suitable metric for measuring the error between the preprocessed image and processed image. The PSNR is expressed in decibels (dB) and is calculated using the following formula:  
Figure 1:5  
where:  
\(MAX\) is the maximum possible pixel value of the image. \(MSE\) is the mean squared error, which is the average of the squared differences between the corresponding pixels of the original and processed images. A PSNR value in the range of 20-30 dB usually indicates an acceptable level of distortion or noise in the processed image. PSNR values above 30 dB are often considered very good. In the proposed image processing to obtain the size distribution of coffee granules, there are three main morphological operation steps that can cause errors for both the coffee granules and the reference coin in a preprocessed image and its processed image. A program was developed to calculate the MSE and PSNR for preliminary analysis. The first step is the closing and opening operations with an \(11\times 11\) octagonal mask, performed on the averaging filtered image in the process to create the mask of ROIs, as shown in Figure 1:6 When test images were used, the obtained MSE and PSNR values were as follows. MSE: 15.220496937200807  
PSNR: 33.24 (dB) (very good)  
The second step is the dilation operation with an \(11\times 11\) octagonal mask in the process to obtain the distance image as shown in Figure 1:7 When the same test images were used, the obtained MSE and PSNR values were as follows. MSE: 9.238032495984505  
PSNR: 20.31 (dB) (acceptable)  
The third step is the closing operation with a \(3\times 3\) octagonal mask in the process of the watershed transform as shown in Figure 1:8 When the same test images were used, the obtained MSE and PSNR values were as follows. MSE: 0.003990808321785505 PSNR: 72.12 (dB) (very good)  
Those obtained MSE and PSNR values are either acceptable or very good at indicating that the errors caused by morphological operations are small. The reason is that when comparing with the 3024 \(\times\) 4032 resolution of images, those \(11\times 11\) or \(3\times 3\) octagonal masks are very small. For the coffee granules that are relatively larger than the masks, when erosion and dilation occur in pairs and in succession, such as the operations of opening and closing, the net changes of pixels are small. The same goes for the reference coin. Therefore, those morphological operations mainly remove noise and fill small holes on coffee granules and do not cause obvious size errors in the preliminary analysis. Please keep in mind that the coffee granules and the reference coin underwent the proposed image processing together, so they both suffered from the same causes generating propagation errors.