# Research on Innovative Apple Grading Technology Driven by Intelligent Vision and Machine Learning
## 2 Materials and Methods
### Apple Feature Extraction
#### 2.4.2 Apple Segmentation  
The background information significantly impacts the results of apple feature extraction, making it necessary to precisely segment the fruit from the background. To ensure more accurate apple segmentation, this paper designed a preprocessing algorithm tailored for the task scenario of an apple grading pipeline, addressing the issue of uneven brightness on apple surfaces. Initially, the image in the RGB space is converted to the LAB space, where the L component representing brightness is separated. Adaptive histogram equalization [21] is then applied for brightness processing. Subsequently, the processed L component is merged with the A and B components. Finally, the image in the LAB space is converted back to the RGB space. As shown in Figure 7, the processed image exhibits a more uniform surface brightness, partially resolving the issue of uneven illumination on the apple surface captured by the camera. The preprocessed apples are segmented using a clustering algorithm. The Density Peak Clustering (DPC) [22] algorithm is a density-based clustering method that can automatically determine cluster centers and the number of clusters. However, its local density calculation for sample points is based on a cutoff distance, which does not account for the distribution of samples within the neighborhood. The choice of cutoff distance significantly affects the apple segmentation results. To address this issue, this paper proposes an improved DPC clustering algorithm (DPC-AWKNN) for apple segmentation. This method employs the Adaptive Weighted K-Nearest Neighbors (AWKNN) approach to adaptively select the nearest neighbors for calculating weighted local density, thereby eliminating the reliance on a global distance threshold. It automatically adjusts to the local density of points, effectively identifying density peaks. This enables the DPC algorithm to dynamically handle apple images with varying densities and shapes, reducing its sensitivity to the cutoff distance parameter. The specific computational process of DPC-AWKNN in the apple image segmentation task is outlined in Algorithm 1. Initially, the Euclidean distance [23] from each pixel to other points is calculated. Then, the AWKNN algorithm is used to compute the weighted local density and determine the minimum distance. Finally, the cluster centers are identified, and the class labels for the remaining pixels are assigned, achieving segmentation of apples from the background. Figure 7: Brightness equalization algorithm processing procedure. ```
Input: Image I Output: Cluster labels
1: Compute the distance matrix (D) for each pixel point. For each pixel point (i, j): D[i][j] = Euclidean distance (i, j). 2: Compute local density (\(\rho\)) using Adaptive Weighted K-Nearest Neighbors. For each pixel point i: \(\rho[i]=1/(\text{average distance to K-Nearest Neighbors})\). 3: Compute minimum distance (\(\delta\)) to a higher density point. For each pixel point i: If \(\rho[i]\) is the highest: \(\delta[i]=\max(\text{D[i][j]})\) for all j Else: \(\delta[i]=\min(\text{D[i][j]})\) for all j, where \(\rho[j]>\rho[i]\). 4: Select cluster centers. Identify points with high \(\rho\) and \(\delta\) as cluster centers. 5: Assign cluster labels. For each non-center data point i: Assign to the cluster of the nearest point j with \(\rho[j]>\rho[i]\). ```  
**Algorithm 1****DPC-AWKNN**  
To demonstrate the effectiveness of the proposed DPC-AWKNN algorithm in apple segmentation tasks, we evaluated the segmentation performance at different resolutions. We plotted the clustering, density decision graph, and clustering distribution graph and compared the segmentation results with the current mainstream K-Means [24] clustering algorithm. The specific visualization results are shown in Figure 8. In the decision and clustering graphs, it is evident that DPC-AWKNN accurately identifies the optimal decision boundary across different resolutions.