A dense edible mushroom target detection post-processing device and method
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-07-15
- Publication Date
- 2026-08-11
AI Technical Summary
在食用菌密集生长场景中,目标固有的高重叠度使得固定阈值的非极大值抑制极易将相邻真实个体误判为同一目标的重复检测,造成误删;而分割掩码的生成与边界细化若缺乏并行加速,单帧处理延迟将超过百毫秒,难以满足智能采收与高速分拣对实时控制的要求
[0034]本发明通过将候选结果接收缓存、局部密集度计算、重叠关系矩阵并行计算、自适应候选抑制、实例掩码边界修正及粘连目标拆分固化为硬件流水线,直接承接前级检测模型输出的候选框和实例掩码,在片上缓存与并行计算资源的协同下完成全部后处理操作,消除了软件串行执行NMS和掩码处理时反复访问外部存储器带来的调度开销与延迟瓶颈。其中,自适应候选抑制单元引入局部密集度值、候选框面积、类别置信度值及与最高置信度候选中心点的欧氏距离四个变量,为每一候选目标动态生成抑制阈值,以该动态阈值替代固定阈值执行去重抑制,使得在密集丛生区域自动升高抑制阈值以保留相邻真实个体,在稀疏区域自动降低阈值以有效剔除重复候选,从而减少密集个体间的误删。实例掩码边界修正单元依据菌盖轮廓凹陷点几何特征和相邻掩码交叠区域沿凹陷点对之间的最短路径重划分边界,粘连目标拆分单元通过连通域标记和距离变换将粘连连通域分割为独立个体掩码,二者协同减少密集个体间的误合并与边界错分,提升实例掩码边界清晰度与个体计数准确性,为食用菌智能采收与自动分拣提供低延迟、高精度的检测后处理能力。
Smart Images

Figure CN122550918A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of target detection, and in particular relates to a post-processing device and method for dense edible fungi target detection. Background Technology
[0002] In scenarios such as intelligent harvesting, online sorting, quality grading, and safety control of edible fungi, visual perception systems need to identify species, locate morphology, determine maturity, and segment instances from densely clustered fungi. Deep learning object detection models, especially the YOLOv8 series, have been introduced into these tasks due to their good balance between speed and accuracy. The candidate boxes, category confidence scores, instance masks, and feature map indexes output by these models form the basis for subsequent decision-making and control. However, edible fungi exhibit significant dense overlapping characteristics, with blurred boundaries between adjacent individuals and weak features in small mushroom buds. The candidate boxes output by general models are densely packed, and the mask contours are severely adhered. If directly used in harvesting robotic arms or sorting actuators, this can easily lead to missed detections, mis-grabbing, and individual damage.
[0003] In existing technologies, one type of detection method relies on spectral analysis or traditional image processing to determine the quality or growth status of edible fungi. For example, Chinese patent CN118533816B discloses a method for detecting the quality of edible fungi based on spectral characteristics. This method acquires Raman spectral data at different ambient temperatures, determines prediction weights by analyzing the intensity of light source fluctuations, and then obtains a standard Raman spectrum for quality detection. This method focuses on the spectral characterization of the sample's material composition, requires acquiring Raman spectral data and performing offline analysis, and cannot achieve real-time positioning and contour segmentation of dense fruiting bodies on planting or sorting lines. Another type of approach utilizes visible light images to analyze mycelial growth areas. For instance, Chinese patent CN117495802B proposes a method and device for detecting the mycelial growth of edible fungi. This method sets a grayscale threshold on the grayscale image of the cultivation box, extracts the edible fungi growth area and dominant growth area, and calculates the mycelial growth rate, density, and robustness accordingly. This approach focuses on the overall grayscale statistics of the mycelial region on the culture medium surface, and lacks the ability to detect and segment individual mature, densely clustered fruiting bodies. Therefore, none of the above methods address the post-processing requirement for refined target detection and masked segmentation of multiple adjacent, overlapping edible fungi individuals in densely growing scenarios.
[0004] With the development of edge deep learning inference chips, forward computations such as convolution, normalization, and activation of models like YOLOv8 can be pipelined and accelerated using dedicated neural network processors or field-programmable gate arrays. However, post-processing of the model output is usually still executed serially in software by embedded CPUs. The post-processing process includes candidate box sorting, intersection-over-union (IoU) calculation, non-maximum suppression (NUS), duplicate box removal, mask matrix generation, and contour boundary correction. When running on general-purpose processors, this requires frequent data transfer between external storage and on-chip cache, resulting in significant memory access latency and power consumption. In scenarios with dense edible mushroom growth, the inherent high overlap of targets makes it easy for fixed-threshold NUS suppression to misclassify adjacent real individuals as duplicate detections of the same target, leading to false deletions. Furthermore, without parallel acceleration, the generation of segmentation masks and boundary refinement can result in single-frame processing latency exceeding hundreds of milliseconds, making it difficult to meet the real-time control requirements of intelligent harvesting and high-speed sorting. Some literature proposes to use parallel sorting networks or hardware-based nonmaximum suppression modules to accelerate the post-processing of general target detection. However, these designs do not take into account the morphological priors such as the roundness of the cap and the direction of the stem of edible fungi fruiting bodies, nor do they make customized corrections for mask adhesion and boundary blurring caused by dense overlap. When directly transferred to edible fungi scenarios, the accuracy loss is significant.
[0005] In summary, the key technical problem that needs to be solved by existing technologies is: how to design a dedicated post-processing hardware device for the detection of densely clustered edible fungi. This device should be able to directly receive the candidate boxes and instance masks output by the detection model. Through the collaborative design of on-chip caching and parallel computing resources, it should realize parallel overlap analysis of candidate boxes, adaptive non-maximum suppression, mask boundary segmentation correction, and formatted output of results. This would eliminate the real-time bottleneck caused by serial execution and frequent memory access in software post-processing, while reducing erroneous deletion, merging, and boundary misclassification among dense individuals, thus providing low-latency and high-precision detection post-processing capabilities for intelligent edible fungi operation equipment. Summary of the Invention
[0006] To address the shortcomings of existing technologies, this invention proposes a post-processing device and method for detecting dense edible fungi targets. The device uses a candidate result receiving and caching unit to cache the candidate box coordinates, category confidence, instance mask, and target center point data output by the previous model in on-chip memory. After the local density calculation unit identifies dense regions based on the candidate box center distance, area, and overlap ratio, the overlap relationship matrix calculation unit efficiently calculates the intersection-union ratio, center distance, and mask overlap rate between targets within the dense region using a parallel comparison array. The adaptive candidate suppression unit dynamically generates suppression thresholds based on multi-dimensional parameters to accurately distinguish between duplicate detections and real individuals. Finally, through mask boundary correction and adhering target splitting optimization, accurate detection and segmentation data are output, significantly improving the detection accuracy in dense scenes.
[0007] To achieve the above objectives, the present invention provides the following technical solution:
[0008] A post-processing device for intensive edible fungi target detection includes:
[0009] The candidate result receiving and caching unit is used to receive candidate box coordinates, class confidence, instance mask and target center point from the front-end YOLOv8 customized inference model, and cache the received data in on-chip memory.
[0010] The local density calculation unit calculates the density of edible fungi targets in a local area based on the distance between the center of the candidate box, the area, and the overlap ratio, in order to identify dense areas;
[0011] The overlap relationship matrix calculation unit is used to calculate the cross-union ratio, center distance and mask overlap rate between each candidate target in the dense region by means of a parallel comparison array;
[0012] An adaptive candidate suppression unit is used to dynamically generate suppression thresholds for each candidate target based on the local density, candidate box size, class confidence and center point separation degree, and perform deduplication suppression on the candidate targets based on the dynamic suppression thresholds to distinguish duplicate detections from adjacent real individuals.
[0013] The instance mask boundary correction unit is used to re-divide the boundary of the instance mask corresponding to the preserved real individual based on the cap outline edge features, the overlapping area of adjacent masks, and the outline depression points to obtain the corrected mask;
[0014] The adhesion target splitting unit is used to split the adhesion mask into independent individual masks by connecting component labeling and distance transformation when the modified mask contains multiple connected edible fungus individuals;
[0015] The result output unit is used to output the target category, location coordinates, split individual mask, and defect identifier.
[0016] Specifically, the adaptive candidate suppression unit includes a threshold generation subunit and a suppression execution subunit. The threshold generation subunit is used to generate a dynamic suppression threshold for each candidate target based on the local density value of the region where the candidate target is located, the candidate box area of the candidate target, the category confidence value, and the Euclidean distance between the candidate target and the center point of the current highest confidence candidate target. The suppression execution subunit, for a first candidate target and a second candidate target that constitute a candidate target pair, obtains the intersection-union ratio or mask overlap rate of the candidate target pair from the overlap relationship matrix. When the intersection-union ratio or mask overlap rate is greater than the dynamic suppression threshold of the candidate target with lower confidence between the first candidate target and the second candidate target, the candidate target with lower confidence is suppressed; otherwise, both are retained, and the retained candidate targets are marked as real individuals and corresponding target identifiers are generated.
[0017] Specifically, the local density calculation unit includes a parallel distance calculation array, an area comparator array, an overlap determination array, a counter array, and a dense region determination module. The parallel distance calculation array is used to calculate the center Euclidean distance between the first candidate target and the second candidate target in parallel and output the distance calculation result. The area comparator array is used to calculate the ratio of the candidate box area of the first candidate target to the candidate box area of the second candidate target and output the area ratio result. The overlap determination array is used to calculate the overlap ratio between the candidate boxes of the first candidate target and the candidate boxes of the second candidate target and output the overlap ratio result.
[0018] Specifically, the counter array is used to count the number of second candidate targets that simultaneously meet the following conditions for the first candidate target: the center Euclidean distance is less than a preset neighborhood radius threshold, the area ratio result falls within a preset area ratio interval, and the overlap ratio result exceeds a preset overlap threshold, and this number is used as the number of neighboring candidate targets of the first candidate target; the dense region determination module is used to read the number of neighboring candidate targets of the first candidate target, and when the number of neighboring candidate targets exceeds a preset number threshold, the region where the first candidate target is located is marked as a dense region, and the dense region identifier is output to the overlap relationship matrix calculation unit.
[0019] Specifically, the instance mask boundary correction unit includes a contour tracking module, a curvature calculation module, and a boundary re-division module. The contour tracking module is used to extract the contour point sequence of the corresponding instance mask using the retained real individual identifier as input, and output the coordinate values of each contour point in the contour point sequence. The curvature calculation module is used to calculate the curvature value of each contour point in the contour point sequence based on the coordinate values of the contour point and the coordinate values of adjacent contour points, and detect contour points in the contour point sequence whose curvature values are greater than the curvature values of the adjacent two contour points as local maxima of curvature, and mark the local maxima of curvature as cap contour depression points.
[0020] Specifically, the boundary re-division module is used to receive the position information of the overlapping area of the adjacent masks. Within the overlapping area of the adjacent masks, a pair of concave points with the smallest spatial Euclidean distance is selected from the cap contour concave points belonging to the first instance mask and the cap contour concave points belonging to the second instance mask to form a concave point pair. The straight line segment between the concave point pair is used as a new dividing boundary to replace the original boundary of the first instance mask and the second instance mask in the overlapping area of the adjacent masks, and the corrected mask of the first instance mask and the second instance mask is output.
[0021] Specifically, the sticky target splitting unit includes a connected component marking module, a distance transformation module, and a watershed control module. The connected component marking module is used to receive the modified mask, perform connected component marking on the modified mask to obtain at least one connected component, calculate the area of each connected component, and when the area of any connected component is greater than a preset area threshold, mark the connected component as a sticky connected component and generate a splitting trigger signal. The distance transformation module is used to respond to the splitting trigger signal, perform Euclidean distance transformation on the sticky connected component, calculate the Euclidean distance from each pixel in the sticky connected component to the nearest background pixel, and generate a distance map.
[0022] Specifically, the watershed control module is used to obtain the target center point coordinates of multiple real individuals located within the cohesive connected domain and retained by the adaptive candidate suppression unit from the candidate result receiving cache unit, use the multiple target center points as seed points, perform watershed segmentation on the distance map, divide the cohesive connected domain into independent regions each containing a seed point, and output the split individual mask.
[0023] Specifically, the result output unit includes a data framing module; the data framing module is used to read the contour point sequence of the split individual mask, read the target category, position coordinates and defect identifier corresponding to the retained real individual, write the target category of the same retained real individual into the target category field of the data frame, write the position coordinates into the position coordinate field, write the contour point sequence into the mask data field, write the defect identifier into the defect identifier field, and write the frame header identifier field, frame length field and cyclic redundancy check field into the data frame in sequence to generate a formatted data frame.
[0024] Specifically, the local density calculation unit further includes a neighborhood radius adjustment module; the neighborhood radius adjustment module is used to read the candidate box area of the first candidate target from the candidate result receiving cache unit, use the candidate box area as the input address to query a preset area-radius mapping table, read the dynamic neighborhood radius threshold corresponding to the candidate box area from the area-radius mapping table, and write the dynamic neighborhood radius threshold into the neighborhood radius configuration register of the counter array; when the counter array counts the number of neighboring candidate targets for the first candidate target, it reads the dynamic neighborhood radius threshold from the neighborhood radius configuration register, compares the center Euclidean distance with the dynamic neighborhood radius threshold, and determines whether the second candidate target falls within the neighborhood range of the first candidate target.
[0025] Specifically, the watershed control module includes a seed point correction submodule; the input of the seed point correction submodule is connected to the output of the distance transformation module for receiving the distance map, and the input of the seed point correction submodule is also connected to the output port of the candidate result receiving buffer unit for obtaining the coordinates of the multiple target center points as initial seed points; the seed point correction submodule is used to define a rectangular search window on the distance map with the coordinates of the initial seed point as the center for each initial seed point, the width of the rectangular search window being a preset search window width value and the height being a preset search window height value. The system reads the distance values of all pixels within the rectangular search window, compares the distance values of all pixels, and determines the pixel with the largest distance value as the corrected seed point. The coordinates of this corrected seed point are then used as the coordinates of the corrected seed point. The watershed control module uses the coordinates of all corrected seed points as seed points and performs watershed segmentation on the distance map. When there are multiple pixels with equal and maximum distance values within the rectangular search window, the seed point correction submodule selects the pixel with the smallest row coordinate and column coordinate as the corrected seed point.
[0026] A post-processing method for dense edible fungi target detection includes:
[0027] The system receives candidate box coordinates, class confidence, instance mask, and target center point from the front-end YOLOv8 customized inference model and caches the received data in on-chip memory.
[0028] The density of edible fungi targets in a local area is calculated based on the distance between the center of the candidate boxes, the area, and the overlap ratio to identify dense areas;
[0029] Within the dense region, the cross-union ratio, center distance, and mask overlap rate among each candidate target are calculated using a parallel comparison array.
[0030] Based on the local density, candidate box size, category confidence and center point separation, a suppression threshold is dynamically generated for each candidate target, and deduplication suppression is performed on the candidate targets based on the dynamic suppression threshold to distinguish duplicate detections from adjacent real individuals;
[0031] For the instance mask corresponding to the preserved real individuals, the boundary is re-divided based on the cap outline edge features, the overlapping area of adjacent masks, and the outline depression points to obtain the corrected mask;
[0032] When the modified mask contains multiple connected edible fungus individuals, the adhesion mask is split into independent individual masks by connecting component labeling and distance transformation.
[0033] Compared with the prior art, the beneficial effects of the present invention are:
[0034] This invention integrates candidate result receiving and caching, local density calculation, parallel calculation of overlap relation matrix, adaptive candidate suppression, instance mask boundary correction, and sticky target splitting into a hardware pipeline. It directly receives candidate boxes and instance masks output from the previous detection model and completes all post-processing operations through on-chip caching and parallel computing resources. This eliminates the scheduling overhead and latency bottleneck caused by repeated access to external memory during software serial execution of NMS and mask processing. Specifically, the adaptive candidate suppression unit introduces four variables: local density value, candidate box area, class confidence value, and Euclidean distance to the center point of the highest confidence candidate. It dynamically generates a suppression threshold for each candidate target, replacing the fixed threshold for deduplication suppression. This automatically increases the suppression threshold in densely clustered regions to retain adjacent real individuals and automatically decreases the threshold in sparse regions to effectively eliminate duplicate candidates, thereby reducing false deletions among dense individuals. The instance mask boundary correction unit re-divides the boundary based on the geometric features of the cap contour depression points and the shortest path between the depression point pairs in the overlapping areas of adjacent masks. The adhesion target splitting unit divides the adhesion connected domain into independent individual masks through connected component labeling and distance transformation. The two work together to reduce the erroneous merging and boundary misclassification between dense individuals, improve the clarity of instance mask boundaries and the accuracy of individual counting, and provide low-latency, high-precision detection and post-processing capabilities for intelligent harvesting and automatic sorting of edible fungi. Attached Figure Description
[0035] Figure 1 This is a schematic diagram of a post-processing device for dense edible fungi target detection according to Embodiment 1 of the present invention;
[0036] Figure 2 This is an execution flowchart of the local density calculation unit in Embodiment 2 of the present invention;
[0037] Figure 3 This is an execution flowchart of the mask boundary correction unit in Embodiment 2 of the present invention;
[0038] Figure 4 This is a flowchart of a method for post-processing of dense edible fungi target detection according to Embodiment 3 of the present invention. Detailed Implementation
[0039] Example 1
[0040] Please see Figure 1 The present invention provides an embodiment of a post-processing device for dense edible fungi target detection, comprising the following steps:
[0041] The candidate result receiving and caching unit is used to receive candidate box coordinates, class confidence, instance mask and target center point from the front-end YOLOv8 customized inference model, and cache the received data in on-chip memory.
[0042] The local density calculation unit calculates the density of edible fungi targets in a local area based on the distance between the center of the candidate box, the area, and the overlap ratio, in order to identify dense areas;
[0043] The overlap relationship matrix calculation unit is used to calculate the cross-union ratio, center distance and mask overlap rate between each candidate target in the dense region by means of a parallel comparison array;
[0044] An adaptive candidate suppression unit is used to dynamically generate suppression thresholds for each candidate target based on the local density, candidate box size, class confidence and center point separation degree, and perform deduplication suppression on the candidate targets based on the dynamic suppression thresholds to distinguish duplicate detections from adjacent real individuals.
[0045] The instance mask boundary correction unit is used to re-divide the boundary of the instance mask corresponding to the preserved real individual based on the cap outline edge features, the overlapping area of adjacent masks, and the outline depression points to obtain the corrected mask;
[0046] The adhesion target splitting unit is used to split the adhesion mask into independent individual masks by connecting component labeling and distance transformation when the modified mask contains multiple connected edible fungus individuals;
[0047] The result output unit is used to output the target category, location coordinates, split individual mask, and defect identifier.
[0048] In this embodiment, the pre-stage YOLOv8 customized inference model refers to a dedicated detection model developed by those skilled in the art, based on the network topology of the open-source YOLOv8-seg instance segmentation model. This model addresses specific domain-specific issues in densely clustered edible fungi environments, such as high overlap rates between adjacent sub-entities, low pixel proportions of small mushroom buds, and varied cap and stem morphologies. It involves targeted network structure modification, training on a dedicated dataset, INT8 quantization compression, and hardware instruction mapping, before being solidified and deployed in the pre-stage inference accelerator. The complete construction and deployment process of the pre-stage YOLOv8 customized inference model in this embodiment includes:
[0049] First, during the dataset construction phase, those skilled in the art used color industrial cameras at mushroom cultivation houses and sorting lines to collect visible light images of common edible fungi such as shiitake, oyster, enoki, king oyster, and button mushrooms during their primordia differentiation, immature, and mature stages under typical lighting conditions including top lighting, side lighting, and shading. The image acquisition resolution was set to 1920×1080 pixels. For each original image, an instance segmentation and annotation tool was used to annotate each identifiable individual edible fungi in the image. The annotation included: labeling the target bounding box with the minimum bounding rectangle, labeling the pixel-level instance mask point by point with polygons, and assigning a label to the variety category and four defect labels: normal, moldy, insect-infested, and mechanically damaged. An edible fungi instance segmentation dataset containing no less than 5000 annotated images was constructed and divided into a training set, a validation set, and a test set in an 8:1:1 ratio.
[0050] Secondly, during the network architecture transformation phase, based on the YOLOv8-seg network topology, three targeted improvements were implemented. These are as follows:
[0051] Firstly, a small target detection layer is added. Based on the existing three detection layers in the feature pyramid network—P3 (8x downsampling, input feature map size 80 pixels × 80 pixels), P4 (16x downsampling, input feature map size 40 pixels × 40 pixels), and P5 (32x downsampling, input feature map size 20 pixels × 20 pixels)—a P2 detection layer is added. This P2 detection layer takes the 4x downsampling feature map output from the second stage of the backbone network as input. This feature map has a size of 160 pixels × 160 pixels and 128 channels. After being compressed to 64 dimensions by a 1×1 convolution, it is fed into the detection head, outputting candidate box predictions and mask coefficient predictions for this scale. The P2 detection layer has three preset anchor frame sizes: 16 pixels × 16 pixels, 32 pixels × 32 pixels, and 64 pixels × 64 pixels. These are specifically designed to detect small mushroom buds with cap outer rectangles ranging from 5 to 15 pixels, thereby improving the detection and recall rate of young fruiting bodies in dense scenes. In this embodiment, the second stage of the backbone network specifically corresponds to the output of the second C2f module in the YOLOv8-seg backbone network. This module is located after the first 3×3 downsampling convolutional layer with a stride of 2. The downsampling factor of its output feature map is strictly four times that of the input image, and the number of channels is fixed at 128 dimensions. Those skilled in the art can accurately locate this feature extraction node based on the network topology structure officially released by YOLOv8.
[0052] Secondly, an edible fungus feature attention module is inserted. After the feature fusion module of the neck network completes top-down and bottom-up multi-scale feature fusion and before each layer of the detection head, an edible fungus feature attention module is inserted. This attention module consists of a channel attention branch and a spatial attention branch in parallel. The channel attention branch performs global average pooling on the input feature map, compressing the feature map of size H×W×A into a 1×1×A channel description vector. Then, it passes through a fully connected layer to compress the number of channels to A / 16, a ReLU activation layer, a second fully connected layer to restore the number of channels to A, and a Sigmoid activation layer, outputting a channel weight vector of length A. The spatial attention branch performs global average pooling and global max pooling on the input feature map along the channel direction, respectively, to obtain an average pooling map and a max pooling map of size H×W×1. These two are concatenated along the channel direction to form a tensor of size H×W×2, which is then compressed to 1 through a 7×7 convolutional layer, and finally output as a spatial weight map of size H×W×1 through a Sigmoid activation function. The original input feature map is weighted by element-wise multiplication of the channel weight vector and the spatial weight map, enhancing the semantic response intensity of the cap edge contour region and the stipe connection region, while suppressing background noise in the culture medium texture and shadow regions. In this embodiment, H×W represents the spatial resolution (i.e., the product of height H and width W) of the feature map output by the preceding YOLOv8 customized inference model during the feature extraction stage. It represents the spatial dimension at which the model perceives the input image in a gridded manner at this scale. In this embodiment, variable A represents the channel dimension of the input feature map of the corresponding detection layer; where P is the layer identifier of the feature pyramid network, and the suffix number represents the downsampling factor of the feature layer relative to the input image, with the value being a base-2 exponential value, i.e., layer P2 corresponds to 4x downsampling, layer P3 corresponds to 8x downsampling, layer P4 corresponds to 16x downsampling, and layer P5 corresponds to 32x downsampling; the channel dimensions A corresponding to each layer are as follows: layer P2 corresponds to A=64, layer P3 corresponds to A=128, layer P4 corresponds to A=256, and layer P5 corresponds to A=512; the feature fusion module of the neck network is specifically the final output node of the bottom-up fusion branch in the path aggregation network, and each detection head corresponds to an independent edible fungus feature attention module, and the channel dimension of each module is strictly matched with the input dimension of the corresponding detection layer.
[0053] Third, replace the bounding box regression loss function. Using C... IoU The loss function replaces G in the original bounding box regression branch. IoU Loss function. (C) IoU The expression for calculating the loss function is: LC IoU =1-IoU+ρ²(b,bgt) / c²+α vWhere IoU is the intersection-union ratio of the predicted bounding box and the ground truth bounding box, ρ²(b,bgt) is the squared Euclidean distance between the center point b of the predicted box and the center point bgt of the ground truth box, c is the diagonal length of the smallest bounding rectangle that simultaneously encloses the predicted box and the ground truth box, and α v The weighting coefficient, α, is used to balance the order of magnitude difference between the aspect ratio penalty term, the IoU term, and the center distance term. v =v / (1-IoU+v), where v is the penalty factor for measuring the consistency of the aspect ratio between the predicted and ground truth bounding boxes, defined as v=(4 / π²)(arctan(wgt / hgt)-arctan(w / h))², where w, h and wgt, hgt are the width and height of the predicted and ground truth bounding boxes, respectively. The total loss function during training is set to L. total =λ cls L cls +λ box LC IoU +λ mask L mask L cls For classification loss, LC IoU C IoU Bounding box regression loss, L mask The binary cross-entropy masking loss has weight coefficients λ. cls Take 1.0, λ box Take 7.5, λ mask We set the initial learning rate to 0.5. We used a stochastic gradient descent optimizer, with an initial learning rate of 0.01, a momentum factor of 0.937, and a weight decay coefficient of 5 × 10⁻⁶. -4 The training process iterates for 300 rounds on the training set, calculating the mask IoU metric every 10 rounds on the validation set. After training, the weight file with the highest mask IoU on the validation set is selected as the training output. In this embodiment, the classification loss L... cls A binary cross-entropy loss function is used to independently calculate the confidence loss for each category; the masking segmentation loss L... mask The binary cross-entropy is calculated pixel-by-pixel between the upsampled instance mask and the real mask based on a resolution of 160×160. The training process uses the gradient descent backpropagation algorithm, with the gradient clipping threshold set to 10.0 and the batch size set to 16, to ensure that those skilled in the art can completely reproduce the model training process based on the above parameters.
[0054] In this embodiment, the weight coefficients of various loss types are set to λ during the training phase. cls =1.0、λ box =7.5、λ mask=0.5, the basis for which is that: the intelligent harvesting and sorting of edible fungi requires extremely high spatial positioning accuracy of the sub-entities, and the candidate boxes of adjacent individuals overlap significantly under dense clustering conditions. Therefore, the bounding box regression loss weight λ is set at 0.5. box The maximum value of 7.5 is set to enhance the model's ability to fit the position and size of the cap, ensuring that it converges to the accurate bounding box coordinates during repeated iterations, providing a reliable positioning basis for subsequent robotic arm grasping; simultaneously, the category confidence loss weight λ cls Set to 1.0 as the baseline balancing term to ensure the model maintains sufficient discriminative power when distinguishing between different varieties and defect categories. Since the localization task weights are already dominant, there's no need to excessively amplify the classification loss to maintain classification performance; while the instance mask segmentation loss weight λ... mask The weight is set to 0.5 because the device of this invention has specially set up a mask boundary correction unit and a sticky target splitting unit in the post-processing stage, which can perform fine secondary correction and independent splitting of the basic mask. Therefore, it is not necessary to pursue the absolute accuracy of the pixel-level mask excessively during the training stage, so as to avoid the mask segmentation loss weight being too high, which would cause the model to overfit the details in the early stage of training and impair the overall feature extraction and localization ability. At the same time, it can ensure that the model generates instance masks with sufficient initialization information. After the post-processing unit performs collaborative optimization, high-precision segmentation of the target can be achieved. The above weight ratio is determined based on the actual test results of recall and precision in dense scenes on the validation set, achieving the optimal performance balance among the three tasks of classification, localization and masking.
[0055] Secondly, in the quantization deployment phase, the weight file output from training is quantized using the post-training static quantization method with INT8 quantization. The specific steps are as follows:
[0056] The first step involves those skilled in the art randomly selecting 100 representative edible fungi images from the training set as a calibration dataset. The selected images cover different edible fungi varieties, different growth stages, and different density scenarios to ensure that the calibration data can fully characterize the numerical distribution range of the input activation values during model inference.
[0057] The second step involves feeding the images from the calibration dataset frame by frame into the FP32 precision model output from the training dataset for forward inference. An activation value observer is inserted at the output of each convolutional layer to record the global maximum (x_max) and global minimum (x_min) of the activation values at that layer's output. Histogram statistics are then performed on the activation values using 256 equal-width intervals. For each layer's weight tensor, its FP32 format weight values are read, and the global maximum (w_max) and global minimum (w_min) of the weights at that layer are recorded.
[0058] The third step involves using an asymmetric quantization scheme for the activation tensor. The formulas for calculating the quantization zero-point value z_a and the scale factor s_a are: z_a = round(0 - x_min / ((x_max - x_min) / 255)), s_a = (x_max - x_min) / 255, where round represents rounding to the nearest integer, and the value of z_a is limited to the range of 0 to 255. For the weight tensor, a symmetric quantization scheme is used, with the scale factor s_w = max(|w_max|,|w_min|) / 127, and the zero-point value z_w fixed at 0. Based on the above formulas, the scale factor and zero-point value of the weight tensor and activation tensor of each convolutional layer are calculated layer by layer to generate the quantization parameter file. In this embodiment, the calculation logic for the quantization zero-point value z_a is as follows: first, calculate the dynamic range of the activation value x_max-x_min, then calculate the numerical span corresponding to a single quantization step size as (x_max-x_min) / 255, and finally take the integer encoding position mapped to the value 0 as the zero point. The calculation process strictly follows the order of operations of division before subtraction. When the activation value exceeds the range of [x_min, x_max], it is directly truncated to the quantization encoding corresponding to the boundary value to avoid overflow errors.
[0059] The fourth step involves reading the FP32 format weight file. For each layer's weight value w_fp32, the 32-bit floating-point value is mapped to an 8-bit signed integer value using the formula w_int8 = round(w_fp32 / s_w) + z_w, where w_int8 ranges from -128 to 127. For each layer's activation value a_fp32, the 32-bit floating-point value is mapped to an 8-bit unsigned integer value using the formula a_int8 = round(a_fp32 / s_a) + z_a, where a_int8 ranges from 0 to 255. After completing the quantization mapping of all layer weights and activation values, the INT8 quantized model is obtained. In the hardware-accelerated inference scenario for dense edible fungus target detection, w_int8 and a_int8 respectively represent the low-precision fixed-point representation of the weight parameters and intermediate feature activation values of each convolutional layer in the preceding YOLOv8 customized inference model after INT8 quantization. Specifically, w_int8 is an 8-bit signed integer that maps the original FP32 format convolutional kernel weights to the range [-128, 127] through symmetric quantization (scale factor s_w and zero point z_w are fixed to 0). It is directly stored in the on-chip weight memory and serves as a fixed coefficient for multiplication and addition operations of the processing unit array. While retaining the ability to extract key semantic features such as the edge of the cap and the connection of the stem of edible fungi, the weight storage space is compressed to one-quarter of the original. a_int8 is an 8-bit unsigned integer that maps the feature map activation value of each layer of convolution output in the forward inference of the model to the range [0, 255] through asymmetric quantization (scale factor s_a and zero point z_a). It represents the spatial feature response intensity passed to the next layer after normalization and activation function. In the context of densely clustered scenarios described in this application, where adjacent sub-entities overlap significantly and small mushroom buds have an extremely low pixel ratio, fixed-point multiplication and addition instructions are executed on a dedicated processing unit array using w_int8 and a_int8. This not only avoids the high latency and high power consumption caused by FP32 floating-point operations at the edge, but also enables the convolution-normalization fusion operation to be completed efficiently in an integer pipelined parallel manner. This ensures that the front-end inference model outputs candidate boxes and instance mask coefficients within 8 milliseconds, providing a stable and compact data input foundation for the low-latency pipeline cascading of the post-processing device. As a result, high-precision, high-throughput real-time target detection is achieved on intelligent harvesting and sorting equipment with limited hardware resources.
[0060] The fifth step involves compiling hardware instructions for the INT8 quantization model using a model compiler. This includes fusing convolution and batch normalization operators into a joint convolution-normalization instruction, mapping the SiLU activation function to corresponding lookup table instructions, mapping upsampling and feature concatenation to data transport and rearrangement instructions, and mapping matrix multiplication to a multiply-add instruction sequence for the processing unit array. The model compiler then arranges these hardware instruction sequences layer by layer into an instruction stream, generating an instruction memory configuration file. Simultaneously, it rearranges the INT8 quantization weight parameters according to the weight arrangement rules of the processing unit array, generating a weight memory configuration file. The instruction memory configuration file is written to the on-chip instruction memory of the preceding YOLOv8 customized inference model, and the weight memory configuration file is written to the on-chip weight memory, completing the model solidification.
[0061] After deployment, the front-end YOLOv8 customized inference model receives frame-by-frame RGB three-channel image frames of edible fungi, normalized to 640 pixels × 640 pixels and with pixel values normalized to the 0-1 range, from the front-end image preprocessing module. It then executes INT8 inference operations layer by layer according to the instruction stream in the on-chip instruction memory. Specifically, the convolutional layer performs multiplication and addition operations with 8-bit integers. The convolution result is fused with the batch normalization parameters and accumulated to a 32-bit integer intermediate result. After activation value quantization and restoration to an 8-bit unsigned integer, it is sent to the next layer. The model detection head outputs the candidate box coordinates (x_center, y_center, width, height), category confidence vector (dimensions of the number of edible fungi varieties plus the number of defect categories), instance mask coefficient vector (32-dimensional), and target center point coordinates (cx, cy) for each detected candidate target in the frame image. After inference is completed, the inference module writes the above four types of data into the candidate result receiving buffer unit of the post-processing device in a data format arranged one by one according to the candidate targets. The data format is as follows: each candidate target data block contains four candidate box coordinates represented by 16-bit fixed-point numbers, N category confidence values represented by 8-bit fixed-point numbers (N is the total number of categories), 32 mask coefficients represented by 8-bit fixed-point numbers, and two target center point coordinates represented by 16-bit fixed-point numbers. Each candidate target data block is written sequentially with the beginning and end connected, and a 16-bit unsigned integer is written at the beginning position to represent the total number of candidate targets in this frame, which serves as the input data for the post-processing device to perform subsequent deduplication suppression, mask boundary correction, and sticky target splitting. In this embodiment, in the candidate box coordinates (x_center, y_center, width, height) output by the model detection head, x_center and y_center represent the horizontal and vertical pixel coordinates of the center point of the candidate target bounding box relative to the input image (640×640 pixels), respectively. In the dense edible fungus scene, these two center point coordinates are directly used by the post-processing device to calculate the central Euclidean distance between adjacent candidate targets, serving as the core spatial feature variables for local density determination (such as determining whether it falls within the dynamic neighborhood radius range) and adaptive suppression threshold generation (such as evaluating the degree of target spatial separation). width and height respectively characterize the pixel span of the candidate box in the horizontal and vertical directions, and their product is the "candidate box area S". This area value is used by the neighborhood radius adjustment module to query the preset area-radius mapping table to dynamically determine the neighborhood radius threshold. On the other hand, it is used in the area comparator array to calculate the area ratio to filter densely adjacent individuals of the same scale, thereby effectively distinguishing mature large sub-entities from small mushroom buds.
[0062] In this embodiment, in the output data structure of the model detection head, cx and cy respectively represent the pixel-level centroid horizontal and vertical coordinates of the edible fungus fruiting entity instance corresponding to the detected candidate target in the input image coordinate system. Unlike the candidate box center point (x_center, y_center), which is located based on the bounding box's geometric outer rectangle, the target center point (cx, cy) is the target's physical spatial geometric center, calculated by the pre-stage YOLOv8 customized inference model based on the mask pixel distribution when decoding instance mask coefficients. It is designed as the core seed point coordinates called by the adhering target splitting unit (especially the watershed control module) in the post-processing pipeline to accurately guide watershed segmentation on the distance map, ensuring that each adhering connected region is split into an independent individual mask containing only a single real cap center. Simultaneously, this center point is also used as a spatial feature variable by the local density calculation unit in the parallel calculation of the Euclidean distance between target centers, providing a precise basis for the target spatial separation degree in determining the dynamic neighborhood radius and generating the adaptive suppression threshold. This effectively anchors the physical position benchmark of each real individual in densely overlapping scenes.
[0063] This application solidifies the candidate result receiving cache, local density calculation, parallel calculation of the overlap relation matrix, adaptive candidate suppression, instance mask boundary correction, and sticky target splitting into a hardware pipeline. It directly receives the candidate box coordinates, class confidence, instance mask, and target center point data output by the previous YOLOv8 customized inference model. With the cooperation of on-chip cache and parallel computing resources, it completes all post-processing operations, eliminating the scheduling overhead and latency bottlenecks caused by serial execution and repeated access to external memory in non-maximum suppression and mask post-processing in general software solutions. Among them, the adaptive candidate suppression unit introduces four variables for each candidate target through the threshold generation subunit: local density value, candidate box area, class confidence value, and Euclidean distance to the center point of the candidate with the highest confidence. It dynamically generates a suppression threshold that adapts to the current local density and the individual's own geometry and confidence characteristics. Then, the suppression execution subunit performs deduplication suppression based on this dynamic threshold instead of the fixed threshold. This mechanism enables the suppression threshold to be automatically increased in densely clustered areas to retain adjacent real individuals, and the threshold to be automatically decreased in sparse areas to effectively eliminate duplicate candidates, thereby significantly reducing the false deletion of dense individuals. The instance mask boundary correction unit redivides the mask boundary along the shortest path between pairs of indented points based on the geometric features of the cap contour depression points and the overlapping areas of adjacent masks. The adhered target splitting unit divides the adhered connected regions with areas exceeding the threshold into independent individual masks, each containing a real center point, through connected component labeling and distance transformation. The two work together to eliminate erroneous merging and boundary misclassification among dense individuals, improve the clarity of instance mask boundaries and the accuracy of individual counting, provide the intelligent edible fungus harvesting robot with the precise positioning coordinates of each harvestable sub-entity, and provide the automated sorting line with accurate individual quantity statistics and single individual contour data, achieving low-latency, high-precision post-processing capabilities for dense edible fungus target detection.
[0064] Example 2
[0065] It should be further explained that the adaptive candidate suppression unit in this embodiment includes a threshold generation subunit and a suppression execution subunit. The threshold generation subunit is used to generate a dynamic suppression threshold for each candidate target based on the local density value of the region where the candidate target is located, the candidate box area of the candidate target, the category confidence value, and the Euclidean distance between the candidate target and the center point of the current highest confidence candidate target. The suppression execution subunit, for a first candidate target and a second candidate target that constitute a candidate target pair, obtains the intersection-union ratio or mask overlap rate of the candidate target pair from the overlap relationship matrix. When the intersection-union ratio or mask overlap rate is greater than the dynamic suppression threshold of the candidate target with lower confidence between the first candidate target and the second candidate target, the candidate target with lower confidence is suppressed; otherwise, both are retained, and the retained candidate targets are marked as real individuals and corresponding target identifiers are generated.
[0066] It should be further explained that, in this embodiment, the threshold generation subunit performs the following calculation logic when generating the dynamic suppression threshold corresponding to each candidate target:
[0067] The threshold generation subunit first reads the local density value D of the region where the candidate target is located from the output register of the local density calculation unit. The value of D is a 16-bit fixed-point number between 0 and 1, where D=0 indicates that there are no other candidate targets in the neighborhood of the candidate target, and D=1 indicates that the number of candidate targets in the neighborhood of the candidate target reaches or exceeds twice the preset number threshold. It then reads the candidate box area S of the candidate target from the candidate result receiving buffer unit. S is represented by a 16-bit unsigned integer in pixels. Next, it reads the category confidence value C of the candidate target from the candidate result receiving buffer unit. C is an 8-bit unsigned integer with a value between 0 and 255, corresponding to a linear mapping of confidence from 0 to 1. Finally, it reads the center point coordinates of the candidate target with the highest confidence from the candidate result receiving buffer unit and calculates the Euclidean distance L between the center point of the candidate target and the center point of the candidate target with the highest confidence. L is represented by a 16-bit fixed-point number. In this embodiment, the local density value D is calculated using a linear mapping method. The calculation formula is D=min(N0 / (2×N_th),1.0), where N0 is the number of neighboring candidate targets corresponding to the candidate target, and N_th is a preset quantity threshold. The calculation result is stored in a 16-bit fixed-point number format, with 1 integer bit and 15 decimal bits, ensuring that the value range is strictly limited to the interval between 0 and 1.
[0068] In this embodiment, the threshold generation subunit calculates the dynamic suppression threshold T corresponding to the candidate target according to the following piecewise linear mapping function:
[0069] When L is greater than the preset maximum separation distance threshold L_max, T is directly taken as the baseline suppression threshold T_base. T_base is preset to 0.5, which means that the separation degree of the two target center points has exceeded the maximum possible size of the edible fungus fruiting body, and conventional suppression is performed according to the fixed low threshold.
[0070] When L is less than or equal to L_max, T = T_base + k1 × D + k2 × (S / S_max) + k3 × (1 - C / 255) - k4 × (L / L_max), where S_max is the preset maximum candidate box area, which is one-quarter of the input image area, and k1, k2, k3, and k4 are preset weight coefficients, which are 0.15, 0.10, 0.10, and 0.05 respectively. The resulting T value is constrained to the range of 0.3 to 0.9. When the calculated result is less than 0.3, it is taken as 0.3, and when it is greater than 0.9, it is taken as 0.9. In this embodiment, the preset maximum separation distance threshold L_max is 300 pixels, which is based on the size calibration of the 640×640 pixel inference input image; the input image area specifically refers to the total area of the normalized 640×640 pixel inference input image, that is, 409600 square pixels, corresponding to an S_max value of 102400 square pixels.
[0071] In this embodiment, the piecewise linear mapping function is set based on the following: a larger local density value D indicates a more crowded region where the candidate target is located, and the suppression threshold should be increased to reduce the false deletion of adjacent real individuals; a larger candidate box area S indicates a higher probability that the candidate target is a mature sub-entity, and a greater possibility of overlap with adjacent targets, so the suppression threshold should be appropriately increased; a lower category confidence value C indicates a lower confidence that the candidate target is a real individual, so the suppression threshold should be decreased to make it easier to be suppressed by high-confidence candidate targets; a larger center point distance L indicates a more obvious spatial separation between two candidate targets, so the suppression threshold should be decreased to more actively eliminate duplicate detections. The above four variables are weighted and summed to jointly determine the final value of the dynamic suppression threshold. The weight coefficients k1 to k4 are determined based on the goal of maximizing the harmonic mean of recall and precision in dense regions using a grid search method on the validation set.
[0072] Please see Figure 2 It should be further explained that the local density calculation unit in this embodiment includes a parallel distance calculation array, an area comparator array, an overlap determination array, a counter array, a dense region determination module, and a neighborhood radius adjustment module; the parallel distance calculation array is used to calculate the center Euclidean distance between the first candidate target and the second candidate target in parallel, and output the distance calculation result; the area comparator array is used to calculate the ratio of the candidate box area of the first candidate target to the candidate box area of the second candidate target, and output the area ratio result; the overlap determination array is used to calculate the overlap ratio between the candidate box of the first candidate target and the candidate box of the second candidate target, and output the overlap ratio result.
[0073] The counter array is used to count the number of second candidate targets that simultaneously meet the following conditions for the first candidate target: the Euclidean distance between their centers is less than a preset neighborhood radius threshold, the area ratio falls within a preset area ratio interval, and the overlap ratio exceeds a preset overlap threshold. This number is then used as the number of neighboring candidate targets of the first candidate target. The dense region determination module is used to read the number of neighboring candidate targets of the first candidate target. When the number of neighboring candidate targets exceeds a preset number threshold, the region where the first candidate target is located is marked as a dense region, and the dense region identifier is output to the overlap relationship matrix calculation unit. It should be further noted that, in this embodiment, the preset overlap threshold and preset area ratio interval used by the counter array to determine whether a second candidate target is included in the number of neighboring candidate targets of the first candidate target are set based on the following:
[0074] In this embodiment, the preset overlap threshold is set to 0.1. The basis for this setting is that when the overlap ratio of two edible fungus candidate boxes is less than 0.1, the two boxes only have edge contact or are completely separated in space and do not have a spatial proximity relationship to form a local dense cluster, so they should not be included in the neighborhood candidate targets; when the overlap ratio reaches or exceeds 0.1, it indicates that there is a meaningful spatial overlap or close proximity relationship between the two candidate boxes, which is consistent with the distribution characteristics of densely clustered edible fungi, and should be included in the local density statistics.
[0075] In this embodiment, the preset area ratio range is [0.3, 3.0]. The basis for this setting is as follows: when the area ratio of the candidate boxes of two candidate targets is less than 0.3 or greater than 3.0, it indicates that the two are more than 3.3 times different in scale. This usually corresponds to a combination of mature large fruiting bodies and small mushroom buds or background noise blocks. In actual scenarios, it is almost impossible for such candidate targets with excessively large scale differences to constitute two independent, densely adjacent real individuals. Excluding them can avoid the interference of background noise on the determination of dense areas of small mushroom buds. When the area ratio falls within the range of 0.3 to 3.0, it indicates that the two candidate targets are comparable in scale and meet the scale consistency characteristics of densely clustered edible fungi at the same growth stage. They should be included in the statistics of neighboring candidate targets.
[0076] The input terminal of the neighborhood radius adjustment module is connected to the output port of the candidate result receiving buffer unit, and the output terminal of the neighborhood radius adjustment module is connected to the configuration input terminal of the counter array.
[0077] The neighborhood radius adjustment module is used to read the candidate box area of the first candidate target from the candidate result receiving cache unit, use the candidate box area as the input address to query a preset area-radius mapping table, read the dynamic neighborhood radius threshold corresponding to the candidate box area from the area-radius mapping table, and write the dynamic neighborhood radius threshold into the neighborhood radius configuration register of the counter array.
[0078] When counting the number of neighboring candidate targets for the first candidate target, the counter array reads the dynamic neighborhood radius threshold from the neighborhood radius configuration register and compares the center Euclidean distance with the dynamic neighborhood radius threshold to determine whether the second candidate target falls within the neighborhood range of the first candidate target.
[0079] It should be further explained that the mapping relationship of the preset area-radius mapping table queried by the neighborhood radius adjustment module in this embodiment and the basis for its setting are as follows:
[0080] The area-radius mapping table uses the candidate box area as the input address and the dynamic neighborhood radius threshold as the output data; the unit of measurement for the candidate box area is square pixels, and the unit of measurement for the dynamic neighborhood radius threshold is pixels. The mapping relationship of this mapping table is divided into the following three consecutive non-overlapping intervals, and the boundary value rules are clear and unique:
[0081] When the area of the candidate box is less than 256 square pixels, the dynamic neighborhood radius threshold is 1.5 times the diagonal length of the candidate box. This level corresponds to young mushroom buds. Such targets have a low pixel ratio, and their neighborhood range is appropriately expanded based on their own size. This can avoid multiple densely distributed mushroom buds being misjudged as isolated individuals due to the neighborhood radius being too small.
[0082] When the area of a candidate box is greater than or equal to 256 square pixels and less than or equal to 1024 square pixels, the dynamic neighborhood radius threshold is 2.0 times the diagonal length of the candidate box. This level corresponds to medium-sized sub-entities in the growth stage. The spatial distance between such individuals is relatively stable. Using twice the diagonal length as the neighborhood radius can effectively cover the similar individuals that are right next to it.
[0083] When the area of the candidate box is greater than 1024 square pixels, the dynamic neighborhood radius threshold is 2.5 times the diagonal length of the candidate box. This level corresponds to mature large-sized sub-entities. After the cap of such individuals unfolds, it occupies a large space. The mutual compression between individuals leads to an increase in the distance between the center points. A larger neighborhood radius is needed to include the individuals that are next to it in the statistical range.
[0084] In this embodiment, the diagonal length of the candidate box is calculated from the width and height of the candidate box using a geometric formula. Specifically, the diagonal length of the candidate box is equal to the square root of the sum of the square of the candidate box width and the square of the candidate box height. The candidate box width and height are measured in pixels and correspond exactly to the candidate box coordinate parameters stored in the candidate result receiving cache unit.
[0085] It should be further explained that, in this embodiment, after the local density calculation unit completes the dense region determination of all candidate targets, it performs data splitting output according to the determination result: For candidate targets marked as dense regions, the dense region determination module assembles the target identifier, candidate box coordinates, category confidence value and dense region identifier of the candidate target into a dense region data packet, and outputs it to the data input port of the overlap relation matrix calculation unit, triggering the overlap relation matrix calculation unit to perform parallel calculation of the intersection-union ratio, center distance and mask overlap rate of the candidate target and its neighboring candidate targets; For candidate targets marked as non-dense regions, the dense region determination module assembles the target identifier, candidate box coordinates and category confidence value of the candidate target into a non-dense region data packet, and outputs it to the data input port of the adaptive candidate suppression unit. The adaptive candidate suppression unit directly performs deduplication suppression on the candidate targets in non-dense regions using a preset fixed suppression threshold of 0.5, without going through the processing of the overlap relation matrix calculation unit and the instance mask boundary correction unit. The data splitting mechanism in this embodiment allows candidate targets in non-dense regions to skip the parallel computation of the overlap relation matrix, and concentrates the parallel computation array resources of the overlap relation matrix computation unit on the relation calculation of candidate target pairs in dense regions, thereby reducing the ineffective occupation of computational resources by sparse candidate targets in non-dense regions.
[0086] It should be further explained that, in this embodiment, the overlap matrix calculation unit performs the following calculation logic when calculating the mask overlap rate:
[0087] The overlap relation matrix calculation unit reads the instance mask coefficient vectors corresponding to each candidate target within a dense region from the candidate result receiving buffer unit. Each instance mask coefficient vector contains 32 8-bit fixed-point numbers. The overlap relation matrix calculation unit has a built-in mask decoder, which generates a binary instance mask map corresponding to each candidate target by performing a linear combination operation between the mask coefficient vectors and the prototype mask matrix. The prototype mask matrix originates from the mask branch output of the preceding YOLOv8 customized inference model and is a 32-channel feature map with a spatial size of 160 pixels × 160 pixels and a channel dimension of 32, corresponding one-to-one with the dimensions of the instance mask coefficient vectors. The specific rules of the linear combination operation are as follows: each coefficient value in the 32-dimensional mask coefficient vector is multiplied pixel-by-pixel with the feature map of the corresponding channel in the prototype mask matrix, and then the multiplication results of the 32 channels are summed point by point according to the pixel position to obtain a single-channel mask response map; then, using 0.5 as the binarization confidence threshold, pixels in the mask response map with pixel values greater than or equal to the threshold are assigned a value of 1, and pixels with pixel values less than the threshold are assigned a value of 0, finally generating a binary instance mask map with a spatial size of 160 pixels × 160 pixels.
[0088] For a first and second candidate target constituting a candidate target pair, the overlap matrix calculation unit performs a pixel-by-pixel logical AND operation on the binary instance mask images of the first and second candidate targets, counting the number of pixels with a logical AND result of 1, which is taken as the overlapping pixel area of the two instance masks. Simultaneously, the total number of pixels with a pixel value of 1 in the binary instance mask image of the first candidate target is counted as the first mask area, and the total number of pixels with a pixel value of 1 in the binary instance mask image of the second candidate target is counted as the second mask area. The mask overlap rate is calculated as follows: the ratio of the overlapping pixel area to the first mask area and the ratio of the overlapping pixel area to the second mask area are calculated respectively, and the larger of the two ratios is taken as the mask overlap rate of the candidate target pair. When either the first or second mask area is 0, the mask overlap rate of the candidate target pair is directly determined to be 0 to avoid division by zero errors. The final mask overlap rate is represented in 8-bit fixed-point format, with a value range of 0 to 1 and a quantization precision of 1 / 256.
[0089] Please see Figure 3It should be further explained that the instance mask boundary correction unit in this embodiment includes a contour tracking module, a curvature calculation module, and a boundary re-division module. The contour tracking module is used to extract the contour point sequence of the corresponding instance mask with the retained real individual identifier as input, and output the coordinate values of each contour point in the contour point sequence. The curvature calculation module is used to calculate the curvature value of each contour point in the contour point sequence based on the coordinate values of the contour point and the coordinate values of the adjacent contour points, and detect the contour points in the contour point sequence whose curvature values are greater than the curvature values of the adjacent two contour points as local maxima of curvature, and mark the local maxima of curvature as cap contour depression points.
[0090] The boundary re-division module is used to receive the position information of the overlapping area of the adjacent masks. Within the overlapping area of the adjacent masks, a pair of concave points with the smallest spatial Euclidean distance is selected from the cap contour concave points belonging to the first instance mask and the cap contour concave points belonging to the second instance mask to form a concave point pair. The straight line segment between the concave point pair is used as the new dividing boundary to replace the original boundary of the first instance mask and the second instance mask in the overlapping area of the adjacent masks, and the corrected mask of the first instance mask and the second instance mask is output.
[0091] It should be further explained that, in this embodiment, the curvature calculation module executes the following calculation logic when calculating the curvature values of contour points and detecting local maxima of curvature:
[0092] The curvature calculation module receives a sequence of contour points from the contour tracking module. This sequence is a set of contour point coordinates arranged clockwise or counterclockwise, with the total number of contour points denoted as M. For the i-th contour point in the sequence, the curvature calculation module takes the (i-2)-th and (i-1)-th contour points forward and the (i+1)-th and (i+2)-th contour points backward, forming a curvature calculation window for the i-th contour point. When contour point i is located at the beginning or end of the sequence, resulting in insufficient points to take forward or backward, a cyclic point taking method is used. That is, the contour point sequence is treated as a closed contour with its beginning and end connected, and the required contour points are taken from the other end of the sequence. Based on the coordinate values of the above 5 consecutive contour points, the curvature calculation module uses the three-point circular arc curvature approximation method to fit an arc using the (i-1)-th, i-th, and (i+1)-th contour points respectively, and calculates the curvature value of the arc as the curvature value of the i-th contour point. The curvature value is represented in 32-bit floating-point format, with the unit being radians per pixel. In this embodiment, the formula for calculating the curvature of the three-point arc is K=4S / (a×b×c), where a, b, and c are the three side lengths of the triangle formed by the three points, and S is the area of the triangle. A positive curvature value indicates that the contour is concave inward, while a negative curvature value indicates that the contour is convex outward. Only local maxima with positive curvature values are selected for the concave points of the cap contour to ensure that the direction of concavity determination is unique.
[0093] After obtaining the curvature values of all contour points in the contour point sequence, the curvature calculation module performs local maximum determination for each contour point: It compares the curvature value of the i-th contour point with the curvature values of the (i-1)-th and (i+1)-th contour points respectively. If the curvature value of the i-th contour point is greater than both the curvature values of the (i-1)-th and (i+1)-th contour points, the i-th contour point is determined to be a local maximum point of curvature and marked as a depression point on the cap contour. If the curvature value of the i-th contour point is equal to the curvature value of its adjacent contour point, it is not determined to be a local maximum point of curvature to avoid mistakenly marking flat areas on the contour as depression points. The above-mentioned method for detecting local curvature maxima only compares two adjacent contour points. The basis for this method is that the concave points of the cap contour of edible fungi are usually sharp corners with a sharp increase in curvature on the contour curve. The curvature difference between them and the adjacent points is significant. Only comparing the adjacent points can effectively detect the concave points, while avoiding misjudging the slowly changing arc-shaped areas on the contour as concave points.
[0094] It should be further explained that the sticky target splitting unit in this embodiment includes a connected component marking module, a distance transformation module, and a watershed control module. The connected component marking module is used to receive the modified mask, perform connected component marking on the modified mask to obtain at least one connected component, calculate the area of each connected component, and when the area of any connected component is greater than a preset area threshold, mark the connected component as a sticky connected component and generate a splitting trigger signal. The distance transformation module is used to respond to the splitting trigger signal, perform Euclidean distance transformation on the sticky connected component, calculate the Euclidean distance from each pixel in the sticky connected component to the nearest background pixel, and generate a distance map. In this embodiment, the Euclidean distance transformation adopts a precise Euclidean distance transformation algorithm, the distance value is stored as a single-precision floating-point number, and the unit is pixels; the distance calculation is based on the pixel center coordinates, the distance value of the background pixel is recorded as 0, and the distance value of the foreground pixel is the shortest Euclidean distance from that point to all background pixels.
[0095] The watershed control module is used to obtain the target center point coordinates of multiple real individuals located in the cohesive connected domain and retained by the adaptive candidate suppression unit from the candidate result receiving cache unit, and to perform watershed segmentation on the distance map using the multiple target center points as seed points, dividing the cohesive connected domain into independent regions, each containing a seed point, and outputting the split individual mask.
[0096] The watershed control module includes a seed point correction submodule. The input of the seed point correction submodule is connected to the output of the distance transformation module to receive the distance map. The input of the seed point correction submodule is also connected to the output port of the candidate result receiving buffer unit to obtain the coordinates of the multiple target center points as initial seed points. For each initial seed point, the seed point correction submodule is used to define a rectangular search window on the distance map, centered on the coordinates of that initial seed point. The width and height of the rectangular search window are preset search window width and height values. The distance values of all pixels within the rectangular search window are read, and the distance values of all pixels are compared numerically. The pixel with the largest distance value is determined as the corrected seed point, and the coordinates of the corrected seed point are used as the coordinates of the corrected seed point. The watershed control module uses the coordinates of all corrected seed points as seed points and performs watershed segmentation on the distance map. When there are multiple pixels with equal and maximum distance values within the rectangular search window, the seed point correction submodule selects the pixel with the smallest row coordinate and the smallest column coordinate as the corrected seed point.
[0097] It should be further explained that the preset search window width and preset search window height values used in the seed point correction submodule in this embodiment are based on and have the following values:
[0098] The preset search window width and height are both set to 15 pixels, meaning the rectangular search window is a square window measuring 15 pixels by 15 pixels. This setting is based on the fact that, given an input image size of 640 pixels by 640 pixels, the cap diameter of a single mature edible mushroom fruiting body typically ranges from 40 to 80 pixels, and the radius of the cap's central area is approximately one-quarter to one-third of the cap diameter, or between 10 and 27 pixels. Setting the search window side length to 15 pixels ensures that the search window completely covers the central area of the cap, while preventing the search window from becoming too large and extending into the cap areas of adjacent, adhered individuals, which could cause the seed point to shift into the interior of adjacent individuals. When the search window side length is less than 15 pixels, for mature fruiting bodies with a large cap diameter and a wide flat area at the center of the distance map, the search window may not be able to cover all pixels with the largest distance value, resulting in the corrected seed point not being located at the center of the cap. When the search window side length is greater than 15 pixels, for two adjacent individuals with a small adhesion distance, the search window may cross the individual boundary and cover the cap area of the adjacent individual, causing the two initial seed points to converge to the same pixel after correction, resulting in the subsequent watershed segmentation being unable to correctly separate the two individuals.
[0099] In this embodiment, when the seed point correction submodule defines a rectangular search window on the distance map, it expands by 7 pixels in each of the four directions (up, down, left, and right) centered on the coordinates of the initial seed point, forming a rectangular search window with a width of 15 pixels and a height of 15 pixels. When the initial seed point is close to the boundary of the distance map, causing the search window to exceed the range of the distance map, the boundary of the search window is truncated to the effective boundary of the distance map, and only the distance values of pixels within the effective range of the distance map are read for comparison.
[0100] It should be further explained that the adhering target splitting unit in this embodiment includes a post-processing control unit coupled to the candidate result receiving buffer unit, the local density calculation unit, the overlap relation matrix calculation unit, the adaptive candidate suppression unit, the instance mask boundary correction unit, and the adhering target splitting unit, respectively. This control unit generates a pipeline tick control signal, which controls the data to be processed to be passed sequentially according to the order of the local density calculation unit, the overlap relation matrix calculation unit, the adaptive candidate suppression unit, the instance mask boundary correction unit, and the adhering target splitting unit, so that the output of the previous unit becomes the input of the next unit after the next pipeline tick arrives. The post-processing control unit is also used to monitor the data writing status of the candidate result receiving buffer unit. When no new candidate box coordinates, category confidence, instance mask, or target center point is detected being written to the candidate result receiving buffer unit within a preset time window, the post-processing control unit stops outputting clock signals to the local density calculation unit, the overlap relation matrix calculation unit, the adaptive candidate suppression unit, the instance mask boundary correction unit, and the adhering target splitting unit.
[0101] It should be further explained that the value of the preset time window used by the post-processing control unit in this embodiment and the basis for its setting are as follows:
[0102] The preset time window is set to 100 milliseconds. This setting is based on the following: After the pre-stage YOLOv8 customized inference model is deployed with INT8 quantization, the inference latency for a single frame image is approximately 5 to 8 milliseconds. During continuous processing, the time interval between writing candidate results from two adjacent frames to the candidate result receiving buffer unit typically does not exceed 10 milliseconds. When the intelligent mushroom harvesting robot or sorting line is in continuous operation, image frames are continuously input, and the candidate result receiving buffer unit generates a new data write event after each inference cycle. Setting the preset time window to 100 milliseconds is equivalent to the time required for the pre-stage inference module to complete the inference of at least 12 frames and write candidate results under normal continuous operation. This duration is sufficient to cover the brief write intervals caused by occasional pipeline pauses in the image preprocessing module or data transfer delays within the pre-stage inference module, preventing the normal inter-frame interval from being mistakenly identified as an idle state and causing the clocks of each processing unit to be incorrectly turned off. When the post-processing control unit does not detect any new candidate box coordinates, class confidence, instance mask, or target center point being written to the candidate result receiving buffer unit within 100 milliseconds, it determines that the preceding inference module has entered an idle state or there is no new frame input. At this time, shutting down the clock signals of each downstream processing unit can eliminate invalid dynamic power consumption. In this embodiment, when the post-processing control unit performs the clock shutdown operation, it sequentially stops the clock output in the order of the adhering target splitting unit, instance mask boundary correction unit, adaptive candidate suppression unit, overlap relation matrix calculation unit, and local density calculation unit. This ensures that each unit has completed the processing of the input data within the current pipeline cycle and written the results to the corresponding output register before the clock shutdown, avoiding the loss of intermediate processing data due to sudden clock shutdown. When the post-processing control unit detects a new data write event in the candidate result receiving buffer unit while the clock is off, it sequentially resumes the clock output in the order of the local density calculation unit, overlap relation matrix calculation unit, adaptive candidate suppression unit, instance mask boundary correction unit, and adhering target splitting unit, so that the pipeline resumes operation step by step from the first-level unit. In this embodiment, each processing unit is equipped with an independent processing completion flag register. Before shutting down the clock of the corresponding unit, the post-processing control unit first reads the completion flag bit of the unit. After confirming that the flag bit is in a valid completion state, the clock shutdown operation is then performed. The timing reference adopts a 1-millisecond timer obtained by dividing the system's 50MHz main clock to ensure uniform timing accuracy of the time window.
[0103] It should be further explained that the result output unit of this embodiment includes a data framing module and an interface controller. The data framing module is used to read the contour point sequence of the split individual mask from the output port of the adhesion target splitting unit, and to read the target category, position coordinates, and defect identifier corresponding to the retained real individual from the output port of the adaptive candidate suppression unit. The target category of the same retained real individual is written into the target category field of the data frame, the position coordinates are written into the position coordinate field, the contour point sequence is written into the mask data field, and the defect identifier is written into the defect identifier field. The frame header identifier field, frame length field, and cyclic redundancy check field are written into the data frame in sequence to generate a formatted data frame. The interface controller is used to read the formatted data frame from the data framing module, use the formatted data frame as the payload, add a USB frame start field and a USB device address field before the payload according to the USB protocol specification, or add an Ethernet frame header field and an Internet Protocol header field before the payload according to the Ethernet protocol specification, and send the protocol-encapsulated data frame to the external control device through the USB physical layer interface or the Ethernet physical layer interface.
[0104] It should be further explained that the implementation logic and setting basis for the interface controller to switch between the USB protocol specification and the Ethernet protocol specification in this embodiment are as follows:
[0105] The interface controller includes a protocol selection and determination module. The input of this module is connected to the output of the post-processing control unit, and it is used to read the current value of the interface mode configuration register stored in the post-processing control unit. The interface mode configuration register is a 2-bit hardware register. Its value is written by an external control device through the chip configuration interface during the power-on initialization phase. The correspondence between the register value and the interface mode is as follows: when the interface mode configuration register value is binary 00, the interface controller selects the USB physical layer interface and encapsulates data according to the USB protocol specification; when the interface mode configuration register value is binary 01, the interface controller selects the Ethernet physical layer interface and encapsulates data according to the Ethernet protocol specification; when the interface mode configuration register value is binary 10 or 11, the interface controller defaults to selecting the Ethernet physical layer interface and encapsulates data according to the Ethernet protocol specification.
[0106] In this embodiment, the interface mode configuration register is set based on the fact that the intelligent mushroom harvesting robot and the sorting line equipment typically use either a USB interface or an Ethernet interface as the standard communication interface. This interface is set once by system integrators according to the actual interface type of the controlled equipment during factory manufacturing or field deployment, eliminating the need for dynamic switching during operation. Setting the interface mode configuration register to a 2-bit width and retaining two default encoding values mapped to Ethernet mode ensures that when a single bit flip occurs due to electromagnetic interference, the probability of the register value flipping from binary 00 to binary 01 or binary 10 is 50%, and the probability of it flipping to binary 11 is 25%. This means the probability of the interface controller incorrectly switching to USB mode due to register bit flips does not exceed 25%, effectively reducing the risk of abnormal communication protocol switching caused by electromagnetic interference in industrial settings.
[0107] In this embodiment, each time the interface controller sends a formatted data frame, it first reads the current interface mode output by the protocol selection determination module. When the interface mode is USB mode, the interface controller adds a USB transaction packet header consisting of an 8-bit synchronization field, an 8-bit packet identifier field, a 7-bit device address field, and a 4-bit endpoint number field before the formatted data frame, and adds a 16-bit USB cyclic redundancy check (CRC) tail after the formatted data frame. After encapsulation, the data is output through the on-chip USB physical layer interface differential signal driver. When the interface mode is Ethernet mode, the interface controller adds an Ethernet media access control (MAC) frame header consisting of a 7-byte preamble field, a 1-byte start-of-frame delimiter field, a 6-byte destination MAC address field, a 6-byte source MAC address field, and a 2-byte type-length field before the formatted data frame, and adds a 4-byte Ethernet frame check sequence tail after the formatted data frame. After encapsulation, the data is output to an external Ethernet physical layer transceiver through the on-chip media-independent interface.
[0108] It should be further explained that the result output unit in this embodiment also includes an interface controller and an interface configuration register; the interface configuration register is connected to the interface controller and is used to store the interface mode selection value; the interface controller includes a protocol encapsulation module and an output multiplexer, the input terminal of the protocol encapsulation module receives the formatted data frame, and the control terminal of the output multiplexer is connected to the interface configuration register; when the interface mode selection value is a first preset value, the protocol encapsulation module adds a USB transaction packet header before the formatted data frame and adds a USB cyclic redundancy check tail after the formatted data frame to generate a USB data packet, and the output multiplexer selects the USB physical layer interface to output the USB data packet; when the interface mode selection value is a second preset value, the protocol encapsulation module adds an Ethernet MAC frame header before the formatted data frame and adds an Ethernet frame check sequence tail after the formatted data frame to generate an Ethernet data frame, and the output multiplexer selects the Ethernet physical layer interface to output the Ethernet data frame. It should be further explained that the specific values and settings of the first and second preset values of the interface mode selection value stored in the interface configuration register in this embodiment are as follows:
[0109] The interface configuration register is a 1-bit hardware register, whose value is written by the external control device during the power-on initialization phase via the chip configuration interface. The first preset value is binary 0, and the second preset value is binary 1. When the interface configuration register is binary 0, the interface controller encapsulates data according to the USB protocol specification and outputs it through the USB physical layer interface; when the interface configuration register is binary 1, the interface controller encapsulates data according to the Ethernet protocol specification and outputs it through the Ethernet physical layer interface. The reason for using a 1-bit register to distinguish between the two interface modes is that the control interfaces of the intelligent mushroom harvesting robot and the sorting line equipment are usually of a single type, either USB or Ethernet, and there is no need to support dynamic switching between more than two interface types. A 1-bit register can completely express the selection status of the two interface modes, and compared with multi-bit registers, a 1-bit register occupies less space, consumes less power, and only requires one clock cycle for read and write operations.
[0110] When the interface mode selection value is the first preset value, the protocol encapsulation module generates a USB transaction packet header containing an 8-bit synchronization field, an 8-bit packet identifier field, a 7-bit device address field, and a 4-bit endpoint number field, with a total header length of 27 bits. A 16-bit cyclic redundancy check (CRC) value is added after the formatted data frame, calculated using a generator polynomial specified by the USB protocol on all data in the USB transaction packet header and the formatted data frame. When the interface mode selection value is the second preset value, the protocol encapsulation module generates an Ethernet media access control (MAC) frame header containing a 7-byte preamble field, a 1-byte start-of-frame delimiter field, a 6-byte destination MAC address field, a 6-byte source MAC address field, and a 2-byte type-length field, with a total header length of 22 bytes. A 4-byte CRC value is added after the formatted data frame, calculated using a 32-bit generator polynomial specified by the Ethernet protocol on all data in the Ethernet MAC frame header and the formatted data frame.
[0111] The output multiplexer is a 2-to-1 multiplexer. Its data input terminals are connected to the USB data packet output signal line and the Ethernet data frame output signal line, respectively. Its control terminal is directly connected to the output terminal of the interface configuration register. When the strobe signal is binary 0, the output multiplexer connects the USB data packet signal line to the output pin and disconnects the Ethernet data frame signal line; when the strobe signal is binary 1, the output multiplexer connects the Ethernet data frame signal line to the output pin and disconnects the USB data packet signal line.
[0112] It should be further noted that the target detection post-processing device in this embodiment further includes: the input end of the data distribution register file is coupled to the output port of the overlap relation matrix calculation unit, and the output end of the data distribution register file is coupled to the input port of the adaptive candidate suppression unit and the input port of the instance mask boundary correction unit, respectively.
[0113] After calculating the crossover ratio, center distance, and mask overlap rate among candidate targets in the dense region, the overlap relationship matrix calculation unit writes the calculation results into the data distribution register file.
[0114] The data distribution register set includes an input register group, a first output register group, and a second output register group. The input register group is used to receive the calculation results written by the overlap relation matrix calculation unit. The first and second output register groups are both connected in parallel with the input register group. Within the same clock cycle, the crossover ratio, center distance, and mask overlap rate stored in the input register group are simultaneously latched into the first and second output register groups. In this embodiment, the parallel connection means that the data input terminals of the first and second output register groups are connected to the data output terminals of the input register group, and the two share the same input data source. The latching operation is synchronously triggered by the rising edge of the clock. The data bit width of each register group is 32 bits, of which the crossover ratio occupies 8 bits, the center distance occupies 12 bits, the mask overlap rate occupies 8 bits, and the remaining 4 bits are reserved bits.
[0115] The adaptive candidate suppression unit reads the cross-parallel ratio, center distance, and mask overlap rate from the first output register group through its input port; the instance mask boundary correction unit reads the cross-parallel ratio, center distance, and mask overlap rate from the second output register group through its input port.
[0116] It should be further explained that in this embodiment, the data distribution register file implements simultaneous latching operations from the input register group to the two output register groups, and its triggering mechanism and read / write timing are as follows:
[0117] After calculating the cross-parallel ratio, center distance, and mask overlap rate between each pair of candidate targets, the overlap relationship matrix calculation unit concatenates these three calculation results into a single data word and writes it into the input register group of the data distribution register file. Simultaneously, it sets a data validity flag signal to high. Upon detecting the rising edge of the data validity flag signal, the input register group latches the cross-parallel ratio, center distance, and mask overlap rate values stored in the input register group into the corresponding fields of the first and second output register groups, respectively, on the rising edge of the next clock cycle. This latching operation is completed within the same clock cycle and requires no additional enable control signal. After latching, the input register group sets the data validity flag signal low, indicating that it can receive the calculation results for the next pair of candidate targets.
[0118] In this embodiment, the adaptive candidate suppression unit reads the crossover ratio, center distance, and mask overlap rate from the first output register group via a dedicated 32-bit parallel read bus between itself and the first output register group on the falling edge of each clock cycle. Similarly, the instance mask boundary correction unit reads these three values from the second output register group via a dedicated 32-bit parallel read bus between itself and the second output register group on the falling edge of each clock cycle. Since the data updates of the first and second output register groups occur on the rising edge of the clock, while the downstream unit's read operation occurs on the falling edge, a half-clock cycle timing interval exists between the two operations, ensuring the stability of the read data and avoiding data errors caused by read-write conflicts.
[0119] In this embodiment, the simultaneous latching mechanism is based on the following: the adaptive candidate suppression unit needs to make candidate suppression decisions based on the cross-parallel ratio and mask overlap rate, while the instance mask boundary correction unit needs to determine the overlapping region and boundary re-division path based on the mask overlap rate and center distance. The data required by both is completely consistent. By simultaneously broadcasting the output of the overlap relation matrix calculation unit to two downstream units within the same clock cycle, the waiting delay caused by time-sharing data reading is eliminated, ensuring the data synchronization of the two parallel processing paths in the pipeline, and improving the overall throughput of post-processing by 100% compared to the time-sharing reading scheme.
[0120] In a specific example of this embodiment, taking the intelligent harvesting scenario of shiitake mushrooms as an example, the workflow of the device of the present invention will be fully described. In this scenario, shiitake mushrooms grow densely on the surface of the substrate in the mushroom house, with adjacent fruiting bodies overlapping and pressing against each other. The diameter of some young mushroom buds is only one-fifth that of mature individuals, and there is background interference such as sawdust and mycelial residue on the surface of the culture medium. An industrial camera captures visible light images of the front of the mushroom bed at a resolution of 640 pixels by 640 pixels and a frame rate of 30 frames per second. After noise reduction and illumination compensation by the image preprocessing module, the images are sent to the front-end YOLOv8 customized inference model to perform INT8 inference operations. The front-end YOLOv8 customized inference model completes the inference of one frame of image in about 6 milliseconds, outputting the candidate box coordinates, class confidence, instance mask coefficient, and target center point coordinates of 82 detected candidate targets in the frame of image. The above data is written into the candidate result receiving buffer unit in a format in which the candidate targets are arranged one by one, and the total number of candidate targets in the header of the data block is 82.
[0121] After the candidate result receiving and caching unit completes data caching, the local density calculation unit starts. The parallel distance calculation array calculates the Euclidean distance between the centers of the 82 candidate targets in parallel, generating a total of 3321 distance values; the area comparator array and the overlap determination array simultaneously calculate the area ratio and overlap ratio of each candidate target pair in parallel. Taking candidate target number 15 as an example, this candidate target corresponds to a mature shiitake mushroom with a cap diameter of approximately 60 pixels. Its candidate box area is 3200 pixels. The neighborhood radius adjustment module uses 3200 as the input address to query the area-radius mapping table. Since 3200 is greater than 1024 pixels, the dynamic neighborhood radius threshold obtained from the table is 2.5 times the diagonal length of the candidate box. The candidate box has a width of 60 pixels, a height of 70 pixels, and a diagonal length of approximately 92 pixels. Therefore, the dynamic neighborhood radius threshold is approximately 230 pixels. The counter array counts the number of second candidate targets that simultaneously meet three conditions: a center Euclidean distance of less than 230 pixels, an area ratio falling between 0.3 and 3.0, and an overlap ratio exceeding 0.1. The result shows that the number of neighboring candidate targets for candidate target number 15 is 8. The dense region determination module reads a preset threshold of 3. Since 8 is greater than 3, the area where candidate target number 15 is located is marked as a dense region, and its dense region identifier and target identifier are output together to the overlap relationship matrix calculation unit. In this embodiment, the dense region determination module sets the preset quantity threshold to 3. The core basis for this is that the physical spatial distribution of densely growing edible fungi usually presents a cluster structure with a single mushroom stick as the unit. The number of naturally adjacent individuals around a single mature fruiting body is mostly concentrated in 2 to 4. If the threshold is set to 1 or 2, sparse targets that are edge-contact or accidentally side by side will be misjudged as dense regions, causing the overlapping relationship matrix calculation unit to consume excessive parallel array resources in unnecessary scenarios. Furthermore, the adaptive candidate suppression unit may erroneously increase the suppression threshold, thereby causing the false deletion of adjacent real individuals. If the threshold is set to 4 or 5, it is easy to miss typical small clusters composed of 3 closely adjacent mushroom buds, causing their adhesion mask to fail to trigger watershed splitting. Through grid search experiments on the validation set with the harmonic mean of recall and precision in dense areas as the target, a threshold of 3 was set to effectively filter out interference from targets that are completely separated in space or only slightly touched. At the same time, it ensures that each marked dense area contains at least 3 collectable individuals with actual adhesion or strong spatial proximity. This allows the calculation of the overlap relationship matrix and the separation of adhered targets to accurately locate severely overlapping areas with low computational overhead, achieving a globally optimal balance between processing accuracy and hardware efficiency.
[0122] After receiving the dense region identifier, the overlap relationship matrix calculation unit calculates the intersection-union ratio, center distance, and mask overlap rate for each candidate target pair within the dense region using a parallel comparison array. Taking the candidate target pair consisting of candidate target No. 15 and candidate target No. 22 as an example, the coordinates of candidate box No. 15 are (320, 280, 60, 70), and the coordinates of candidate box No. 22 are (310, 290, 55, 65). The area of the intersection region of the two boxes is 1850 square pixels, and the area of the union region is 3850 square pixels, resulting in an intersection-union ratio of 0.48. The Euclidean distance between the center points of the two boxes is approximately 14 pixels. The two instance masks overlap by a total of 126 pixels in the overlapping region, accounting for 18% and 22% of their respective mask areas, respectively. The larger value of 22% is taken as the mask overlap rate. After the calculation is completed, the intersection-to-union ratio of 0.48, the center distance of 14 pixels, and the mask overlap rate of 0.22 are written into the input register group of the data distribution register file, and simultaneously latched into the first output register group and the second output register group on the rising edge of the next clock cycle.
[0123] After the adaptive candidate suppression unit reads the overlap relationship data from the first output register group, the threshold generation subunit generates a dynamic suppression threshold for candidate target number 22. The local density value of the region where candidate target number 22 is located is 0.62, the candidate box area is 2980 pixels, the class confidence value is 210, the Euclidean distance between the center point of this candidate target and the center point of the current global candidate target with the highest confidence is 14 pixels, the preset maximum separation distance threshold is 300 pixels, and the preset maximum candidate box area is 102400 pixels. Since 14 is less than 300, the dynamic suppression threshold is calculated according to the piecewise linear mapping function: the baseline suppression threshold is 0.5 + 0.15 × 0.62, plus 0.10 × 2980 ÷ 102400, plus 0.10 × (1 - 210 ÷ 255), and then minus 0.05 × 14 ÷ 300. The result is 0.5 + 0.093 + 0.0029 + 0.0176 - 0.0023, which equals 0.6112. This is constrained within the range of 0.3 to 0.9, and the final dynamic suppression threshold is 0.61. The suppression execution subunit compares the category confidence values of candidate target 15 and candidate target 22. The confidence value of candidate target 15 is 228, while that of candidate target 22 is 210. Since candidate target 22 has a lower confidence value, the mask overlap rate of 0.22 is compared with the dynamic suppression threshold of candidate target 22 of 0.61. Since 0.22 is less than 0.61, candidate target 22 is determined to be a neighboring real individual of candidate target 15, and both are retained. After the adaptive candidate suppression unit performs the above deduplication suppression on all candidate target pairs, 67 real individuals are retained out of 82 candidate targets. Among these, three neighboring mushroom individuals that would otherwise be easily deleted by a fixed threshold are retained within the dense region of this frame image.
[0124] After receiving the 67 preserved real individual identifiers and their corresponding instance masks, the instance mask boundary correction unit extracts a sequence of closed contour points for each instance mask. Taking shiitake mushrooms No. 15 and No. 22 as examples, the contour of instance mask No. 15 contains 356 contour points, and the contour of instance mask No. 22 contains 312 contour points. The curvature calculation module calculates the curvature value of each point on both contours one by one, detecting 6 local maxima of curvature on contour No. 15 and 5 local maxima of curvature on contour No. 22, all of which are marked as depressions in the cap contour. In the overlapping area of adjacent masks, the boundary re-division module selects a pair of concave points with the smallest spatial Euclidean distance from the concave points of the cap outline of the two instance masks. The coordinates of the concave point of mask No. 15 are (352, 296) and the coordinates of the concave point of mask No. 22 are (348, 300). The Euclidean distance between the two points is approximately 5.7 pixels. The original boundary of the overlapping area is replaced by the straight line segment between the two points, and the corrected instance mask No. 15 and instance mask No. 22 are output.
[0125] After receiving the corrected masks, the connected component labeling module performs 8-neighbor connected component labeling on each of the 67 corrected masks, obtaining 71 connected components. Among them, corrected masks No. 15 and No. 22 were originally labeled as the same connected component due to edge adhesion, and the area of this connected component is 2850 pixels. The preset area threshold is 0.5% of the input image area, i.e., 2048 pixels. Since 2850 is greater than 2048, the connected component is determined to be an adhered connected component, and a splitting trigger signal is generated. The distance transformation module responds to the splitting trigger signal and performs Euclidean distance transformation on the adhered connected component to generate a distance map. The watershed control module obtains the coordinates of the center points of the two real individuals, No. 15 and No. 22, located within the adhered connected component from the candidate result receiving buffer unit as initial seed points. The seed point correction submodule searches for the pixel with the largest distance value on the distance map using a 15-pixel by 15-pixel rectangular search window for each initial seed point as the corrected seed point. The corrected seed points are located in the actual center area of the cap. The watershed control module performs watershed splitting using two corrected seed points, dividing the cohesive connected components into two independent regions, each containing one seed point, and outputting the split individual masks for the 15th and 22nd individuals.
[0126] The data framing module of the output unit reads the contour point sequence, target category, position coordinates, and defect identifier of 67 split individual masks from the output ports of the adhesion target splitting unit and the adaptive candidate suppression unit, and generates a formatted data frame for each retained individual. In this embodiment, the defect identifier is directly output by the classification branch of the preceding YOLOv8 customized inference model. Each candidate target corresponds to a 2-bit defect category code, with a code value of 0 corresponding to normal, 1 to mold, 2 to insect infestation, and 3 to mechanical damage. The post-processing device directly transmits this identifier to the output data frame.
[0127] Taking mushroom No. 15 as an example, the target category field of the data frame contains the product category identifier, the location coordinate field contains the center point coordinates and the bounding box quadruple, the mask data field contains the contour point sequence, the defect identifier field contains the normal category identifier, the frame header identifier field contains 0x7E, the frame length field contains the total number of bytes in the frame, and the cyclic redundancy check field contains the check value for all data. The interface controller reads the interface configuration register, which is set to binary 1, and selects the Ethernet physical layer interface. The protocol encapsulation module adds a 22-byte Ethernet Media Access Control frame header before formatting the data frame and adds a 4-byte frame check sequence to the end of the frame. After encapsulation, it is sent to the Ethernet control port of the intelligent harvesting robot through the media-independent interface. After receiving the data frame, the intelligent harvesting robot parses the spatial coordinates and mask contour of mushroom No. 15, and controls the end effector of the robotic arm to move to the target position to complete the precise harvesting. The total delay from image acquisition to control command issuance is less than 50 milliseconds, which meets the control timing requirements of real-time harvesting in the production line.
[0128] This embodiment provides a post-processing device for dense edible fungi target detection. By solidifying candidate result receiving and caching, local density calculation, parallel calculation of overlap relation matrix, adaptive candidate suppression, instance mask boundary correction, and sticky target splitting into a hardware pipeline architecture, it directly receives candidate box coordinates, category confidence, instance mask, and target center point data output by the front-end YOLOv8 customized inference model. All post-processing operations are completed with the cooperation of on-chip cache and parallel computing resources. This eliminates the scheduling overhead and latency bottleneck caused by serial execution and repeated access to external memory in non-maximum suppression and mask post-processing in general software solutions. The total latency from image acquisition to control command issuance is less than 50 milliseconds, meeting the real-time control requirements of intelligent harvesting and high-speed sorting of edible fungi. The adaptive candidate suppression unit introduces four variables for each candidate target through the threshold generation subunit: local density value, candidate box area, class confidence value, and Euclidean distance to the center point of the candidate with the highest confidence. Based on the piecewise linear mapping function, it dynamically generates a suppression threshold that adapts to the current local density and the geometric and confidence characteristics of the individual. In densely clustered regions, the suppression threshold is automatically increased to retain adjacent real individuals, and in sparse regions, the threshold is automatically decreased to effectively eliminate duplicate candidates. This significantly reduces the false deletion among dense individuals, so that 67 real individuals are correctly retained after deduplication suppression of 82 candidate targets. Adjacent individuals in dense regions that are originally easily deleted by fixed thresholds are completely retained. The instance mask boundary correction unit detects the cap contour depression points based on the local maxima of curvature in the contour point sequence. In the overlapping area of adjacent masks, it re-divides the mask boundary along the shortest path between the depression point pairs. The adhered target splitting unit divides the adhered connected regions with areas exceeding a preset threshold into independent individual masks through 8-neighbor connected component labeling, Euclidean distance transformation, and watershed segmentation after seed point correction. The two work together to eliminate the erroneous merging and boundary missegmentation between dense individuals, effectively improving the clarity of instance mask boundaries and the accuracy of individual counting. This provides the intelligent edible fungus harvesting robot with precise positioning coordinates and contour data for each harvestable sub-entity, and provides the automated sorting line with accurate individual quantity statistics. It achieves low-latency, high-precision post-processing capability for dense edible fungus target detection.
[0129] Example 3
[0130] Please see Figure 4 Another embodiment of the present invention provides: a post-processing method for dense edible fungi target detection, comprising:
[0131] S1. Receive candidate box coordinates, class confidence, instance mask and target center point from the front-end YOLOv8 customized inference model, and cache the received data in on-chip memory;
[0132] S2. Calculate the density of edible fungi targets in the local area based on the center distance, area and overlap ratio of the candidate boxes to identify dense areas;
[0133] S3. Within the dense region, the cross-union ratio, center distance, and mask overlap rate among each candidate target are calculated using a parallel comparison array.
[0134] S4. Based on the local density, candidate box size, category confidence and center point separation degree, dynamically generate suppression thresholds for each candidate target, and perform deduplication suppression on the candidate targets based on the dynamic suppression thresholds to distinguish duplicate detections from adjacent real individuals.
[0135] S5. For the instance mask corresponding to the preserved real individuals, the boundary is re-divided based on the cap outline edge features, the overlapping area of adjacent masks, and the outline depression points to obtain the corrected mask;
[0136] S6. When the modified mask contains multiple connected edible fungus individuals, the adhesion mask is split into independent individual masks by connecting component labeling and distance transformation, and the target category, location coordinates, split individual masks and defect identifiers are output.
[0137] The embodiments of the present invention have been described above with reference to the accompanying drawings. However, the present invention is not limited to the specific embodiments described above. The specific embodiments described above are merely illustrative and not restrictive. Those skilled in the art can make changes, modifications, substitutions and variations to the above embodiments under the guidance of the present invention without departing from the spirit and scope of the present invention. All of these variations are within the scope of protection of the present invention.
Claims
1. A post-processing device for intensive edible fungi target detection, characterized in that, include: The candidate result receiving and caching unit is used to receive candidate box coordinates, class confidence, instance mask and target center point from the front-end YOLOv8 customized inference model, and cache the received data in on-chip memory. The local density calculation unit calculates the density of edible fungi targets in a local area based on the distance between the center of the candidate box, the area, and the overlap ratio, in order to identify dense areas; The overlap relationship matrix calculation unit is used to calculate the cross-union ratio, center distance and mask overlap rate between each candidate target in the dense region by means of a parallel comparison array; An adaptive candidate suppression unit is used to dynamically generate suppression thresholds for each candidate target based on the local density, candidate box size, class confidence and center point separation degree, and perform deduplication suppression on the candidate targets based on the dynamic suppression thresholds to distinguish duplicate detections from adjacent real individuals. The instance mask boundary correction unit is used to re-divide the boundary of the instance mask corresponding to the preserved real individual based on the cap outline edge features, the overlapping area of adjacent masks, and the outline depression points to obtain the corrected mask; The adhesion target splitting unit is used to split the adhesion mask into independent individual masks by connecting component labeling and distance transformation when the modified mask contains multiple connected edible fungus individuals; The result output unit is used to output the target category, location coordinates, split individual mask, and defect identifier.
2. The post-processing device for intensive edible fungi target detection as described in claim 1, characterized in that, The adaptive candidate suppression unit includes a threshold generation subunit and a suppression execution subunit; the threshold generation subunit is used to generate a dynamic suppression threshold for each candidate target based on the local density value of the region where the candidate target is located, the candidate box area of the candidate target, the category confidence value, and the Euclidean distance between the candidate target and the center point of the current highest confidence candidate target. The suppression execution subunit, for a first candidate target and a second candidate target constituting a candidate target pair, obtains the cross-union ratio or mask overlap rate of the candidate target pair from the overlap relationship matrix. When the cross-union ratio or mask overlap rate is greater than the dynamic suppression threshold of the candidate target with lower confidence among the first candidate target and the second candidate target, the candidate target with lower confidence is suppressed; otherwise, both are retained, and the retained candidate target is marked as a real individual and a corresponding target identifier is generated.
3. The post-processing device for intensive edible fungi target detection as described in claim 2, characterized in that, The local density calculation unit includes a parallel distance calculation array, an area comparator array, an overlap determination array, a counter array, and a dense region determination module. The parallel distance calculation array is used to calculate the center Euclidean distance between the first candidate target and the second candidate target in parallel and output the distance calculation result. The area comparator array is used to calculate the ratio of the candidate box area of the first candidate target to the candidate box area of the second candidate target and output the area ratio result. The overlap determiner array is used to calculate the overlap ratio between the candidate bounding boxes of the first candidate target and the candidate bounding boxes of the second candidate target, and output the overlap ratio result.
4. The post-processing device for intensive edible fungi target detection as described in claim 3, characterized in that, The counter array is used to count the number of second candidate targets that simultaneously meet the following conditions for the first candidate target: the center Euclidean distance is less than a preset neighborhood radius threshold, the area ratio result falls within a preset area ratio interval, and the overlap ratio result exceeds a preset overlap threshold, and this number is taken as the number of neighboring candidate targets of the first candidate target. The dense region determination module is used to read the number of neighboring candidate targets of the first candidate target. When the number of neighboring candidate targets exceeds a preset threshold, the region where the first candidate target is located is marked as a dense region, and the dense region identifier is output to the overlap relationship matrix calculation unit.
5. The post-processing device for intensive edible fungi target detection as described in claim 4, characterized in that, The instance mask boundary correction unit includes a contour tracking module, a curvature calculation module, and a boundary re-division module; the contour tracking module is used to extract the contour point sequence of the corresponding instance mask with the retained real individual identifier as input, and output the coordinate values of each contour point in the contour point sequence. The curvature calculation module is used to calculate the curvature value of each contour point in the contour point sequence based on the coordinate value of the contour point and the coordinate value of the adjacent contour points, and to detect contour points in the contour point sequence whose curvature value is greater than the curvature values of the adjacent two contour points as local maxima of curvature, and to mark the local maxima of curvature as cap contour depression points.
6. The post-processing device for intensive edible fungi target detection as described in claim 5, characterized in that, The boundary re-division module is used to receive the position information of the overlapping area of the adjacent masks. Within the overlapping area of the adjacent masks, a pair of concave points with the smallest spatial Euclidean distance is selected from the cap contour concave points belonging to the first instance mask and the cap contour concave points belonging to the second instance mask to form a concave point pair. The straight line segment between the concave point pair is used as the new dividing boundary to replace the original boundary of the first instance mask and the second instance mask in the overlapping area of the adjacent masks, and the corrected mask of the first instance mask and the second instance mask is output.
7. The post-processing device for intensive edible fungi target detection as described in claim 6, characterized in that, The sticky target splitting unit includes a connected component marking module, a distance transformation module, and a watershed control module. The connected component marking module is used to receive the modified mask, perform connected component marking on the modified mask to obtain at least one connected component, calculate the area of each connected component, and when the area of any connected component is greater than a preset area threshold, mark the connected component as a sticky connected component and generate a splitting trigger signal. The distance transformation module is used to respond to the splitting trigger signal, perform Euclidean distance transformation on the sticky connected component, calculate the Euclidean distance from each pixel in the sticky connected component to the nearest background pixel, and generate a distance map.
8. The post-processing device for intensive edible fungi target detection as described in claim 7, characterized in that, The watershed control module is used to obtain the target center point coordinates of multiple real individuals located in the cohesive connected domain and retained by the adaptive candidate suppression unit from the candidate result receiving cache unit, and to perform watershed segmentation on the distance map using the multiple target center points as seed points, dividing the cohesive connected domain into independent regions, each containing a seed point, and outputting the split individual mask.
9. The post-processing device for intensive edible fungi target detection as described in claim 8, characterized in that, The result output unit includes a data framing module; the data framing module is used to read the contour point sequence of the split individual mask, read the target category, position coordinates and defect identifier corresponding to the retained real individual, write the target category of the same retained real individual into the target category field of the data frame, write the position coordinates into the position coordinate field, write the contour point sequence into the mask data field, write the defect identifier into the defect identifier field, and write the frame header identifier field, frame length field and cyclic redundancy check field into the data frame in sequence to generate a formatted data frame.
10. The post-processing device for intensive edible fungi target detection as described in claim 9, characterized in that, The local density calculation unit further includes a neighborhood radius adjustment module; the neighborhood radius adjustment module is used to read the candidate box area of the first candidate target from the candidate result receiving cache unit, use the candidate box area as the input address to query a preset area-radius mapping table, read the dynamic neighborhood radius threshold corresponding to the candidate box area from the area-radius mapping table, and write the dynamic neighborhood radius threshold into the neighborhood radius configuration register of the counter array; when the counter array counts the number of neighboring candidate targets for the first candidate target, it reads the dynamic neighborhood radius threshold from the neighborhood radius configuration register, compares the center Euclidean distance with the dynamic neighborhood radius threshold, and determines whether the second candidate target falls within the neighborhood range of the first candidate target.
11. The post-processing device for intensive edible fungi target detection as described in claim 10, characterized in that, The watershed control module includes a seed point correction submodule; the input of the seed point correction submodule is connected to the output of the distance transformation module for receiving the distance map, and the input of the seed point correction submodule is also connected to the output port of the candidate result receiving buffer unit for obtaining the coordinates of the multiple target center points as initial seed points; the seed point correction submodule is used to define a rectangular search window on the distance map with the coordinates of the initial seed point as the center for each initial seed point, and the width and height of the rectangular search window are preset search window width and height values.
12. The post-processing device for intensive edible fungi target detection as described in claim 11, characterized in that, The seed point correction submodule is further configured to: read the distance values of all pixels within the rectangular search window, compare the distance values of all pixels, determine the pixel with the largest distance value as the corrected seed point, and use the coordinates of the corrected seed point as the coordinates of the corrected seed point; the watershed control module uses the coordinates of all corrected seed points as seed points and performs watershed segmentation on the distance map; wherein, when there are multiple pixels with equal and maximum distance values within the rectangular search window, the seed point correction submodule selects the pixel with the smallest row coordinate and the smallest column coordinate as the corrected seed point.
13. A method for post-processing of dense edible fungi target detection, implemented based on the dense edible fungi target detection post-processing device according to any one of claims 1-12, characterized in that, include: The system receives candidate box coordinates, class confidence, instance mask, and target center point from the front-end YOLOv8 customized inference model and caches the received data in on-chip memory. The density of edible fungi targets in a local area is calculated based on the distance between the center of the candidate boxes, the area, and the overlap ratio to identify dense areas; Within the dense region, the cross-union ratio, center distance, and mask overlap rate among each candidate target are calculated using a parallel comparison array. Based on the local density, candidate box size, category confidence and center point separation, a suppression threshold is dynamically generated for each candidate target, and deduplication suppression is performed on the candidate targets based on the dynamic suppression threshold to distinguish duplicate detections from adjacent real individuals. For the instance mask corresponding to the preserved real individuals, the boundary is re-divided based on the cap outline edge features, the overlapping area of adjacent masks, and the outline depression points to obtain the corrected mask; When the modified mask contains multiple connected edible fungus individuals, the adhesion mask is split into independent individual masks by connecting component labeling and distance transformation.
Citation Information
Patent Citations
A method and device for detecting the growth of edible fungi mycelium
CN117495802B
Edible fungus quality detection method based on spectral characteristics
CN118533816B