# Grading Algorithm for Orah Sorting Line Based on Improved ShuffleNet V2
## 2 Materials and Methods
### Orah Diameter Detection  
The background of the image can interfere with diameter measurement. To ensure accurate results, image preprocessing is required before measurement. First, the image is converted to the HSV color space, and the brightness channel (V) is binarized using the Otsu thresholding method. Next, the connected domain area is calculated, and regions smaller than 50 pixels are removed to eliminate background interference. The specific process is illustrated in Figure 7.  
As shown in Figure 8, the Orah in the image capture area of the sorting line will be photographed continuously 10 times, obtaining 10 images of the Orah from different orientations. These images will serve as the input data for the program.  
Based on the filtered binary image, the fitEllipse function from the OpenCV library is used to determine the best-fitting ellipse and obtain the length of the major axis. The outcomes of these steps are shown in Figure 9.  
Figure 6: Images after data augmentation. (**a**) Original appearance of Orah; (**b**) horizontal flip; (**c**) vertical flip; (**d**) random rotation; (**e**) random brightness and contrast; and (**f**) random scaling.  
Figure 7: Images preprocessing procedure. (**a**) Original image; (**b**) V channel; (**c**) Otsu binary image; and (**d**) filtered binary image.  
Pos1 to Pos12. Due to lower image quality and greater distortion at the edges of the frame, measurements were taken at 10 positions within the range of Pos2 to Pos11.
2. Image distortion correction was applied. An orange plastic ball with a diameter of 85 mm was used for diameter tests across the Pos2 to Pos11 range to calculate distortion coefficients at each position. The corrected diameter value is obtained by multiplying the original diameter value by the distortion coefficient.
3. Orange plastic balls with diameters of 55 mm, 70 mm, and 85 mm were used to calibrate the system. A conversion function was generated to convert fitted diameter pixel values into the corresponding physical diameters.  
Diameter algorithm: Based on the above algorithm, the long-axis diameters of a fitted ellipse for the same Orah can be obtained from 10 measurements taken at different angles. However, testing shows that 1-3 results among these 10 often deviate significantly. Analysis indicates this is caused by the binarization process using [PERSON]'s method, which is an automatic thresholding algorithm. If low-grayscale regions appear near the fruit's contour, such as black spots or scars, they may also be classified as background, leading to underestimated results. Similarly, if leaves or other debris from the background appear in the fruit cup, they can merge with the fruit after binarization, resulting in overestimated diameters. Based on practical usage and experience, the following algorithm is used to refine the diameter data:  
1. Define a list \(D=[d_{1}\), \(d_{2}\), \(\ldots\), \(d_{10}]\) and set two thresholds \(D_{\text{max}}\) and \(D_{\text{min}}\). For each diameter, check if \(d_{i}<D_{\text{min}}\) or \(d_{i}>D_{\text{max}}\). If so, remove \(d_{i}\) from the list. \(D_{\text{min}}\) and \(D_{\text{max}}\) are determined based on the size range of the fruit. In this paper, \(D_{\text{min}}=40\) and \(D_{\text{max}}=200\).
2. Use the Z-score method to identify the most outlying value, calculated as follows: Define a threshold \(Z_{\text{max}}\). For each, calculate its Z-score \(z_{i}\). If \(z_{i}>Z_{\text{max}}\), remove the corresponding \(d_{i}\) from the list. According to test results, \(Z_{\text{max}}=15\) was selected.
3. Repeat this step until the list length is less than 6 or all \(z_{i}<Z_{\text{max}}\).  
If the list length is less than 6, an error message is output, and the Orah is directed to a special exit, which leads to the front of the image acquisition chamber for re-grading. If the result is valid, output the average of the remaining values in the list.