Fruit identification method based on improved YOLOv8n network
By using adaptive image scaling, red channel quantile statistics, and nonlinear mapping to generate fusion weights, combined with channel and spatial attention maps, the problem of insufficient feature extraction in complex scenarios such as lighting changes and occlusion in pomegranate fruit recognition is solved, significantly improving the accuracy of fruit recognition and positioning precision.
Patent Information
- Application Number
- CN202511849637.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-09
- Publication Date
- 2026-04-03
AI Technical Summary
Existing technologies for pomegranate fruit recognition in complex natural scenes suffer from problems such as insufficient feature extraction due to changes in lighting, occlusion, complex backgrounds, and morphological differences, resulting in high rates of missed detections and false detections. Furthermore, traditional methods struggle to simultaneously capture differences in channel importance and spatial correlations, leading to a mismatch between classification confidence and positioning accuracy.
By adaptively scaling the image based on the average diameter of the fruit, combining red channel quantile statistics and nonlinear mapping to generate fusion weights, feature fusion is performed. Channel attention and spatial attention maps are used for feature enhancement. Multidimensional vector flattening and point-by-point linear classification are combined with dynamic thresholding to filter background noise, thereby improving the accuracy of fruit recognition.
It effectively suppresses background interference, improves the model's stable feature extraction under complex lighting conditions, enhances the ability to capture multi-scale targets, reduces the missed detection of small targets, lowers the misjudgment rate of similar color regions, and improves the accuracy and positioning precision of fruit recognition.
Smart Images

Figure CN121789206A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of fruit recognition methods, specifically a fruit recognition method based on an improved YOLOv8n network. Background Technology
[0002] Pomegranate recognition technology has significant application value in the field of smart agriculture. Accurate identification of pomegranate fruits is fundamental to achieving automated pomegranate harvesting, yield prediction, and orchard management. However, pomegranate detection in natural settings faces numerous challenges: fruit occlusion, varying lighting conditions, complex backgrounds, and morphological differences at different growth stages. Traditional computer vision methods often perform poorly under these complex conditions, while the emergence of deep learning object detection algorithms provides an effective solution to this problem.
[0003] In existing technologies, traditional fruit recognition algorithms are not robust enough to changes in lighting, occlusion by branches and leaves, and areas where the background and fruit color are similar, such as withered leaves or soil. This leads to insufficient feature extraction and high rates of missed detections and false detections. Because pomegranate fruits vary greatly in size and the image acquisition distance is not fixed, existing methods are prone to losing small target details or introducing redundant background noise when preprocessing images with a fixed scaling ratio. Most models rely only on single-stage attention mechanisms or shallow feature fusion, making it difficult to simultaneously capture differences in channel importance and spatial correlation, resulting in a mismatch between classification confidence and localization accuracy. Furthermore, the original YOLOv8n model still suffers from missed detections and false detections in complex scenarios. Therefore, there is an urgent need for a fruit recognition method based on an improved YOLOv8n.
[0004] The information disclosed in the background section is only intended to enhance the understanding of the background of this disclosure, and therefore may include information that does not constitute prior art known to those skilled in the art. Summary of the Invention
[0005] The purpose of this invention is to provide a fruit recognition method based on an improved YOLOv8n network to solve the problems mentioned in the background art.
[0006] To achieve the above objectives, the present invention provides the following technical solution: A fruit recognition method based on an improved YOLOv8n network, comprising the following steps: S1: Calculate the average diameter of mature pomegranate fruits and obtain the image to be identified. Based on the average diameter, adaptively scale the image to be identified to obtain a second image. Calculate and sort the red channel values of all pixels in the second image to obtain a first set. Traverse the first set to obtain the red channel values of the set quantiles and further obtain the global contrast. S2: Generate the fusion weight coefficients of the second image by nonlinear mapping based on global contrast. Generate the third image by Gaussian filtering the second image. Perform feature fusion on the second and third images based on the fusion weight coefficients to obtain the enhanced image. Input the enhanced image into the YOLOv8n network and output the output feature map of the image to be recognized. S3: Perform global average pooling and convolution on the output feature map to obtain the channel attention map and spatial attention map. Use element-wise multiplication to fuse the output feature map, channel attention map and spatial attention map to obtain the fused image. S4: Extract the values of all channels in the fused image at each pixel point to form a multi-dimensional vector at the pixel point. Flatten the multi-dimensional vector into a one-dimensional form to form a local feature vector for each pixel point. Use a point-by-point linear classification method to classify the local feature vector into two categories: no pomegranate fruit and pomegranate fruit. Calculate the probability percentage of pixels in the pomegranate fruit category. S5: Traverse all pixels and count the number of pixels with a probability of containing pomegranate fruit that is greater than a preset threshold and mark them as valid pixels. Use the density threshold method to calculate the proportion of valid pixels. When the proportion is greater than the preset density threshold, it is determined that there is fruit.
[0007] Furthermore, a second image is obtained by adaptively scaling the image to be recognized based on its average diameter. The specific steps are as follows: Pomegranate fruits were selected as the sample set. The average diameter of all pomegranate fruits in the sample set was measured, and a scaling factor for the image to be identified was constructed based on the following logic: ; in, Indicates the scaling factor; and These represent the height and width of the image to be recognized, respectively. This indicates the average diameter of a pomegranate fruit; Indicates the target size of the pomegranate fruit in the image; This represents the maximum side length limit of the target area in the pomegranate fruit image; Multiply the scaling factor by the height and width of the image to be recognized respectively, and then round down to obtain the height and width of the scaled image.
[0008] Furthermore, the logic for obtaining the second image is as follows: When the scaling factor is less than or equal to 1, the scaled image is directly output as the second image; When the scaling factor is greater than 1, the position of the current pixel is determined based on the height and width of the scaled image. If the position of the current pixel is within the coverage area of the scaled image, that is, within the height range of the scaled image in the vertical direction and within the width range of the scaled image in the horizontal direction, then the pixel value of the scaled image at the corresponding position is directly adopted. For other non-covered areas, a specific grayscale value is uniformly filled. The grayscale value is generated by: statistically analyzing the red channel values of all pixels in the original image, calculating the average of the sum of the mean and median of all pixels in the red channel, and multiplying the average by a preset brightness attenuation coefficient to obtain the brightness value to be filled.
[0009] Further, the enhanced image is generated, and the specific steps are as follows: The red channel values of all pixels in the second image are statistically analyzed and sorted to obtain the first set. The red channel values at a set quantile are then obtained from this first set. Furthermore, the global contrast is obtained, and a non-linear mapping is performed based on the global contrast to generate the fusion weight coefficients for the second image. ; in, ; This represents the preset weight parameters; Indicates global contrast; The red channel value represents the 85th percentile. The red channel value represents the 15th percentile. The second image is subjected to Gaussian filtering using a 5x5 Gaussian kernel to generate the third image. Then, feature fusion is performed between the second and third images based on the fusion weight coefficients to obtain the enhanced image. The formula used is as follows: ; in, Indicates the position of the second image Pixel value at; Indicates the location of the enhanced image. The pixel value at that location.
[0010] Further, the output feature map of the image to be recognized is output, and the specific steps are as follows: The YOLOv8n network consists of a backbone convolutional layer and a C2f module group. The specific steps for obtaining the output feature map are as follows: The size of the YOLOv8n convolution is [value missing]. In the backbone convolutional layer, a preliminary feature map is obtained, and then the preliminary feature map is input into the improved C2f module group in YOLOv8n to obtain the output feature map; The improved C2f module includes a dual-branch structure, namely... Separate convolution and The dilated convolution takes the initial feature map as input into the dual-branch structure. The feature maps output by the dual branches are added element-wise to generate a multi-scale fused feature map. The multi-scale fused feature map is then aligned with the initial feature map according to the channels and added element-wise again to obtain the output feature map.
[0011] Further, the fused image is obtained, and the specific steps are as follows: Global average pooling and convolution are performed on the output feature maps to obtain channel attention maps and spatial attention maps: ; ; in, Indicates global average pooling; Represents the channel attention map; Represents a spatial attention map; This indicates that the data is spliced according to the channel dimension; This represents the weight matrix of the first fully connected layer; Represents the ReLU activation function; This represents the weight matrix of the second fully connected layer; Represents the sigmoid function; This indicates that the maximum value is taken along the channel dimension; This indicates that the mean value is taken along the channel dimension; express Convolutional layer; This represents the output feature map; The output feature map, channel attention map, and spatial attention map are fused using element-wise multiplication to obtain the fused image. ; in, This represents the merged image; This indicates the element-wise multiplication method.
[0012] Further, the probability percentage of pixels containing the pomegranate fruit category is calculated, and the specific steps are as follows: The values of all channels in the fused image are extracted at each pixel to form a multidimensional vector at that pixel. This multidimensional vector is then flattened into a one-dimensional form to form the local feature vector for each pixel. A point-by-point linear classification method is then used to classify the local feature vectors. ; in, Indicates the position The eigenvector at that location is flattened into a one-dimensional vector. Indicates the first The classification weight vector of each category; Indicates the first Bias terms for each category; Indicates position The pixel at point belongs to the first Confidence level of each category; Indicates the number of category indexes. , This indicates that there are no pomegranate fruits. This indicates that it belongs to the pomegranate fruit; Calculate the probability percentage of pixels that are pomegranate fruit type; ; in, Indicates position The probability that a pixel belongs to the fruit category; Indicates position The confidence level of a pixel belonging to a pomegranate fruit.
[0013] Furthermore, the specific steps to determine the presence of pomegranate fruit are as follows: Iterate through all pixels and count the number of pixels whose probability of containing pomegranate fruit is greater than a preset threshold, marking them as valid pixels: ; in, Indicates an indicator function; Indicates the number of valid pixels; This indicates the percentage of preset threshold values; Indicates the width of the scaled image; Indicates the height of the scaled image; The proportion of effective pixels is calculated using a density threshold method. If the proportion exceeds a preset density threshold, then fruit is considered to exist. Specifically: ; in, This indicates the presence of pomegranate fruit. This indicates that there are no pomegranate fruits. This indicates the preset density threshold.
[0014] Compared with the prior art, the beneficial effects of the present invention are: The scaling factor is dynamically adjusted based on the average diameter of the fruit to preserve key details while suppressing background interference. By generating fusion weights through red channel quantile statistics and nonlinear mapping, the distinguishability between the target area and the background is improved, enabling the model to stably extract pomegranate features even under complex lighting conditions. Channel attention enhances the response of fruit color-related features, while spatial attention focuses on the central area of the fruit. The element-wise fusion of the two enhances the model's ability to capture multi-scale targets and reduces the missed detection of small targets introduced by scaling. Through multi-dimensional vector flattening and point-by-point linear classification, pixel-level features are directly associated with the probability of fruit presence. Combined with dynamic threshold filtering of background noise, the misclassification rate of similar color areas is reduced. Attached Figure Description
[0015] Figure 1 This is a schematic diagram of the overall method flow of the present invention; Figure 2 This is a graph showing the relationship between global contrast and the corresponding fusion weight coefficients. Detailed Implementation
[0016] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to specific embodiments.
[0017] It should be noted that, unless otherwise defined, the technical or scientific terms used in this invention should have the ordinary meaning understood by one of ordinary skill in the art to which this invention pertains. The terms "first," "second," and similar terms used in this invention do not indicate any order, quantity, or importance, but are merely used to distinguish different components. Terms such as "comprising" or "including" mean that the element or object preceding the word encompasses the elements or objects listed following the word and their equivalents, without excluding other elements or objects. Terms such as "connected" or "linked" are not limited to physical or mechanical connections, but can include electrical connections, whether direct or indirect. Terms such as "upper," "lower," "left," and "right" are used only to indicate relative positional relationships; when the absolute position of the described object changes, the relative positional relationship may also change accordingly.
[0018] Example: Please see Figures 1-2 The present invention provides a technical solution: A fruit recognition method based on an improved YOLOv8n, comprising the following steps: S1: Calculate the average diameter of mature pomegranate fruits and obtain the image to be identified. Based on the average diameter, adaptively scale the image to be identified to obtain a second image. Calculate and sort the red channel values of all pixels in the second image to obtain a first set. Traverse the first set to obtain the red channel values of the set quantiles and further obtain the global contrast. The specific steps for obtaining the second image by adaptively scaling the image to be recognized based on the average diameter are as follows: Pomegranate fruits were selected as the sample set. The average diameter of all pomegranate fruits in the sample set was measured, and a scaling factor for the image to be identified was constructed based on the following logic: ; in, Indicates the scaling factor; and These represent the height and width of the image to be recognized, respectively. This indicates the average diameter of a pomegranate fruit; Indicates the target size of the pomegranate fruit in the image; This represents the maximum side length limit of the target area in the pomegranate fruit image; Multiply the scaling factor by the height and width of the image to be recognized respectively, and then round down to obtain the height and width of the scaled image.
[0019] In the above process, by introducing the ratio of the average diameter of the pomegranate fruit to the target size as a scaling factor constraint, it is ensured that the pomegranate fruit occupies a fixed physical pixel scale in the scaled image. Large fruits photographed at close range are compressed to the target size to avoid exceeding the detection field of view; small fruits photographed at a distance are magnified to the target size, preserving details such as skin texture, which significantly reduces the problem of missed or false detections caused by the difference in target size. By obtaining the scaled image of integer size through the rounding down method, pixel redundancy or blank filling caused by non-integer scaling factors is avoided, ensuring that all pixels in the image input to the subsequent model come from the effective area. The scaling factor is dynamically calculated based on the original image size, the average diameter of the pomegranate fruit, the target size, and the maximum side length limit. It is used to adaptively adjust the image to be recognized into a second image suitable for the detection task, ensuring that the scaled image does not exceed the preset maximum side length limit. To avoid wasting storage or computing resources due to excessively large images, and to adapt to the target size of pomegranate fruits. This ensures that the proportion of detected fruits in the image meets the model input requirements, based on the average diameter. To eliminate scale bias caused by differences in actual fruit size, the larger the original image size, the better. and The smaller the value, the more it limits... The upper limit is set to prevent the scaled image from exceeding the hardware's processing capabilities. This reflects the actual fruit size. If the average fruit diameter is larger than the target size, then... If the value is less than 1, the image will be forcibly reduced in size to fit the fruit size for detection; otherwise, it will be allowed to be enlarged appropriately. This directly determines the proportion of target pixels in the fruit; the larger the value, the better. The higher the upper limit; The logic for obtaining the second image is as follows: When the scaling factor is less than or equal to 1, the scaled image is directly output as the second image; When the scaling factor is greater than 1, the position of the current pixel is determined based on the height and width of the scaled image. If the position of the current pixel is within the coverage area of the scaled image, that is, within the height range of the scaled image in the vertical direction and within the width range of the scaled image in the horizontal direction, then the pixel value of the scaled image at the corresponding position is directly adopted. For other non-covered areas, a specific grayscale value is uniformly filled. The grayscale value is generated by: statistically analyzing the red channel values of all pixels in the original image, calculating the average of the sum of the mean and median of all pixels in the red channel, and multiplying the average by a preset brightness attenuation coefficient to obtain the brightness value to be filled.
[0020] In the above process, when the scaling factor is less than or equal to 1, the pixels of the original image are mapped to the pixels of the scaled image, specifically as follows: ; ; in, Represents the x-coordinate of a pixel in the original image; Represents the ordinate of a pixel in the original image; The x-coordinate of the pixels in the scaled image; This represents the vertical coordinate of the pixels in the scaled image.
[0021] S2: Generate the fusion weight coefficients of the second image by nonlinear mapping based on global contrast. Generate the third image by Gaussian filtering the second image. Perform feature fusion on the second and third images based on the fusion weight coefficients to obtain the enhanced image. Input the enhanced image into the YOLOv8n network and output the output feature map of the image to be recognized. The specific steps to generate the enhanced image are as follows: The red channel values of all pixels in the second image are statistically analyzed and sorted to obtain the first set. The red channel values at a set quantile are then obtained from this first set. Furthermore, the global contrast is obtained, and a non-linear mapping is performed based on the global contrast to generate the fusion weight coefficients for the second image. ; in, ; Indicates the fusion weight coefficient; Indicates global contrast; The red channel value represents the 85th percentile. The red channel value represents the 15th percentile. In the above process, by calculating the global contrast of the red channel, the brightness difference of the main pomegranate fruit area in the image is focused, extreme noise is eliminated, and based on... Nonlinear mapping generates weight coefficients To achieve dynamic adjustment where higher contrast results in greater enhancement, select... and Quantiles, rather than extreme values, can effectively filter out a small number of abnormal pixels in an image, ensuring... Accurately reflects the true contrast distribution of the main area of the fruit, improves the algorithm's anti-interference ability, and through The function transitions smoothly. Lower bound. Ensures that the basic enhancement effect is retained even at extremely low contrast levels, with an upper limit. To prevent artifacts from being generated in high-contrast images due to over-enhancement, while balancing the stability and flexibility of the processing results; Based on global contrast of the red channel of the image Dynamically generated weight parameters are used to control the strength of the fusion algorithm during image enhancement. Quantize the contrast difference in the red channel of an image; the higher the contrast, the better. The larger the value, the stronger the enhancement algorithm's optimization of high-contrast regions; conversely, the smaller the value, the less intervention is needed to avoid noise in low-contrast images due to over-enhancement. Ensure a lower limit for weights to prevent the algorithm from failing when the contrast is too low. The threshold representing the brighter red pixels in the image excludes extreme highlight noise and reflects the distribution characteristics of the highlighted parts of the main fruit. A threshold representing darker red pixels is used to filter out shadows or background interference and extract effective information from the dark areas of the fruit's main subject. The larger the value, the more significant the difference in brightness between the red channel and the image; increasing the value further in this case... Strengthening integration enhances strength and highlights details; conversely, neglecting integration reduces effectiveness. To avoid over-enhancing low-contrast areas, overall contrast... Increase The function output value increases. Increase.
[0022] In the above embodiments, 20 sets of data on global contrast and corresponding fusion weight coefficients are provided to reflect the relationship between the fusion weight coefficients and the global contrast, as shown in Table 1: Table 1: Relationship between global contrast and corresponding fusion weight coefficients
[0023] As can be seen from Table 1 above, global contrast Increase The function output value increases. Increase the size, strengthen the integration, enhance the strength, and highlight the details.
[0024] The second image is subjected to Gaussian filtering using a 5x5 Gaussian kernel to generate the third image. Then, feature fusion is performed between the second and third images based on the fusion weight coefficients to obtain the enhanced image. The formula used is as follows: ; in, Indicates the position of the second image Pixel value at; Indicates the location of the enhanced image. Pixel value at; Indicates the position of the third image Pixel value at; In the above process, a 5×5 Gaussian convolution kernel is constructed. The weights at each position are calculated and normalized according to a two-dimensional Gaussian distribution. The center of the Gaussian convolution kernel has the highest weight, and the weights decrease symmetrically around the edges. The kernel is then convolved pixel by pixel with the second image. For each pixel, the pixel value in its 5×5 neighborhood is taken, multiplied by the corresponding weight in the kernel, and then summed. The result is used as the pixel value of the corresponding position in the third image. High-frequency noise is suppressed by local weighted averaging while low-frequency main features are preserved, providing a smooth base for subsequent feature fusion.
[0025] By weighted fusion of the original image and the Gaussian-filtered image, a synergistic optimization of high-frequency details and low-frequency smoothness is achieved. Gaussian filtering effectively suppresses noise and illumination but blurs details such as fruit texture. The original image retains sharp edges but contains noise interference. The weighting coefficients... In high-contrast scenes, such as when the fruit and background are clearly distinguishable, the enhancement algorithm tends to preserve the details of the original image, highlighting the texture and contours of the fruit peel. In low-contrast scenes, it focuses on the smoothness of the third image, suppressing noise while maintaining a natural transition of the image and avoiding distortion caused by over-sharpening.
[0026] Through weighting coefficients The original image and the Gaussian-filtered image are dynamically fused to generate optimized pixel values. Preserve high-frequency details of the original image. Noise is suppressed by Gaussian filtering. When the value is high, the focus is on the second image pixel value, enhancing edges and textures to improve fruit recognition. At lower values, the focus is on the third image pixel values to suppress noise and smooth areas of uneven lighting. Increase The increased weighting results in richer details in the output image. Decrease The weighting is improved, resulting in a smoother output image with lower noise. The specific steps for outputting the feature map of the image to be recognized are as follows: The YOLOv8n network consists of a backbone convolutional layer and a C2f module group. The specific steps for obtaining the output feature map are as follows: The size of the YOLOv8n convolution is [value missing]. In the backbone convolutional layer, a preliminary feature map is obtained, and then the preliminary feature map is input into the improved C2f module group in YOLOv8n to obtain the output feature map; The improved C2f module includes a dual-branch structure, namely... Separate convolution and The dilated convolution takes the initial feature map as input into the dual-branch structure. The feature maps output by the dual branches are added element-wise to generate a multi-scale fused feature map. The multi-scale fused feature map is then aligned with the initial feature map according to the channels and added element-wise again to obtain the output feature map.
[0027] In the above process, the multi-scale fused feature map is superimposed with the preliminary features output by the backbone convolutional layer after channel alignment, achieving complementary enhancement of cross-layer features. The combination of low-level features extracted by the 3×3 backbone convolution, such as fruit outlines, with high-level semantic features such as texture distribution generated by the improved C2f module significantly improves adaptability to complex scenes with overlapping fruits and occlusion of branches and leaves.
[0028] The improved C2f module extracts feature information at different scales through a dual-branch structure, namely 3×3 separable convolution and 5×5 dilated convolution in parallel. Separable convolution reduces the number of parameters and computational cost while preserving local details such as the subtle texture of the pomegranate fruit surface. Dilated convolution expands the receptive field to capture a wider range of contextual information, such as the spatial relationship between the fruit and the branches. The feature maps output by the dual branches are fused element-wise to achieve multi-scale fusion, which enhances the sensitivity to small targets such as distant fruits and blurred edges, and avoids the limitations of a single convolution kernel. Furthermore, residual connection is performed between the fused features and the initial feature map, which can effectively alleviate the gradient vanishing problem, improve training stability, and accelerate model convergence.
[0029] S3: Perform global average pooling and convolution on the output feature map to obtain the channel attention map and spatial attention map. Use element-wise multiplication to fuse the output feature map, channel attention map and spatial attention map to obtain the fused image. The specific steps for obtaining the fused image are as follows: Global average pooling and convolution are performed on the output feature maps to obtain channel attention maps and spatial attention maps: ; ; in, Indicates global average pooling; Represents the channel attention map; Represents a spatial attention map; This indicates that the data is spliced according to the channel dimension; This represents the weight matrix of the first fully connected layer; Represents the ReLU activation function; This represents the weight matrix of the second fully connected layer; Represents the sigmoid function; This indicates that the maximum value is taken along the channel dimension; This indicates that the mean value is taken along the channel dimension; express Convolutional layer; This represents the output feature map; In the above process, global average pooling extracts channel-level statistical features, such as the high-saturation red channel of pomegranate fruit, with higher weights. This is combined with fully connected layers and nonlinear... and Mapping dynamically enhances key channels related to fruit texture and color, suppressing interference from irrelevant background elements such as branches, leaves, or shadows; the maximum channel dimension highlights significant areas, such as the main body of the fruit under light, while the mean is used to smooth background noise. The two are then combined... Convolution captures a wide range of spatial dependencies, accurately locates target boundaries, and mitigates false responses in areas with uneven lighting or blurriness. The two processes work together to optimize the feature map weight distribution, significantly improving the robustness and localization accuracy of fruit detection in complex scenes.
[0030] The output feature map, channel attention map, and spatial attention map are fused using element-wise multiplication to obtain the fused image. ; in, This represents the merged image; This indicates the element-wise multiplication method.
[0031] In the above process, channel attention Dynamically calibrating channel weights, for example, significantly enhancing the red channel of the fruit to distinguish the background, spatial attention. By focusing on key areas such as fruit edges and occlusion boundaries, the two work together to significantly improve the contrast and semantic consistency of target features through pixel-by-pixel modulation. In complex scenes such as dense foliage or uneven lighting, this fusion strategy effectively suppresses background interference while enhancing fruit texture, color, and spatial distribution features, thereby improving the model's sensitivity to detecting low-contrast targets and small-sized fruits.
[0032] S4: Extract the values of all channels in the fused image at each pixel point to form a multi-dimensional vector at the pixel point. Flatten the multi-dimensional vector into a one-dimensional form to form a local feature vector for each pixel point. Use a point-by-point linear classification method to classify the local feature vector into two categories: no pomegranate fruit and pomegranate fruit. Calculate the probability percentage of pixels in the pomegranate fruit category. The specific steps for calculating the probability percentage of pixels belonging to the pomegranate fruit category are as follows: The values of all channels in the fused image are extracted at each pixel to form a multidimensional vector at that pixel. This multidimensional vector is then flattened into a one-dimensional form to form the local feature vector for each pixel. A point-by-point linear classification method is then used to classify the local feature vectors. ; in, Indicates the position The eigenvector at that location is flattened into a one-dimensional vector. Indicates the first The classification weight vector of each category; Indicates the first Bias terms for each category; Indicates position The pixel at point belongs to the first Confidence level of each category; Indicates the number of category indexes. , This indicates that there are no pomegranate fruits. This indicates that it belongs to the pomegranate fruit; In the above process, the flattening operation of the fused feature map at each pixel preserves local discriminative features. The linear classifier explicitly maps the features through the weight vector and, combined with the bias term, corrects inter-class imbalances, such as in scenarios where background pixels dominate. It directly outputs the confidence score of a pixel belonging to a pomegranate fruit, avoiding spatial information loss caused by global pooling and significantly improving the detection consistency of small targets.
[0033] Calculate the probability percentage of pixels that are pomegranate fruit type; ; in, Indicates position The probability that a pixel belongs to the fruit category; Indicates position The confidence level of a pixel belonging to a pomegranate fruit.
[0034] In the above process, the confidence difference between pomegranate fruit and background is amplified by an exponential function to generate normalized probability, effectively eliminating the difference in confidence scale between classes, avoiding the detail blurring caused by global pooling, and significantly improving the segmentation consistency of small targets; This represents the normalized probability that a pixel belongs to the pomegranate fruit category. Its technical effect lies in... Probability normalization will increase confidence level Mapping to a probability distribution eliminates class imbalance. and A positive correlation indicates a higher confidence level and a greater probability.
[0035] S5: Traverse all pixels and count the number of pixels with a probability of containing pomegranate fruit that is greater than a preset threshold and mark them as valid pixels. Use the density threshold method to calculate the proportion of valid pixels. When the proportion is greater than the preset density threshold, it is determined that there is fruit.
[0036] The specific steps to determine the presence of pomegranate fruit are as follows: Iterate through all pixels and count the number of pixels whose probability of containing pomegranate fruit is greater than a preset threshold, marking them as valid pixels: ; in, Indicates an indicator function; Indicates the number of valid pixels; This indicates the percentage of preset threshold values; Indicates the width of the scaled image; Indicates the height of the scaled image; In the above process, based on pixel-level probability distribution Valid pixels are filtered using an indicator function and a local threshold. Filter out low-confidence pixels and retain high-confidence fruit areas; this provides a basis for judging subsequent fruits. The proportion of effective pixels is calculated using a density threshold method. If the proportion exceeds a preset density threshold, then fruit is considered to exist. Specifically: ; in, This indicates the presence of pomegranate fruit. This indicates that there are no pomegranate fruits. This indicates the preset density threshold.
[0037] In the above process, This represents the binarization result of pomegranate fruit, using the density thresholding method to determine the proportion of effective pixels. With global threshold In comparison, the impact of small-area noise, such as localized red interference from branches and leaves, on the judgment is eliminated, and the global significance of fruit distribution is quantified; for example, if the proportion of densely fruited areas meets the standard, it is judged as present. Directly by The number of pixels determines, Threshold control is used for local probability screening; for example, the probability of fruit edge needs to be higher than that of background noise. Normalization ensures scale invariance in the determination of images at different resolutions. The preset density threshold dynamically adapts to the complexity of the scene. and A positive correlation is observed; the more effective pixels there are, the greater the probability of determining the presence of fruit. and They are negatively correlated; the higher the density threshold, the stricter the judgment condition, meaning a higher proportion is required to trigger the existence judgment. In the above process, The method for obtaining the data is as follows: based on the statistical analysis of the pixel-level probability values of pomegranate fruit regions in the sample set. We select its quantile, such as the 95th percentile, as the threshold to ensure that low-confidence pixels are filtered out while retaining high-probability fruit features. The acquisition method is as follows: dynamically calibrate according to the scene complexity. For single fruit detection in the near scene, i.e., the fruit area is dense, set a lower threshold such as 0.1. For multi-occluded scenes, i.e. the fruit is sparsely distributed, increase the threshold such as 0.3. Statistically analyze the distribution of effective pixel density through actual measurement data, and select the optimal value in combination with the actual precision curve.
[0038] The above formulas are all dimensionless calculations. The formulas are derived from software simulations based on a large amount of collected data to obtain the most recent real-world results. The preset parameters in the formulas are set by those skilled in the art according to the actual situation.
[0039] The above embodiments can be implemented, in whole or in part, by software, hardware, firmware, or any other combination thereof. When implemented in software, the above embodiments can be implemented, in whole or in part, as a computer program product. Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented by electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution.
[0040] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment, depending on actual needs.
[0041] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any changes or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application.
Claims
1. A fruit recognition method based on an improved YOLOv8n network, characterized in that, include: S1: Calculate the average diameter of mature pomegranate fruits and obtain the image to be identified. Based on the average diameter, adaptively scale the image to be identified to obtain a second image. Calculate and sort the red channel values of all pixels in the second image to obtain a first set. Traverse the first set to obtain the red channel values of the set quantiles and further obtain the global contrast. S2: Generate the fusion weight coefficients of the second image by nonlinear mapping based on global contrast. Generate the third image by Gaussian filtering the second image. Perform feature fusion on the second and third images based on the fusion weight coefficients to obtain the enhanced image. Input the enhanced image into the YOLOv8n network and output the output feature map of the image to be recognized. S3: Perform global average pooling and convolution on the output feature map to obtain the channel attention map and spatial attention map. Use element-wise multiplication to fuse the output feature map, channel attention map and spatial attention map to obtain the fused image. S4: Extract the values of all channels in the fused image at each pixel point to form a multi-dimensional vector at the pixel point. Flatten the multi-dimensional vector into a one-dimensional form to form a local feature vector for each pixel point. Use a point-by-point linear classification method to classify the local feature vector into two categories: no pomegranate fruit and pomegranate fruit. Calculate the probability percentage of pixels in the pomegranate fruit category. S5: Traverse all pixels and count the number of pixels with a probability of containing pomegranate fruit that is greater than a preset threshold and mark them as valid pixels. Use the density threshold method to calculate the proportion of valid pixels. When the proportion is greater than the preset density threshold, it is determined that there is fruit.
2. The fruit recognition method based on the improved YOLOv8n according to claim 1, characterized in that, The specific steps for obtaining the second image by adaptively scaling the image to be recognized based on the average diameter are as follows: Pomegranate fruits were selected as the sample set. The average diameter of all pomegranate fruits in the sample set was measured, and a scaling factor for the image to be identified was constructed based on the following logic: ; in, Indicates the scaling factor; and These represent the height and width of the image to be recognized, respectively. This indicates the average diameter of a pomegranate fruit; Indicates the target size of the pomegranate fruit in the image; This represents the maximum side length limit of the target area in the pomegranate fruit image; Multiply the scaling factor by the height and width of the image to be recognized respectively, and then round down to obtain the height and width of the scaled image.
3. The fruit recognition method based on the improved YOLOv8n network according to claim 2, characterized in that, The logic for obtaining the second image is as follows: When the scaling factor is less than or equal to 1, the scaled image is directly output as the second image; When the scaling factor is greater than 1, the position of the current pixel is determined based on the height and width of the scaled image. If the position of the current pixel is within the coverage area of the scaled image, that is, within the height range of the scaled image in the vertical direction and within the width range of the scaled image in the horizontal direction, then the pixel value of the scaled image at the corresponding position is directly adopted. For other non-covered areas, a specific grayscale value is uniformly filled. The grayscale value is generated by: statistically analyzing the red channel values of all pixels in the original image, calculating the average of the sum of the mean and median of all pixels in the red channel, and multiplying the average by a preset brightness attenuation coefficient to obtain the brightness value to be filled.
4. The fruit recognition method based on the improved YOLOv8n network according to claim 1, characterized in that, The specific steps for generating the enhanced image are as follows: The red channel values of all pixels in the second image are statistically analyzed and sorted to obtain the first set. The red channel values at a set quantile are then obtained from this first set. Furthermore, the global contrast is obtained, and a non-linear mapping is performed based on the global contrast to generate the fusion weight coefficients for the second image. ; in, ; This represents the preset weight parameters; Indicates global contrast; The red channel value represents the 85th percentile. The red channel value represents the 15th percentile. The second image is subjected to Gaussian filtering using a 5x5 Gaussian kernel to generate the third image. Then, feature fusion is performed between the second and third images based on the fusion weight coefficients to obtain the enhanced image. The formula used is as follows: ; in, Indicates the position of the second image Pixel value at; Indicates the location of the enhanced image. The pixel value at that location.
5. The fruit recognition method based on the improved YOLOv8n network according to claim 1, characterized in that, The specific steps for outputting the feature map of the image to be recognized are as follows: The YOLOv8n network consists of a backbone convolutional layer and a C2f module group. The specific steps for obtaining the output feature map are as follows: The size of the YOLOv8n convolution is [value missing]. In the backbone convolutional layer, a preliminary feature map is obtained, and then the preliminary feature map is input into the improved C2f module group in YOLOv8n to obtain the output feature map; The improved C2f module includes a dual-branch structure, namely... Separate convolution and The dilated convolution takes the initial feature map as input into the dual-branch structure. The feature maps output by the dual branches are added element-wise to generate a multi-scale fused feature map. The multi-scale fused feature map is then aligned with the initial feature map according to the channels and added element-wise again to obtain the output feature map.
6. The fruit recognition method based on the improved YOLOv8n network according to claim 5, characterized in that, The specific steps for obtaining the fused image are as follows: Global average pooling and convolution are performed on the output feature maps to obtain channel attention maps and spatial attention maps: ; ; in, Indicates global average pooling; Represents the channel attention map; Represents a spatial attention map; This indicates that the data is spliced according to the channel dimension; This represents the weight matrix of the first fully connected layer; Represents the ReLU activation function; This represents the weight matrix of the second fully connected layer; Represents the sigmoid function; This indicates that the maximum value is taken along the channel dimension; This indicates that the mean value is taken along the channel dimension; express Convolutional layer; This represents the output feature map; The output feature map, channel attention map, and spatial attention map are fused using element-wise multiplication to obtain the fused image. ; in, This represents the merged image; This indicates the element-wise multiplication method.
7. The fruit recognition method based on an improved YOLOv8n network according to claim 1, characterized in that, The specific steps for calculating the probability percentage of pixels belonging to the pomegranate fruit category are as follows: The values of all channels in the fused image are extracted at each pixel to form a multidimensional vector at that pixel. This multidimensional vector is then flattened into a one-dimensional form to form the local feature vector for each pixel. A point-by-point linear classification method is then used to classify the local feature vectors. ; in, Indicates the position The eigenvector at that location is flattened into a one-dimensional vector. Indicates the first The classification weight vector of each category; Indicates the first Bias terms for each category; Indicates position The pixel at point belongs to the first Confidence level of each category; Indicates the number of category indexes. , This indicates that there are no pomegranate fruits. This indicates that it belongs to the pomegranate fruit; Calculate the probability percentage of pixels that are pomegranate fruit type; ; in, Indicates position The probability that a pixel belongs to the fruit category; Indicates position The confidence level of a pixel belonging to a pomegranate fruit.
8. The fruit recognition method based on the improved YOLOv8n network according to claim 1, characterized in that, The specific steps to determine the presence of pomegranate fruit are as follows: Iterate through all pixels and count the number of pixels whose probability of containing pomegranate fruit is greater than a preset threshold, marking them as valid pixels: ; in, Indicates an indicator function; Indicates the number of valid pixels; This indicates the percentage of preset threshold values; Indicates the width of the scaled image; Indicates the height of the scaled image; The proportion of effective pixels is calculated using a density threshold method. If the proportion exceeds a preset density threshold, then fruit is considered to exist. Specifically: ; in, This indicates the presence of pomegranate fruit. This indicates that there are no pomegranate fruits. This indicates the preset density threshold.