A clam body size character measurement method and system based on improved YOLO and SAM2

CN122368797BActive Publication Date: 2026-09-11INST OF OCEANOLOGY - CHINESE ACAD OF SCI
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202610829636.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2026-06-10
Publication Date
2026-09-11
Estimated Expiration
2046-06-10

AI Technical Summary

Technical Problem

[0007]本发明目的是提供一种基于改进YOLO架构与SAM2实例分割模型的蛤类体尺性状测量方法,旨在解决现有蛤类体尺性状测量中人工测量效率低的问题,进而实现了蛤类壳长和壳高的批量自动化测量,具有检测迅速、并行目标数多、测量精度高等优势

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122368797B_ABST
    Figure CN122368797B_ABST
Patent Text Reader

Abstract

The present application relates to the technical field of computer vision and image analysis, in particular to a clam body size trait measurement method and system based on improved YOLO and SAM2, comprising the following steps: acquiring a digital image containing clam individuals and standard reference objects; detecting clams and reference objects using a trained YOLO target detection model to generate candidate bounding boxes; using a SAM2 instance segmentation model to obtain accurate contour masks and extract boundary point sets based on the bounding boxes; identifying the reference objects and obtaining their four vertices using SAM2, establishing a pixel-physical calibration factor and performing geometric correction; calculating shell length and shell height based on the boundary point sets; and highlighting high-quality individuals by percentage after converting to physical dimensions and outputting the results. Through the cooperation of YOLO and SAM2, the present application realizes automatic and high-throughput measurement of clam body size traits in complex environments, improves small target detection accuracy and anti-interference ability, and is suitable for monitoring growth traits and parent selection in aquaculture and genetic breeding.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer vision and image analysis technology, and in particular to a method and system for measuring the body size characteristics of clams based on improved YOLO and SAM2. Background Technology

[0002] Growth trait analysis plays a vital role in agricultural science, ecological monitoring, and genetic breeding. With the development of precision agriculture and smart aquaculture technologies, higher demands are placed on the acquisition of phenotypic data from organisms. In shellfish research, morphological indicators such as shell length and shell height are key indicators for assessing growth status, conducting resource surveys, and selecting superior breeds.

[0003] Currently, the acquisition of phenotypic data of shellfish growth mainly relies on manual measurement using simple tools such as vernier calipers and balances. This method has the following inherent defects: (1) high labor intensity and low measurement efficiency; (2) easily affected by the subjective factors of the operator, resulting in poor data consistency; (3) limited by the speed of manual operation, it is difficult to achieve high-throughput collection of large-scale samples, resulting in insufficient data representativeness and restricting the accuracy and effectiveness of breeding selection.

[0004] With the rapid development of computer vision technology, automated measurement methods based on image processing have become a research hotspot. The YOLO series of models has become the mainstream method in the field of target detection due to its excellent real-time performance and detection accuracy. However, the general YOLO model has the following shortcomings when applied to shell detection: (1) Small-sized shells lose detailed features, resulting in a high false negative rate; (2) It lacks an effective background suppression mechanism, and water stains, mud and sand and other interferences are easily misidentified as shells; (3) The conventional loss function is not sensitive to the positional shift of small targets, which affects the measurement accuracy.

[0005] SAM2 (Segment Anything Model 2), a new generation of general-purpose visual segmentation model launched by Meta, has zero-shot instance segmentation capabilities, accurately extracting object edges with only simple cues such as bounding boxes. However, when directly applied to high-resolution, multi-object shell images, it suffers from problems such as excessive memory consumption and slow processing speed.

[0006] Therefore, there is an urgent need for a high-precision, high-efficiency, and automated method for measuring the shell length and height of clams. Summary of the Invention

[0007] The purpose of this invention is to provide a method for measuring the body size of clams based on an improved YOLO architecture and the SAM2 instance segmentation model. This method aims to solve the problem of low efficiency in manual measurement of clam body size characteristics in existing methods, and thus realizes the batch automated measurement of clam shell length and shell height. It has the advantages of rapid detection, large number of parallel targets, and high measurement accuracy.

[0008] The technical solution adopted by the present invention to achieve the above objectives is: a method for measuring the body size characteristics of clams based on improved YOLO and SAM2, comprising the following steps:

[0009] Step S1: Obtain a digital image containing the individual clam to be tested and a standard reference object with known actual physical size;

[0010] Step S2: Using the trained YOLO object detection model, detect clam-like targets and reference objects in the digital image and generate candidate bounding boxes;

[0011] Step S3: Using the SAM2 instance segmentation model, with candidate bounding boxes as cues, segment the digital image, obtain the precise contour mask of each clam target, and extract the contour boundary point set;

[0012] Step S4: Identify reference objects in the digital image, use the SAM2 model to identify the positions of the four vertices of the reference object, establish a calibration factor based on the ratio of the pixel size of the reference object in the image to its actual physical size, and perform geometric correction on the image.

[0013] Step S5: Based on the contour boundary point set, calculate the maximum Euclidean distance between any two points in the point set as the pixel shell length, and calculate the maximum vertical distance as the pixel shell height using the line connecting the two ends of the shell length as the baseline.

[0014] Step S6: Use calibration factors to convert pixel shell length and pixel shell height into actual physical dimensions, highlight high-quality individuals according to preset percentiles, and output measurement results.

[0015] The YOLO target detection model is an improvement on the YOLO11 architecture and includes: a backbone network, a neck network, and a detection head;

[0016] The backbone network adopts a C3k2 structure, which adds a P2 detection layer to the YOLO11 backbone network. The P2 detection layer is located in the shallow layer of the network, and the feature map downsampling factor is 4, which is used to capture the edge, texture and shape details of small-sized shellfish.

[0017] The neck network is used for multi-scale feature fusion through a feature pyramid structure;

[0018] The detection head adopts an anchorless design and is used to output target detection results.

[0019] Step S2, the training steps for training the YOLO object detection model, are as follows:

[0020] Step S2-1: Collect shellfish images containing reference objects, annotate the bounding boxes of shellfish targets in the shellfish images, and define the category label as "shell"; perform data augmentation processing on the dataset;

[0021] Step S2-2: Divide the dataset into training and validation sets proportionally;

[0022] Steps S2-3: Load YOLO pre-trained weights and fine-tune them on the training dataset through transfer learning; use the AdamW optimizer, set the input image resolution, batch size, initial learning rate, and number of training epochs, and train using mixed precision.

[0023] The loss function used is GWPIoU, which is defined as follows:

[0024]

[0025]

[0026] in, The Gaussian weighting factor is used to adjust the contribution of targets of different sizes to the loss. The weight approaches 1 for small targets and approaches 0 for large targets. The absolute size of the target, in pixels, is used to calculate the weighting factor; and The width and height of the actual bounding box, in pixels; The attenuation coefficient controls the attenuation rate of the weight as the target size changes, and its value ranges from 16 to 64. It is a natural exponential function; The Gaussian-weighted perturbation crossover ratio is used to improve the loss function value; This is the intersection-union ratio between the predicted bounding box and the ground truth bounding box, with a value ranging from 0 to 1; The center point normalization offset is used to measure the deviation of the center point between the predicted box and the ground truth box.

[0027] Step S2-4: Monitor and evaluate the metrics, and save the optimal weights for subsequent reasoning.

[0028] The shellfish images include: clean background images and outdoor operation images containing changes in natural lighting, water stains, and mud and sand interference;

[0029] The data enhancements include Mosaic enhancement, Mixup enhancement, and HSV color perturbation.

[0030] Step S3 includes the following steps:

[0031] Step S3-1: Use the trained YOLO model to extract the coordinates of the shell bounding boxes and set the detection parameters; the detection parameters include image resolution, confidence threshold and IoU threshold;

[0032] Step S3-2: Input the bounding box coordinates as prompt information into the SAM2 model, and use a batch processing mechanism. Each batch has 10 to 50 detection boxes. After each batch is completed, perform memory garbage collection.

[0033] Step S3-3: Extract the outer contour of the generated binary mask, apply a polygon approximation algorithm with an approximation accuracy of 0.005 to 0.02 times the contour perimeter, and perform geometric filtering based on area and length-to-height ratio to obtain a simplified set of boundary feature points;

[0034] Step S3-4: Perform geometric filtering based on area threshold and length-to-height ratio threshold to exclude false detection targets.

[0035] The standard reference object is a standard red square card; identifying the reference object in the digital image includes the following steps:

[0036] Step S4-1: Convert the acquired image to the HSV color space, and lock the reference area according to the preset hue threshold range, saturation threshold range, and brightness threshold range;

[0037] Among them, the hue threshold range is 0~15 or 160~180, the saturation threshold range is 50~255, and the brightness threshold range is 50~255;

[0038] Step S4-2: Extract the outline of the reference object. Target reference objects are selected based on the proportion of the outline area to the image area being between 0.01% and 20% and the length-to-height ratio of the outline being less than 2.0. The pixel size of the target reference object is then obtained.

[0039] Step S4-3: Input the candidate bounding box of the reference object into the SAM2 model and identify the coordinates of the four vertices of the square;

[0040] Step S4-4: Establish the calibration factor, wherein the calibration factor k is:

[0041]

[0042] Step S4-5: Calculate the perspective transformation matrix based on the coordinates of the four vertices, rotate and correct the image to correct the skewed reference object into a normal square; when a shooting angle deviation is detected, apply the perspective transformation matrix to correct the image and eliminate trapezoidal distortion.

[0043] Geometric dimension calculations based on boundary point sets, including shell length and shell height calculations:

[0044] The shell length is calculated as follows: traverse the boundary point set, calculate the Euclidean distance between any two points, and take the maximum distance value as the pixel shell length. The calculation formula is:

[0045]

[0046] Actual shell length: = ;

[0047] in, The pixel shell length of the target object to be tested, in pixels; The index number of any two distinct points in the set of contour boundary points; Let x and y be the x and y coordinates of the i-th contour boundary point, in pixels; Let x and y be the x and y coordinates of the j-th contour boundary point, in pixels; This is a maximum value function, representing the maximum value of the Euclidean distance after traversing all pairs of points;

[0048] The shell height is calculated as follows: taking the line connecting the two ends of the shell length as the baseline, the boundary point set is divided into two point clusters on both sides. The feature point with the smallest distance difference from the two ends of the shell length is found in the point clusters on both sides, and the distance between the two points is calculated as the pixel shell height of the shell.

[0049] Implemented using the vector cross product method: with the line connecting the endpoints of the shell length as the reference vector: The distribution of the remaining point set can be determined using the cross product of vectors.

[0050]

[0051] in, The reference vector is formed by the coordinate difference between the two endpoints of the shell length, and its direction is from... point to ; Let the coordinates of the starting point of the shell length be denoted as ( ); Let the coordinates of the end point of the shell length be denoted as ( ); This is the cross product value, used to determine which side of the baseline the contour point is located on; Let be the coordinates of any point in the boundary point set, denoted as ( );

[0052] The contour point set is divided into two point clusters based on the cross product sign, where For one side, On the other side, take the point furthest from the baseline on both sides as the height endpoint, and calculate the Euclidean distance between these two points as the pixel shell height. The actual shell height is: ;

[0053] in, The actual physical shell height of the target object, in millimeters; The pixel height of the target under test, in pixels.

[0054] In step S6, the step of highlighting high-quality individuals according to preset percentiles specifically involves:

[0055] Based on the percentile ranking of shell length or shell height, individuals that rank in the top N% for both shell length and shell height are highlighted with the first color, individuals that rank in the top N% for only shell length are highlighted with the second color, and individuals that rank in the top N% for only shell height are highlighted with the third color, where N is a preset percentage.

[0056] The output measurement results include automatically generated data reports, which include: raw data, data sorted by shell length, data sorted by shell height, data filtered by preset percentiles, and statistical information including total sample size, minimum value, maximum value, average value, and standard deviation.

[0057] The output measurement results also include visual annotations: the measurement results are plotted on the digital image in real time, with shell length marked by blue line segments and shell height marked by green line segments; the numerical display is adaptively adjusted in size; and the total number of identified individuals, shell length and shell height values ​​for each individual are displayed.

[0058] A clam body size measurement system based on improved YOLO and SAM2, used to implement the clam body size measurement method based on improved YOLO and SAM2 as described above, the system comprising:

[0059] The data acquisition module is used to acquire digital images containing seashells and standard reference objects;

[0060] The model pre-training module is used to construct a training dataset containing shell images from multiple angles and scales. Based on the YOLO11 architecture, it utilizes a backbone network with an integrated C3k2 structure, an anchor-free detection head, and a neck network for multi-scale feature fusion. The weight parameters are optimized through transfer learning, and the GWPIoU loss function is used to optimize the detection accuracy of small-sized shell targets.

[0061] The automatic calibration and geometric correction module is used to set a standard reference object within the shooting area to ensure that the reference object and the shell are on the same plane; it automatically locks the reference object using HSV color space conversion and morphological processing, identifies the four vertices of the reference object using the SAM2 model, and calculates the calibration factor k; it then performs perspective transformation based on the four vertices to correct trapezoidal distortion in the image.

[0062] The shell recognition and pixel-level segmentation module is used to output candidate bounding boxes using a trained YOLO model and set detection parameters. The bounding box coordinates are used as prompts to input the SAM2 model, which is processed in batches with adjustable batch size. After generating a mask, a polygon approximation algorithm is used to extract the boundary feature point set with an approximation accuracy of 0.005 to 0.02 times the contour perimeter. Geometric filtering is performed based on area and length-to-height ratio to obtain a simplified boundary feature point set.

[0063] The precise morphological parameter calculation module is used to calculate the shell length using the extreme distance method, calculate the shell height using the vector cross product method based on the shell length baseline, and convert it into the actual physical size through the scaling factor k.

[0064] The intelligent data output module is used to annotate shell length and shell height on images; it introduces a percentile filtering mechanism to highlight individuals that meet the criteria; and it automatically generates data reports containing raw data, sorting results, and statistical information.

[0065] The present invention has the following beneficial effects and advantages:

[0066] 1. This invention automates the entire process from target localization, calibration, segmentation to statistical derivation. Through a batch processing mechanism and memory reclamation strategy, it solves the memory overflow problem of large visual models when processing high-resolution, multi-target images, significantly improving the measurement efficiency of large-scale field operations. A single batch can automatically process hundreds of shell samples.

[0067] 2. This invention utilizes HSV color space analysis to achieve automatic identification of reference objects, effectively overcoming interference from complex environments such as outdoor lighting variations, water stains, and muddy backgrounds on spatial calibration. Combined with image geometric correction technology, it eliminates perspective distortion caused by handheld shooting or non-perpendicular angles, lowering the shooting threshold for outdoor operations and ensuring the reliability of the measurement benchmark.

[0068] 3. The system of this invention incorporates automatic screening logic based on percentiles (e.g., the top 10%), which can automatically identify and highlight superior individuals (those with both excellent shell length and shell height) in the population. It supports the automatic generation of multi-dimensional Excel reports containing raw data, distribution statistics, and ranking results, providing intuitive data support for parent selection, screening of superior traits, and genetic breeding, and significantly improving the efficiency of breeding work.

[0069] 4. This invention is not dependent on any specific shellfish species and can be widely applied to the measurement of body size of various intertidal shellfish such as clams, mussels, and scallops. Its lightweight design and automated process are particularly suitable for outdoor aquaculture surveys and genetic breeding scenarios, providing reliable and efficient technical support for the analysis of shellfish growth traits.

[0070] 5. This invention is particularly applicable to the selection of parent shellfish in outdoor aquaculture surveys and genetic breeding, providing reliable technical support for the rapid assessment of shellfish growth traits and the selection of superior individuals. Attached Figure Description

[0071] Figure 1 Overall method flowchart of the present invention;

[0072] Figure 2 Diagram of the improved YOLO11 network structure of this invention;

[0073] Figure 3 Flowchart of the automatic calibration module execution method of the present invention;

[0074] Figure 4 Flowchart of the shell recognition and segmentation module execution method of the present invention;

[0075] Figure 5 Schematic diagram illustrating the geometric calculation principle of shell length and height in this invention. Detailed Implementation

[0076] The present invention will now be described in further detail with reference to the accompanying drawings and embodiments.

[0077] Example 1:

[0078] This embodiment uses clams (commonly known as hard clams) as the measurement object, but the scope of protection of this invention is not limited to this, and can be widely applied to the measurement of body size characteristics of tidal flat shellfish (such as clams, scallops, etc.).

[0079] like Figure 1 The diagram shown is a flowchart of the overall method of the present invention; a method for measuring the body size of clams based on YOLO and SAM2 includes the following steps:

[0080] Step S1: Obtain a digital image containing the individual clam to be tested and a standard reference object with known actual physical size;

[0081] In step S1, in this embodiment, the image acquisition device is a smartphone, and the shooting parameters are: focal length 24 mm, aperture F1.78, and resolution 4284×2712 pixels. During shooting, a standard red square card of known physical size (side length 20 mm × 20 mm) is placed within the field of view, ensuring that the card and the clam to be measured are on the same plane.

[0082] Step S2: Using the trained YOLO object detection model, detect clam-like targets and reference objects in the digital image and generate candidate bounding boxes;

[0083] The YOLO object detection model is an improvement on the YOLO11 architecture, which includes: a backbone network, a neck network, and a detection head;

[0084] The backbone network adopts the C3k2 structure, which adds a P2 detection layer to the backbone network of YOLO11. The P2 detection layer is located in the shallow layer of the network, and the feature map downsampling factor is 4, which is used to capture the edge, texture and shape details of small-sized shellfish. The input resolution is set to 1024×1024 pixels (training) or 1280×1280 pixels (inference).

[0085] The neck network is used for multi-scale feature fusion through a feature pyramid structure;

[0086] The detection head adopts an anchor-free design for outputting target detection results.

[0087] In step S2, the training steps for training the YOLO object detection model are as follows:

[0088] Step S2-1: Collect shellfish images containing reference objects, annotate the bounding boxes of shellfish targets in the shellfish images, and define the category label as "shell"; perform data augmentation processing on the dataset;

[0089] Step S2-2: Divide the dataset into training and validation sets in an 8:2 ratio;

[0090] Step S2-3: Load the YOLO pre-trained weights and fine-tune them on the training dataset through transfer learning; use the AdamW optimizer, set the input image resolution, batch size, initial learning rate, and number of training epochs, and use mixed precision training; in this embodiment, the initial learning rate is set to 0.001.

[0091] The training parameters are set as shown in Table 1:

[0092] Table 1 Training Parameter Table

[0093] Number of training rounds 300 rounds The P2 structure requires more time to converge. Input image size 1024×1024 pixels Leveraging the detail capture capabilities of P2 layer Batch size 4 Balancing GPU memory and training speed Initial learning rate 0.01 Using AdamW optimizer Mixed precision training Open Reduce video memory usage

[0094] The loss function used in the object detection network is GWPIoU (Gaussian-Weighted Perturbation IoU), which is defined as:

[0095]

[0096]

[0097] in, The Gaussian weighting factor is used to adjust the contribution of targets of different sizes to the loss. The weight approaches 1 for small targets and approaches 0 for large targets. The absolute size of the target, in pixels, is used to calculate the weighting factor; and The width and height of the actual bounding box, in pixels; The attenuation coefficient controls the attenuation rate of the weight as the target size changes, and its value ranges from 16 to 64. It is a natural exponential function; The Gaussian-weighted perturbation crossover ratio is used to improve the loss function value; This is the intersection-union ratio between the predicted bounding box and the ground truth bounding box, with a value ranging from 0 to 1; The center point normalization offset is used to measure the deviation of the center point between the predicted box and the ground truth box.

[0098] Step S2-4: Monitor and evaluate the metrics, and save the optimal weights for subsequent reasoning.

[0099] Step S3: Using the SAM2 instance segmentation model, with candidate bounding boxes as cues, segment the digital image, obtain the precise contour mask of each clam target, and extract the contour boundary point set;

[0100] Among them, such as Figure 4 As shown, step S3 in this embodiment includes the following steps:

[0101] Step S3-1: Use the trained YOLO model to extract the coordinates of the shell bounding box and set the detection parameters; the detection parameters include image resolution, confidence threshold and IoU threshold; in this embodiment, they are set as follows: image size 1280×1280 pixels, confidence threshold 0.35, IoU threshold 0.5;

[0102] Step S3-2: Input the bounding box coordinates as prompt information into the SAM2 model, and use a batch processing mechanism. Each batch contains 10 to 50 detection boxes. After each batch is completed, perform memory garbage collection.

[0103] Step S3-3: Extract the outer contour from the generated binary mask, apply the polygon approximation algorithm (Douglas-Peucker algorithm), with an approximation accuracy of 0.005~0.02 times the contour perimeter (set to 0.01 in this embodiment), perform geometric filtering based on area and length-to-height ratio to obtain a simplified set of boundary feature points, effectively suppressing detail noise caused by edge water stains;

[0104] Step S3-4: Perform geometric filtering based on area threshold and length-to-height ratio threshold to exclude false detection targets.

[0105] In this embodiment, the area threshold is 150~8000 pixels and the aspect ratio is <1.8.

[0106] Step S4: Identify reference objects in the digital image, use the SAM2 model to identify the positions of the four vertices of the reference object, establish a calibration factor based on the ratio of the pixel size of the reference object in the image to its actual physical size, and perform geometric correction on the image.

[0107] like Figure 3 As shown, in this embodiment, the standard reference object is a standard red square card;

[0108] Identifying reference objects in a digital image includes the following steps:

[0109] Step S4-1: Convert the acquired image to the HSV color space, and lock the reference area according to the preset hue threshold range, saturation threshold range, and brightness threshold range;

[0110] Among them, the hue threshold range is 0~15 or 160~180, the saturation threshold range is 50~255, and the brightness threshold range is 50~255;

[0111] Step S4-2: Extract the outline of the reference object. Target reference objects are selected based on the proportion of the outline area to the image area being between 0.01% and 20% and the length-to-height ratio of the outline being less than 2.0. The pixel size of the target reference object is then obtained.

[0112] Step S4-3: Input the candidate bounding box of the reference object into the SAM2 model and identify the coordinates of the four vertices of the square;

[0113] Step S4-4: Establish the calibration factor, wherein the calibration factor k is:

[0114]

[0115] Step S4-5: Calculate the perspective transformation matrix based on the coordinates of the four vertices, rotate and correct the image to correct the skewed reference object into a normal square; when a shooting angle deviation is detected, apply the perspective transformation matrix to correct the image and eliminate trapezoidal distortion.

[0116] Step S5: Based on the contour boundary point set, calculate the maximum Euclidean distance between any two points in the point set as the pixel shell length, and calculate the maximum vertical distance as the pixel shell height using the line connecting the two ends of the shell length as the baseline.

[0117] like Figure 5 As shown, the geometric dimensions are calculated based on the boundary point set, including shell length and shell height:

[0118] The shell length is calculated as follows: Traverse the boundary point set, calculate the Euclidean distance between any two points, and take the maximum distance value as the pixel shell length. The calculation formula is:

[0119]

[0120] Actual shell length: = ;

[0121] in, The pixel shell length of the target object to be tested, in pixels; The index number of any two distinct points in the set of contour boundary points; Let x and y be the x and y coordinates of the i-th contour boundary point, in pixels; Let x and y be the x and y coordinates of the j-th contour boundary point, in pixels; This is a maximum value function, representing the maximum value of the Euclidean distance after traversing all pairs of points;

[0122] The shell height is calculated as follows: taking the line connecting the two ends of the shell length as the baseline, the boundary point set is divided into two point clusters on both sides. The feature point with the smallest distance difference from the two ends of the shell length is found in the point clusters on both sides, and the distance between the two points is calculated as the pixel shell height of the shell.

[0123] Implemented using the vector cross product method: with the line connecting the endpoints of the shell length as the reference vector: The distribution of the remaining point set can be determined using the cross product of vectors.

[0124]

[0125] in, The reference vector is formed by the coordinate difference between the two endpoints of the shell length, and its direction is from... point to ; Let the coordinates of the starting point of the shell length be denoted as ( ); Let the coordinates of the end point of the shell length be denoted as ( ); This is the cross product value, used to determine which side of the baseline the contour point is located on; Let be the coordinates of any point in the boundary point set, denoted as ( );

[0126] The contour point set is divided into two point clusters based on the cross product sign, where For one side, On the other side, take the point furthest from the baseline on both sides as the height endpoint, and calculate the Euclidean distance between these two points as the pixel shell height. The actual shell height is: ;

[0127] in, The actual physical shell height of the target object, in millimeters; The pixel height of the target under test, in pixels.

[0128] Step S6: Use calibration factors to convert pixel shell length and pixel shell height into actual physical dimensions, highlight high-quality individuals according to preset percentiles, and output measurement results.

[0129] This embodiment highlights high-quality individuals based on preset percentiles and outputs the measurement results, specifically:

[0130] Based on percentile rankings of shell length or shell height, individuals ranking in the top N% for both shell length and shell height are highlighted with the first color; individuals ranking in the top N% for shell length only are highlighted with the second color; and individuals ranking in the top 10% for shell height only are highlighted with the third color. The output measurement results include automatically generated data reports, which include: raw data, data sorted by shell length, data sorted by shell height, data filtered by preset percentiles, and statistical information including the total sample size, minimum value, maximum value, mean, and standard deviation.

[0131] The output measurement results also include visual annotations, generating two data tables, as shown in Tables 2 and 3 respectively:

[0132] Table 2 Data Output Excel Report (1)

[0133]

[0134] Table 3 Data Output Excel Report (2)

[0135]

[0136] Experimental verification: Experimental results on a self-built clam dataset show that, compared with the benchmark YOLO model, the method of the present invention improves mAP@0.5 by 4.3%, reduces the number of parameters by 25%, and reduces the shell length measurement error by about 30%, thus verifying the effectiveness of the method of the present invention.

[0137] Example 2:

[0138] To realize a method for measuring the body size traits of clams based on improved YOLO and SAM2, this embodiment provides a clam body size trait measurement system based on improved YOLO and SAM2, the system comprising:

[0139] The data acquisition module is used to acquire digital images containing seashells and standard reference objects;

[0140] The model pre-training module is used to construct a training dataset containing shell images from multiple angles and scales. Based on the YOLO11 architecture, it utilizes a backbone network with an integrated C3k2 structure, an anchor-free detection head, and a neck network for multi-scale feature fusion. The weight parameters are optimized through transfer learning, and the GWPIoU loss function is used to optimize the detection accuracy of small-sized shell targets.

[0141] The automatic calibration and geometric correction module is used to set a standard reference object within the shooting area to ensure that the reference object and the shell are on the same plane; it automatically locks the reference object using HSV color space conversion and morphological processing, identifies the four vertices of the reference object using the SAM2 model, and calculates the calibration factor k; it then performs perspective transformation based on the four vertices to correct trapezoidal distortion in the image.

[0142] The shell recognition and pixel-level segmentation module is used to output candidate bounding boxes using a trained YOLO model and set detection parameters. The bounding box coordinates are used as prompts to input the SAM2 model, which is processed in batches with adjustable batch size. After generating a mask, a polygon approximation algorithm is used to extract the boundary feature point set with an approximation accuracy of 0.005 to 0.02 times the contour perimeter. Geometric filtering is performed based on area and length-to-height ratio to obtain a simplified boundary feature point set.

[0143] The precise morphological parameter calculation module is used to calculate the shell length using the extreme distance method, calculate the shell height using the vector cross product method based on the shell length baseline, and convert it into the actual physical size through the scaling factor k.

[0144] The intelligent data output module is used to annotate shell length and shell height on images; it introduces a percentile filtering mechanism to highlight individuals that meet the criteria; and it automatically generates data reports containing raw data, sorting results, and statistical information.

[0145] This invention provides a method and system for measuring the body size of clams based on improved YOLO and SAM2, applicable to aquaculture, marine biological resource surveys, and genetic breeding. This method requires no expensive equipment; it can achieve automated batch measurement of shell sizes using only images captured by a regular smartphone, automatically processing hundreds of shell samples per batch. Through HSV color space calibration and perspective correction, it effectively overcomes interference from complex environmental factors such as outdoor lighting variations and waterlogging / siltation. Batch processing and memory reclamation strategies solve the segmentation bottleneck of high-resolution multi-target images. This invention is particularly suitable for field operations involving tidal flat shellfish, germplasm resource assessment, and parent selection, demonstrating significant economic benefits and promising application prospects. Those skilled in the art should understand that various optimizations and improvements can be made to the technical solution without departing from the principles of this invention, and all such modifications fall within the scope of protection of this invention.

[0146] Those skilled in the art will understand that the above description is merely a preferred embodiment of the present invention, and the features described in the various embodiments of this disclosure can be combined or combined in various ways, even if such combinations or combinations are not explicitly described in this disclosure. This is not intended to limit the present invention. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art can still modify the technical solutions described in the foregoing embodiments or make equivalent substitutions for some of the technical features. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.

Claims

1. A method for measuring the body size of clams based on improved YOLO and SAM2, characterized in that, Includes the following steps: Step S1: Obtain a digital image containing the individual clam to be tested and a standard reference object with known actual physical size; Step S2: Using the trained YOLO object detection model, detect clam-like targets and reference objects in the digital image and generate candidate bounding boxes; Step S2, the training steps for training the YOLO object detection model, are as follows: Step S2-1: Collect shellfish images containing reference objects, annotate the bounding boxes of shellfish targets in the shellfish images, and define the category label as "shell"; perform data augmentation processing on the dataset; Step S2-2: Divide the dataset into training and validation sets proportionally; Steps S2-3: Load YOLO pre-trained weights and fine-tune them on the training dataset through transfer learning; use the AdamW optimizer, set the input image resolution, batch size, initial learning rate, and number of training epochs, and train with mixed precision. The loss function used is GWPIoU, which is defined as follows: ; ; in, The Gaussian weighting factor is used to adjust the contribution of targets of different sizes to the loss. The weight approaches 1 for small targets and approaches 0 for large targets. The absolute size of the target, in pixels, is used to calculate the weighting factor; and The width and height of the actual bounding box, in pixels; The attenuation coefficient controls the attenuation rate of the weight as the target size changes, and its value ranges from 16 to 64. It is a natural exponential function; The Gaussian-weighted perturbation crossover ratio is used to improve the loss function value; This is the intersection-union ratio between the predicted bounding box and the ground truth bounding box, with a value ranging from 0 to 1; The center point normalization offset is used to measure the deviation of the center point between the predicted box and the ground truth box. Step S2-4: Monitor and evaluate the indicators, and save the optimal weights for subsequent reasoning; The shellfish images include: clean background images and outdoor operation images containing changes in natural lighting, water stains, and mud and sand interference; The data enhancements include Mosaic enhancement, Mixup enhancement, and HSV color perturbation; Step S3: Using the SAM2 instance segmentation model, with candidate bounding boxes as cues, segment the digital image, obtain the precise contour mask of each clam target, and extract the contour boundary point set; Step S4: Identify reference objects in the digital image, use the SAM2 model to identify the positions of the four vertices of the reference objects, establish a calibration factor based on the ratio of the pixel size of the reference object in the image to its actual physical size, and perform geometric correction on the image. Step S5: Based on the contour boundary point set, calculate the maximum Euclidean distance between any two points in the point set as the pixel shell length, and calculate the maximum vertical distance as the pixel shell height using the line connecting the two ends of the shell length as the baseline. Step S6: Use calibration factors to convert pixel shell length and pixel shell height into actual physical dimensions, highlight high-quality individuals according to preset percentiles, and output measurement results.

2. The method for measuring the body size of clams based on improved YOLO and SAM2 according to claim 1, characterized in that, The YOLO target detection model is an improvement on the YOLO11 architecture and includes: a backbone network, a neck network, and a detection head; The backbone network adopts a C3k2 structure, which adds a P2 detection layer to the YOLO11 backbone network. The P2 detection layer is located in the shallow layer of the network, and the feature map downsampling factor is 4, which is used to capture the edge, texture and shape details of small-sized shellfish. The neck network is used for multi-scale feature fusion through a feature pyramid structure; The detection head adopts an anchorless design and is used to output target detection results.

3. The method for measuring the body size of clams based on improved YOLO and SAM2 according to claim 1, characterized in that, Step S3 includes the following steps: Step S3-1: Use the trained YOLO model to extract the coordinates of the shell bounding boxes and set the detection parameters; the detection parameters include image resolution, confidence threshold and IoU threshold; Step S3-2: Input the bounding box coordinates as prompt information into the SAM2 model, and use a batch processing mechanism. Each batch has 10 to 50 detection boxes. After each batch is completed, perform memory garbage collection. Step S3-3: Extract the outer contour of the generated binary mask, apply a polygon approximation algorithm with an approximation accuracy of 0.005 to 0.02 times the contour perimeter, and perform geometric filtering based on area and length-to-height ratio to obtain a simplified set of boundary feature points; Step S3-4: Perform geometric filtering based on area threshold and length-to-height ratio threshold to exclude false detection targets.

4. The method for measuring the body size of clams based on improved YOLO and SAM2 according to claim 1, characterized in that, The standard reference object is a standard red square card; identifying the reference object in the digital image includes the following steps: Step S4-1: Convert the acquired image to the HSV color space, and lock the reference area according to the preset hue threshold range, saturation threshold range, and brightness threshold range; Among them, the hue threshold range is 0~15 or 160~180, the saturation threshold range is 50~255, and the brightness threshold range is 50~255; Step S4-2: Extract the outline of the reference object. Target reference objects are selected based on the proportion of the outline area to the image area being between 0.01% and 20% and the length-to-height ratio of the outline being less than 2.

0. The pixel size of the target reference object is then obtained. Step S4-3: Input the candidate bounding box of the reference object into the SAM2 model and identify the coordinates of the four vertices of the square; Step S4-4: Establish the calibration factor, wherein the calibration factor k is: ; Step S4-5: Calculate the perspective transformation matrix based on the coordinates of the four vertices, rotate and correct the image to correct the skewed reference object into a normal square; when a shooting angle deviation is detected, apply the perspective transformation matrix to correct the image and eliminate trapezoidal distortion.

5. A method for measuring the body size of clams based on improved YOLO and SAM2 according to claim 4, characterized in that, Geometric dimension calculations based on boundary point sets, including shell length and shell height calculations: The shell length is calculated as follows: traverse the boundary point set, calculate the Euclidean distance between any two points, and take the maximum distance value as the pixel shell length. The calculation formula is: ; Actual shell length: = ; in, The pixel shell length of the target object to be tested, in pixels; The index number of any two distinct points in the set of contour boundary points; Let x and y be the x and y coordinates of the i-th contour boundary point, in pixels; Let x and y be the x and y coordinates of the j-th contour boundary point, in pixels; This is a maximum value function, representing the maximum value of the Euclidean distance after traversing all pairs of points; The shell height is calculated as follows: taking the line connecting the two ends of the shell length as the baseline, the boundary point set is divided into two point clusters on both sides. The feature point with the smallest distance difference from the two ends of the shell length is found in the point clusters on both sides, and the distance between the two points is calculated as the pixel shell height of the shell. Implemented using the vector cross product method: with the line connecting the endpoints of the shell length as the reference vector: The distribution of the remaining point set can be determined using the cross product of vectors. ; in, The reference vector is formed by the coordinate difference between the two endpoints of the shell length, and its direction is from... point to ; Let the coordinates of the starting point of the shell length be denoted as ( ); Let the coordinates of the end point of the shell length be denoted as ( ); This is the cross product value, used to determine which side of the baseline the contour point is located on; Let be the coordinates of any point in the boundary point set, denoted as ( ); The contour point set is divided into two point clusters based on the cross product sign, where For one side, On the other side, take the point furthest from the baseline on both sides as the height endpoint, and calculate the Euclidean distance between these two points as the pixel shell height. The actual shell height is: ; in, The actual physical shell height of the target object, in millimeters; The pixel height of the target under test, in pixels.

6. The method for measuring the body size of clams based on improved YOLO and SAM2 according to claim 1, characterized in that, In step S6, the step of highlighting high-quality individuals according to preset percentiles specifically involves: Based on the percentile ranking of shell length or shell height, individuals that rank in the top N% for both shell length and shell height are highlighted with the first color, individuals that rank in the top N% for only shell length are highlighted with the second color, and individuals that rank in the top N% for only shell height are highlighted with the third color, where N is a preset percentage.

7. The method for measuring the body size of clams based on improved YOLO and SAM2 according to claim 1, characterized in that, The output measurement results include automatically generated data reports, which include: raw data, data sorted by shell length, data sorted by shell height, data filtered by preset percentiles, and statistical information including total sample size, minimum value, maximum value, average value, and standard deviation. The output measurement results also include visual annotations: the measurement results are plotted on the digital image in real time, with shell length marked by blue line segments and shell height marked by green line segments; the numerical display is adaptively adjusted in size; and the total number of identified individuals, shell length and shell height values ​​for each individual are displayed.

8. A clam body size measurement system based on improved YOLO and SAM2, characterized in that, For implementing the clam body size measurement method based on improved YOLO and SAM2 as described in any one of claims 1 to 7, the system comprises: The data acquisition module is used to acquire digital images containing seashells and standard reference objects; The model pre-training module is used to construct a training dataset containing shell images from multiple angles and scales. Based on the YOLO11 architecture, it utilizes a backbone network with an integrated C3k2 structure, an anchor-free detection head, and a neck network for multi-scale feature fusion. The weight parameters are optimized through transfer learning, and the GWPIoU loss function is used to optimize the detection accuracy of small-sized shell targets. The loss function used is GWPIoU, which is defined as follows: ; ; in, The Gaussian weighting factor is used to adjust the contribution of targets of different sizes to the loss. The weight approaches 1 for small targets and approaches 0 for large targets. The absolute size of the target, in pixels, is used to calculate the weighting factor; and The width and height of the actual bounding box, in pixels; The attenuation coefficient controls the attenuation rate of the weight as the target size changes, and its value ranges from 16 to 64. It is a natural exponential function; The Gaussian-weighted perturbation crossover ratio is used to improve the loss function value; This is the intersection-union ratio between the predicted bounding box and the ground truth bounding box, with a value ranging from 0 to 1; The center point normalization offset is used to measure the deviation of the center point between the predicted box and the ground truth box. The automatic calibration and geometric correction module is used to set a standard reference object within the shooting area to ensure that the reference object and the shell are on the same plane; it automatically locks the reference object using HSV color space conversion and morphological processing, identifies the four vertices of the reference object using the SAM2 model, and calculates the calibration factor k; it then performs perspective transformation based on the four vertices to correct trapezoidal distortion in the image. The shell recognition and pixel-level segmentation module is used to output candidate bounding boxes using a trained YOLO model and set detection parameters. The bounding box coordinates are used as prompts to input the SAM2 model, which is processed in batches with adjustable batch size. After generating a mask, a polygon approximation algorithm is used to extract the boundary feature point set with an approximation accuracy of 0.005 to 0.02 times the contour perimeter. Geometric filtering is performed based on area and length-to-height ratio to obtain a simplified boundary feature point set. The precise morphological parameter calculation module is used to calculate the shell length using the extreme distance method, calculate the shell height using the vector cross product method based on the shell length baseline, and convert it into the actual physical size through the scaling factor k. The extreme distance method calculates the shell length based on the geometric dimensions of the boundary point set, including shell length and shell height calculations. The shell length is calculated as follows: traverse the boundary point set, calculate the Euclidean distance between any two points, and take the maximum distance value as the pixel shell length. The calculation formula is: ; Actual shell length: = ; in, The pixel shell length of the target object to be tested, in pixels; The index number of any two distinct points in the set of contour boundary points; Let x and y be the x and y coordinates of the i-th contour boundary point, in pixels; Let x and y be the x and y coordinates of the j-th contour boundary point, in pixels; This is a maximum value function, representing the maximum value of the Euclidean distance after traversing all pairs of points; The shell height is calculated as follows: taking the line connecting the two ends of the shell length as the baseline, the boundary point set is divided into two point clusters on both sides. The feature point with the smallest distance difference from the two ends of the shell length is found in the point clusters on both sides, and the distance between the two points is calculated as the pixel shell height of the shell. Implemented using the vector cross product method: with the line connecting the endpoints of the shell length as the reference vector: The distribution of the remaining point set can be determined using the cross product of vectors. ; in, The reference vector is formed by the coordinate difference between the two endpoints of the shell length, and its direction is from... point to ; Let the coordinates of the starting point of the shell length be denoted as ( ); Let the coordinates of the end point of the shell length be denoted as ( ); This is the cross product value, used to determine which side of the baseline the contour point is located on; Let be the coordinates of any point in the boundary point set, denoted as ( ); The contour point set is divided into two point clusters based on the cross product sign, where For one side, On the other side, take the point furthest from the baseline on both sides as the height endpoint, and calculate the Euclidean distance between these two points as the pixel shell height. The actual shell height is: ; in, The actual physical shell height of the target object, in millimeters; The pixel height of the target under test, in pixels; The intelligent data output module is used to annotate shell length and shell height on images; it introduces a percentile filtering mechanism to highlight individuals that meet the criteria; and it automatically generates data reports containing raw data, sorting results, and statistical information.

Citation Information

Patent Citations

  • Image-based particle shape and granularity detection method for small particles

    CN121505416A