Body size and weight measuring method for live pig key point detection under feed component optimization condition
By improving the NLM algorithm and constructing the SwinT-SimCC model using Swin Transformer, the problem of low key point detection accuracy in non-standard pig postures was solved, achieving efficient and low-stress pig weight measurement and improving key point recognition rate and weight measurement accuracy.
Patent Information
- Application Number
- CN202610119108.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-01-28
- Publication Date
- 2026-05-12
AI Technical Summary
Existing technologies have low accuracy in detecting key points in non-standard pig postures, leading to errors in phenotypic parameter calculations. Traditional contact-based measurements are inefficient and prone to triggering stress responses.
An improved SimCC recognition model was adopted, combined with the Swin Transformer and a non-contact pig weight measurement method. By improving the NLM algorithm to increase the normal and curvature discrimination features, the SwinT-SimCC model was constructed. The spatial topological association of key points on the body surface was strengthened by the window attention mechanism and relative position encoding, and the weight was output by combining the M3 model.
It improved the key point recognition rate, reduced the error under non-standard postures, and achieved efficient and low-stress pig weight measurement. MSE was reduced by 12.8%, PSNR was increased by 5.7%, and the weight measurement error was 3.99%.
Smart Images

Figure CN122024281A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of non-contact measurement technology of pig weight, and more specifically, relates to a method for measuring the body size and weight of pigs under optimized feed composition conditions. Background Technology
[0002] In large-scale pig farming, accurately obtaining phenotypic parameters and weight information of pigs is crucial for growth monitoring, breeding management, and disease control. Traditional contact measurement methods are inefficient and easily induce stress in pigs. With technological advancements, non-contact measurement technologies, with their advantages of high efficiency and low stress, have become key to the intelligent transformation of pig farming.
[0003] In research on keypoint recognition methods for pigs, Liu Gang et al. improved YOLOv5-pose by first integrating the CBAM module, then replacing C3 with C3Ghost and introducing DyHead, significantly improving the keypoint detection accuracy under normal poses (mAP reached 92.6%). However, when pigs exhibit non-standard poses (such as lying down or sitting), the edge measurement point recognition is prone to deviation. Wang Xiaopin et al.'s YOLOv5DA-HRST improved multi-object scene adaptability through Mosaic9 enhancement and deformable convolution (mAP 81.5%), but keypoints still have certain errors under non-standard poses. These keypoint recognition deviations will directly lead to errors in the subsequent calculation of phenotypic parameters such as body length, body width, and body height. Kendall et al. (2017) found in their CornerNet study that even with the most advanced convolutional neural networks, keypoint detection accuracy decreases by about 23.7% under non-standard poses of farm animals such as cattle and pigs (such as lying down or wriggling). In their study "3D Pose Estimation", Pavllo et al. (2019) further demonstrated that mainstream algorithms based on RGB-D cameras will have key point recognition errors. Summary of the Invention
[0004] To address the aforementioned issues, this paper proposes a recognition model based on an improved SimCC algorithm and a non-contact method for measuring pig weight. First, normal and curvature discrimination features are introduced into the Non-Local Means (NLM) algorithm to improve point cloud denoising. Second, the original feature network of the Simple Coordinate Classification (SimCC) algorithm is replaced with a Swin Transformer to construct an attention-based Simple Coordinate Classification (SwinT-SimCC) algorithm. This algorithm utilizes window attention and relative position encoding to strengthen the spatial topological association of key points on the body surface, thereby improving the key point recognition rate. Finally, phenotypic parameters are calculated based on phenotypic key points, and the weight is output using the M3 model.
[0005] To achieve the above objectives, the present invention adopts the following technical solution:
[0006] A method for measuring body size and weight of pigs under optimized feed composition conditions, including the following steps:
[0007] Step 1: First, obtain the pig point cloud and denoise the point cloud by improving NLM. That is, after the depth camera takes pictures of the pig, 3D reconstruction is performed using the depth information to obtain the pig point cloud data, and point cloud denoising is performed based on the improved NLM.
[0008] Step 2: Use SwinT-SimCC to perform pose recognition and phenotypic key point recognition. Obtain the point cloud coordinates of the phenotypic key points through coordinate transformation and calculate the corresponding phenotypic parameters.
[0009] Step 3: Based on the recognition results of Step 2, obtain the camera coordinates, convert the camera coordinates into point cloud coordinates, and output the pig's body size based on the point cloud coordinates.
[0010] Step 4: Input the pig's body size data obtained in step S3, output the weight through the M3 model, and finally complete the non-contact measurement of the pig's weight.
[0011] Further optimization of this technical solution, in step 1, improves NLM for point cloud denoising, including:
[0012] The core formula of NLM is as follows:
[0013] (1)
[0014] (2)
[0015] (3)
[0016] In the formula, It is a point Denoising the 3D coordinate vector; It is a point The original three-dimensional coordinate vector; It is a point The set of candidate points; It is a point Point Similarity weights; It is a point Local descriptors; It is the filter strength; It is the normalization coefficient.
[0017] Since the NLM algorithm may only rely on simple geometric features for similarity calculation without considering more complex geometric features or higher-order structural information, it leads to insufficient denoising. To address this issue, the following two improvement strategies are proposed:
[0018] 1) Add outlier measurement features. The core steps and formulas after the update are as follows:
[0019] Step 1.1.1: Let I, N, and K be the three types of input channels, representing the point cloud projection intensity channel, normal channel, and curvature channel, respectively. The intermediate features obtained at layer t are as follows:
[0020] (4)
[0021] In the formula, K I (t) K N (t) K K (t) These are the convolution kernels for the three input channels of the t-th layer; F I (t) F N (t) F K (t) These are the feature tensors of the three input channels at the t-th layer;
[0022] Step 1.1.2: Perform fusion using weighted summation, as shown in the formula below:
[0023] (5)
[0024] In the formula, w I (t) w N (t) w K (t)These are the weights of the three input channels in the t-th layer; b (t) σ is the bias term; σ is the activation function. It is the output feature tensor of the t-th layer network, and the other variables are consistent with equation (4).
[0025] Step 1.1.3: Add residual terms to the output layer, as shown in the formula below:
[0026] (6)
[0027] In the formula, skip(t) is the residual of the skip connection, which prevents information loss. It is the intermediate feature tensor obtained by weighted fusion of multi-channel features in the t-th layer network, and the other variables are consistent with equation (4-5);
[0028] 2) Weight the mean squared error loss with the normal constraint loss and curvature constraint loss to construct a new loss function. The improved loss function... As shown below:
[0029] (7)
[0030] (8)
[0031] (9)
[0032] (10)
[0033] In the formula, L MSE L normal L curv These are the mean square error constraint term, the normal constraint term, and the curvature constraint term; λ mse , λ n , λ k They are L MSE L normal L curv The weighting coefficients, These are the actual 3D coordinates and the predicted 3D coordinates of the i-th point, respectively. These are the true normal vector and the predicted normal vector at the i-th point, respectively. These are the actual curvature value and the predicted curvature value at the i-th point, respectively, and the remaining variables are consistent with equations (1-3).
[0034] In a further optimization of this technical solution, the point cloud data of the pig in step 1 includes:
[0035] In daily pig farming, standing, lying, and sitting are the three most common postures of pigs. The spatial positions of phenotypic key points differ significantly under different postures, directly affecting the accuracy of phenotypic parameter calculation. To ensure that the model can accurately adapt to common scenarios in actual farming, it is necessary to train the model for these three common postures. Therefore, images of pigs with clear and complete back contours are first selected.
[0036] Key phenotypic features in pigs are divided into 7 categories, and their specific locations are shown below:
[0037] Chest width starting point CW0, chest width ending point CW1: These two points are located at the junction of the left and right forelimbs and the trunk, approximately one-third of the way up the height of the foreleg;
[0038] The starting point of hip width HW0 and the ending point of hip width HW1 are located at the top of the hip joint where the left and right hind limbs connect to the trunk, approximately at the upper half of the height of the hind leg.
[0039] Length0 (starting point): This point is located at the junction of the head and neck with the trunk, that is, behind the base of the ears and at the junction of the neck and back. Measured from the tip of the snout to the base of the tail, it is approximately at the front 1 / 4 of the total body length.
[0040] Length 1: This point is located at the junction of the tail root and rump, that is, at the upper edge of the tail's starting point. Measured from the total body length, it is approximately at the posterior 1 / 6 of the body length.
[0041] Body height measurement point H0: This point is located at the highest point of the spine on the back, usually near the midpoint of the line connecting the two scapulae, about halfway between the body length Length0 and Length1;
[0042] Complete the corresponding annotation of pose category and phenotypic key points in the Labelme annotation tool.
[0043] In a further optimization of this technical solution, step 2 includes:
[0044] Constructing the Swin-TSimCC model
[0045] Step 2.1: The input image is first divided into non-overlapping image patches of fixed size. Each patch is flattened and mapped to the embedding dimension through a linear projection layer to obtain an initial low-dimensional feature vector token sequence, which is used as the input to the subsequent Transformer-block. The core formula is as follows:
[0046] (14)
[0047] In the formula, patch p It is the p-th image patch; vec is the column vector flattened from the patch; We b e These are the weights and biases of the linear projection; z p It is the projected representation of the token;
[0048] Step 2.2: Each stage maintains the same number of token sequences and performs several Swing blocks. Between stages, the sampling space size is halved and the channel width is expanded through Patch Merging. The core formula is as follows:
[0049] (15)
[0050] In the formula, It is the first in the current feature map A sequence of tokens at each position; W merge b merge It is a downsampling linear layer; It is the location feature after downsampling;
[0051] Step 2.3: To reduce computational complexity, Swing performs multi-head self-attention within locally non-overlapping windows, thereby reducing the global O(N) complexity. 2 The complexity is reduced to be linearly related to the input size. A "learnable relative position bias" is added to the similarity term of attention to encode relative position information. The core formula is as follows:
[0052] (16)
[0053] In the formula, d is the dimension; Q, K, and V are value matrices; B is the relative position bias matrix; and Softmax is the normalization process. It is a multi-head output linear mapping matrix;
[0054] Step 2.4: Swin alternates between regular window division W-MSA and shifted window SW-MSA between two adjacent Transformer blocks: the window is spatially shifted downwards / right by halving, so that some tokens fall into the same window as tokens in different windows above. The core formula is as follows:
[0055] (17)
[0056] In the formula, M mask It is the mask matrix, and the other variables are consistent with equation (16);
[0057] Step 2.5: Swin uses a learnable relative position bias table to fill B with the relative coordinate index values between patches within the window, so that attention can perceive relative position information within the local window.
[0058] After feature extraction is completed by the Swing Transformer, the fused features are fed into the SimCC coordinate encoding branch. For the three-dimensional coordinates of the key points of the pig phenotype, linear projection discretization is performed. Through fully connected layers and the Softmax activation function, the coordinate category probability distributions in the x, y, and z dimensions are output respectively, so that the coordinate prediction in each dimension has probabilistic interpretability. This effectively reduces the impact of key point position fluctuations in lying and sitting postures on the prediction stability. Finally, through coordinate decoding and sub-pixel interpolation calculation, the three-dimensional coordinates of each core phenotype key point are accurately restored.
[0059] In a further optimization of this technical solution, step 3, to achieve accurate calculation of phenotypic parameters, requires converting the pixel coordinates in the depth map into camera coordinates and point cloud coordinates sequentially. The specific steps and formulas are as follows:
[0060] In the depth map, the coordinates of each pixel are represented by (u, v), where u is the horizontal pixel index and v is the vertical pixel index, corresponding to the depth value Z, which is directly output by the depth camera. Based on the pinhole camera model, the pixel coordinates (u, v) and the camera coordinates (X) are... c , Y c Z c The conversion relationship is as follows:
[0061] (19)
[0062] In the formula, (X c , Y c Z c (c) represents the three-dimensional coordinates in the camera coordinate system. x , c y (f) represents the principal point coordinates in the camera's intrinsic parameters. x f y ) represents the horizontal and vertical focal lengths in the camera intrinsics, and Z represents the depth value corresponding to pixel (u, v) in the depth map;
[0063] The camera coordinates need to be further converted to world coordinates. The origin of the world coordinate system can be defined according to requirements. The conversion relationship is achieved through the rotation matrix R and the translation vector T.
[0064] (20)
[0065] In the formula, (X w , Y w Zw R is the 3D coordinate in the world coordinate system; R is a 3×3 rotation matrix, describing the rotation attitude of the camera coordinate system relative to the world coordinate system; T is a 3×1 translation vector, describing the translation amount of the origin of the camera coordinate system relative to the origin of the world coordinate system.
[0066] Chest width, hip width, and body length are calculated using the formula for the distance between two points in space, as shown below:
[0067] (twenty one)
[0068] Where d1, d2, and d3 are chest width, hip width, and body length, respectively. , These are the X coordinates of the point cloud at the starting points of chest width, hip width, and body length, respectively. These are the X coordinates of the point cloud at the endpoints of chest width, hip width, and body length, respectively. , These are the Y-coordinates of the point cloud starting points for chest width, hip width, and body length, respectively. These are the point cloud Y coordinates of the endpoints for chest width, hip width, and body length, respectively. , These are the Z-coordinates of the point cloud at the starting points of chest width, hip width, and body length, respectively. These are the Z-coordinates of the point cloud at the endpoints of chest width, hip width, and body length, respectively.
[0069] Body height is determined based on depth information from a depth camera, using the formula shown below:
[0070] (twenty two)
[0071] Where H is the 3D height; v0 is the pixel position of the image center on the Z-axis, with a value of 240; v is the Z-axis pixel coordinate of the target point on the image; Z is the depth value of the target point measured by the depth camera, i.e., the z-coordinate in the point cloud coordinates; f z It is the focal length of the camera in the Z-axis direction.
[0072] In a further optimization of this technical solution, step 4 includes:
[0073] The M3 model uses the body length of pigs... Chest width Hip width , body height Construct weight as input feature The prediction formula is as follows:
[0074] (twenty three)
[0075] In the formula, a, b, c, d, and e are the parameters to be estimated in the model; It is the first Random error of a sample;
[0076] The model parameters are solved by minimizing the sum of squared errors between the predicted and actual values. The specific steps are as follows:
[0077] Step 4.1: Place the first The residuals of each sample Defined as the difference between actual weight and predicted weight:
[0078] (twenty four)
[0079] (25)
[0080] In the formula, n is the sample size, and the other variables are the same as above;
[0081] Step 4.2: Use the Levenberg-Marquardt algorithm to find the minimum value of the objective function, and calculate the partial derivatives of the residuals with respect to the parameters using the Jacobian matrix (J).
[0082] (26)
[0083] The parameter update formula is:
[0084] (27)
[0085] In the formula, is the damping factor; I is a 5×5 identity matrix, and r is the residual vector.
[0086] Unlike existing technologies, the above technical solution has the following beneficial effects:
[0087] 1) This invention adds metric features to the NLM algorithm, and the improved algorithm reduces MSE by 12.8% and increases PSNR by 5.7%. In pig point clouds, it effectively removes ghosting noise from the pig's back point cloud, preserves complete body surface structure details, and provides a high-quality data foundation for subsequent key point recognition.
[0088] 2) After replacing the original SimCC feature network with the Swin Transformer, the recognition rate of this invention is improved by 10.65% compared with SimCC. The core value of this improvement lies in the fact that the window attention mechanism and relative position encoding of the Swin Transformer enhance the correlation modeling between local details and global morphology, and are more suitable for the spatial topological features of key points on the pig body surface. Thus, while maintaining the model efficiency, a significant improvement in recognition rate is achieved, laying the foundation for subsequent phenotypic parameter calculation.
[0089] 3) The average error of the M3 nonlinear regression model in predicting body weight was 3.99%. After expanding the data to 160 groups, the residual distribution was closer to the normal distribution, which verified the effectiveness and stability of the method in non-contact weight measurement. Attached Figure Description
[0090] Figure 1 This is the overall flowchart;
[0091] Figure 2 Example diagram with annotations;
[0092] Figure 3 Here is a diagram of the SimCC architecture;
[0093] Figure 4 This is a diagram of the SwinT-SimCC architecture.
[0094] Figure 5 This is a comparison chart of the noise reduction results;
[0095] Figure 6 For the LOSE curve;
[0096] Figure 7 Figures showing the confusion matrix results for different models;
[0097] Figure 8 Recognize the standing posture for different models;
[0098] Figure 9 Recognition results (lying position) for different models;
[0099] Figure 10 Recognize the sitting posture for different models;
[0100] Figure 11 The fitting result;
[0101] Figure 12 It follows the residual distribution. Detailed Implementation
[0102] To explain in detail the technical content, structural features, objectives, and effects of the technical solution, the following description is provided in conjunction with specific embodiments and accompanying drawings.
[0103] The overall process of analyzing the correlation between phenotypic parameters and body weight in pigs based on an improved simple coordinate classification algorithm and regression analysis is as follows: Figure 1 As shown, it includes:
[0104] Offline training of the SwinT-SimCC model: First, the labeled pig body size key point data is divided into training set and validation set, and then SwinT-SimCC is used to train the dataset offline.
[0105] Offline training of the M3 regression model: The manually measured body size parameters and weight data of pigs are expanded, and then the M3 regression model is used to train the dataset offline.
[0106] Online Keypoint Recognition and Weight Prediction for Pigs Based on SwinT-SimCC: First, point clouds of pigs are acquired. Noise is removed from the point clouds using an improved Natural Language Model (NLM). Then, SwinT-SimCC is used for pose recognition and phenotypic keypoint recognition. The point cloud coordinates of the phenotypic keypoints are obtained through coordinate transformation, and the corresponding phenotypic parameters are calculated. The weight is then output using an M3 regression model, ultimately completing the non-contact measurement of pig weight.
[0107] The specific steps for measuring body size and weight of pigs under optimized feed composition conditions are as follows:
[0108] Step 1: First, acquire the pig point cloud and denoise it using an improved NLM (Neural Modeling). This involves taking a picture of the pig with a depth camera, performing 3D reconstruction using the depth information to obtain the pig's point cloud data, and then denoising the point cloud using an improved NLM.
[0109] Step 1.1 Point cloud denoising based on improved NLM
[0110] NLM eliminates noise by utilizing numerous repetitive or similar structures in an image (such as textures or pixel distributions near edges). The core formula of NLM is shown below:
[0111] (1)
[0112] (2)
[0113] (3)
[0114] In the formula, It is a point Denoising the 3D coordinate vector; It is a point The original three-dimensional coordinate vector; It is a point The set of candidate points; It is a point Point The similarity weight (non-negative); It is a point Local descriptors; It is the filter strength; It is the normalization coefficient.
[0115] Because the NLM algorithm may only rely on simple geometric features (such as position and normal vectors) for similarity calculation, without considering more complex geometric features or higher-order structural information, it leads to insufficient denoising. To address this problem, the following two improvement strategies are proposed:
[0116] 1) Add outlier measurement features (normal, curvature). The updated core steps and formulas are shown below:
[0117] Step 1.1.1: Let I, N, and K be the three types of input channels, representing the point cloud projection intensity channel, normal channel, and curvature channel, respectively. The intermediate features obtained at layer t are as follows:
[0118] (4)
[0119] In the formula, K I (t) K N (t) K K (t) These are the convolution kernels for the three input channels of the t-th layer; F I (t) F N (t) F K (t) These are the feature tensors of the three input channels at the t-th layer.
[0120] Step 1.1.2: Perform fusion using weighted summation, as shown in the formula below:
[0121] (5)
[0122] In the formula, w I (t) w N (t) w K (t) These are the weights of the three input channels in the t-th layer; b (t) σ is the bias term; σ is the activation function (ReLU). It is the output feature tensor of the t-th layer network, and the other variables are consistent with equation (4).
[0123] Step 1.1.3: Add residual terms to the output layer, as shown in the formula below:
[0124] (6)
[0125] In the formula, skip(t) is the residual of the skip connection, which prevents information loss. It is the intermediate feature tensor obtained by weighted fusion of multi-channel features in the t-th layer network, and the other variables are consistent with equation (4-5).
[0126] 2) Weight the mean squared error loss with the normal constraint loss and curvature constraint loss to construct a new loss function. The improved loss function... As shown below:
[0127] (7)
[0128] (8)
[0129] (9)
[0130] (10)
[0131] In the formula, L MSE L normal L curv These are the mean square error constraint term, the normal constraint term, and the curvature constraint term; λ mse , λ n , λ k They are L MSE L normal L curv The weighting coefficients, These are the actual 3D coordinates and the predicted 3D coordinates of the i-th point, respectively. These are the true normal vector and the predicted normal vector at the i-th point, respectively. These are the actual curvature value and the predicted curvature value at the i-th point, respectively, and the remaining variables are consistent with equations (1-3).
[0132] NLM algorithm evaluation metrics
[0133] To verify whether the above improvements will optimize the denoising effect, peak signal-to-noise ratio (PSNR) and mean square error (MSE) were selected as evaluation metrics to objectively evaluate the results. MSE is the ratio of ghosting points to other points. The formula for PSNR is as follows:
[0134] (11)
[0135] In the formula, MSE is the mean squared error; MAX is the maximum value of the coordinates on each axis in the entire point cloud. The larger the PSNR, the better the denoising effect.
[0136] Step 1.2: Data annotation. The pig photos taken by the depth camera are annotated with pose and key points.
[0137] In daily pig farming, standing, lying, and sitting are the three most common postures of pigs. The spatial positions of phenotypic key points differ significantly under different postures, directly affecting the accuracy of phenotypic parameter calculations. To ensure that the model can accurately adapt to common scenarios in actual farming, it is necessary to train the model for these three common postures. Therefore, images of pigs with clear and complete back contours are first selected. In this example, the subjects selected are Duroc crossbred three-way crossbred pigs and American Black pigs in the early fattening stage of a farm in Shouxian County, Anhui Province, with a weight range of 20KG~40KG.
[0138] Key phenotypic features in pigs are divided into 7 categories, and their specific locations are shown below:
[0139] Chest width starting point (CW0) and chest width ending point (CW1): These two points are located at the junction of the left and right forelimbs and the trunk, approximately one-third of the way up the height of the foreleg.
[0140] The starting point of hip width (HW0) and the ending point of hip width (HW1) are located at the top of the hip joint where the left and right hind limbs connect to the trunk, approximately at the upper half of the hind leg height.
[0141] Length0: This point is located at the junction of the head and neck with the trunk, that is, behind the base of the ears and at the junction of the neck and back. The total body length from the tip of the pig's snout to the base of the tail is approximately at the front 1 / 4 of the body length.
[0142] Length 1: This point is located at the junction of the tail root and rump, that is, at the upper edge of the tail's starting point. Measured from the total body length, it is approximately at the posterior 1 / 6 of the body length.
[0143] Height measurement point (H0): This point is located at the highest point of the spine in the back, usually near the midpoint of the line connecting the two scapulae, about halfway between the body length (Length0 to Length1).
[0144] In the Labelme annotation tool, complete the corresponding annotations for posture categories (standing, lying, sitting) and phenotypic key points. An example annotation is shown below. Figure 2 As shown.
[0145] Step 2: Use SwinT-SimCC to perform pose recognition and phenotypic key point recognition. Obtain the point cloud coordinates of the phenotypic key points through coordinate transformation and calculate the corresponding phenotypic parameters.
[0146] SimCC Model
[0147] The structure of SimCC is as follows: Figure 3 As shown, SimCC encodes, classifies, and decodes the horizontal and vertical coordinate information respectively to accurately obtain the location of the target key points. This structure mainly includes two stages: coordinate encoding and coordinate decoding.
[0148] In the coordinate encoding stage, SimCC first performs a linear projection on the original keypoint coordinates to map continuous coordinate values to discrete classification labels. Let the pixel coordinates of the p-th keypoint in the image be (x...). p , y p , z p The discretized representation obtained after linear projection is:
[0149] (12)
[0150] In the formula, k is a scaling factor (k > 1), with a value of 4, used to expand the coordinate range to improve quantization accuracy; round is the rounding operation. This process maps continuous coordinates to a finer-grained discrete space so that a classifier can be used for prediction in subsequent stages.
[0151] The classifier outputs probability distributions in the x, y, and z directions.
[0152] During the decoding phase, the final keypoint coordinates are obtained by selecting the location with the highest probability and performing scale restoration.
[0153] (13)
[0154] In the formula, argmax represents the index position where the maximum value is selected in the probability distribution; k is consistent with the encoding stage and is used to map the classification result back to the original image scale.
[0155] The Swin-TSimCC model is constructed as follows:
[0156] Network replacement: The original ResNet feature extraction network of SimCC is replaced with a Swing Transformer as the feature extraction backbone;
[0157] Retain core logic: retain SimCC's coordinate discretization encoding, probability distribution decoding, and sub-pixel interpolation modules;
[0158] Feature adaptation: The feature map output by the Swing Transformer is input into the fully connected layer of SimCC to achieve probabilistic prediction of key point coordinates.
[0159] Due to limitations in discretization strategies, feature modeling, and dynamic adaptability, SimCC cannot meet the application requirements of high-precision scenarios. Therefore, the original SimCC network is replaced with Swin Transformer. Swin Transformer achieves effective fusion of local features and global information through a window-based multi-head self-attention (W-MSA) mechanism, enhancing the ability to model cross-regional feature associations while maintaining low computational complexity.
[0160] The core steps and formulas of the Swin Transformer are as follows:
[0161] Step 2.1: The input image is first divided into non-overlapping image patches of fixed size. Each patch is flattened and mapped to the embedding dimension through a linear projection layer to obtain an initial low-dimensional feature vector (token) sequence, which is used as the input to the subsequent Transformer-block. The core formula is as follows:
[0162] (14)
[0163] In the formula, patch p It is the p-th image patch; vec is the column vector flattened from the patch; W e b e These are the weights and biases of the linear projection; z p It is the projected representation of the token.
[0164] Step 2.2: Maintain the same number of feature vector sequences in each stage and perform several Swin modules. Downsampling (halving the spatial size) and expanding the channel width are performed between each stage by merging image patches. The core formula is as follows:
[0165] (15)
[0166] In the formula, It is the th in the current feature map A sequence of tokens at each position; W merge b merge It is a downsampling linear layer; It is the location feature after downsampling.
[0167] Step 2.3: To reduce computational complexity, Swing performs multi-head self-attention within locally non-overlapping windows, thereby reducing the global O(N) complexity. 2The complexity is reduced to be linearly related to the input size. A "learnable relative position bias" is added to the similarity term of attention to encode relative position information; the core formula is as follows:
[0168] (16)
[0169] In the formula, d is the dimension; Q, K, and V are value matrices; B is the relative position bias matrix; and Softmax is the normalization process. It is a multi-head output linear mapping matrix.
[0170] Step 2.4: Swin alternates between regular window partitioning (W-MSA) and shifted windowing (SW-MSA) between two adjacent Transformer blocks: the window is spatially shifted by halving downwards (downwards / rightwards), so that some tokens fall into the same window as tokens in different windows above. The core formula is as follows:
[0171] (17)
[0172] In the formula, M mask It is the mask matrix, and the other variables are consistent with equation (16).
[0173] Step 2.5: Swin uses a learnable relative position bias table to fill B with the relative coordinate index values between patches within the window, so that attention can perceive relative position information within the local window.
[0174] After replacing the SimCC feature network with the Swin transform, the structure of SwinT-SimCC is as follows: Figure 4 As shown.
[0175] After feature extraction is completed by the Swing Transformer, the fused features are fed into the SimCC coordinate encoding branch. Linear projection discretization is performed on the 3D coordinates of key points in the pig phenotypic pattern. Through a fully connected layer and a Softmax activation function, the probability distributions of coordinate categories in the x, y, and z dimensions are output, ensuring probabilistic interpretability for each dimension's coordinate prediction and effectively reducing the impact of key point position fluctuations under lying and sitting postures on prediction stability. Finally, through coordinate decoding and sub-pixel interpolation calculations, the 3D coordinates of each core phenotypic key point are accurately reconstructed.
[0176] Swin-TSimCC model evaluation metrics
[0177] In the key point recognition experiment, the recognition rate was selected as the model evaluation metric, and the specific formula is shown below:
[0178] (18)
[0179] In the formula, 'a' is the distance error weight, which takes a value of 0.6; is the scale factor, with a value of 0.05; MEDE is the mean Euclidean distance error; RPA is the relative position accuracy.
[0180] Step 3: Based on the recognition results of Step 2, obtain the camera coordinates, convert the camera coordinates into point cloud coordinates, and output the pig's body size based on the point cloud coordinates.
[0181] To achieve accurate calculation of phenotypic parameters (such as body length and chest width), the pixel coordinates in the depth map need to be converted into camera coordinates and point cloud coordinates sequentially. The specific steps and formulas are as follows:
[0182] In the depth map, the coordinates of each pixel are represented by (u, v) (u is the horizontal pixel index, v is the vertical pixel index), and the corresponding depth value is Z (directly output by the depth camera). Based on the pinhole camera model, the pixel coordinates (u, v) and the camera coordinates (X) are... c , Y c Z c The conversion relationship is as follows:
[0183] (19)
[0184] In the formula, (X c , Y c Z c (c) represents the three-dimensional coordinates in the camera coordinate system; x , c y (f) represents the principal point coordinates in the camera's intrinsic parameters. x f y ) represents the horizontal and vertical focal lengths in the camera intrinsics; Z represents the depth value corresponding to pixel (u, v) in the depth map.
[0185] The camera coordinates need to be further converted to world coordinates. The origin of the world coordinate system can be defined according to requirements. The conversion relationship is achieved through the rotation matrix R and the translation vector T (extrinsic parameter):
[0186] (20)
[0187] In the formula, (X w , Y w Z w R is the 3D coordinate in the world coordinate system; R is a 3×3 rotation matrix, describing the rotation attitude of the camera coordinate system relative to the world coordinate system; T is a 3×1 translation vector, describing the translation amount of the origin of the camera coordinate system relative to the origin of the world coordinate system.
[0188] Chest width, hip width, and body length are calculated using the formula for the distance between two points in space, as shown below:
[0189] (twenty one)
[0190] Where d1, d2, and d3 are chest width, hip width, and body length, respectively. , These are the X coordinates of the point cloud at the starting points of chest width, hip width, and body length, respectively. These are the X coordinates of the point cloud at the endpoints of chest width, hip width, and body length, respectively. , These are the Y-coordinates of the point cloud starting points for chest width, hip width, and body length, respectively. These are the point cloud Y coordinates of the endpoints for chest width, hip width, and body length, respectively. , These are the Z-coordinates of the point cloud at the starting points of chest width, hip width, and body length, respectively. These are the Z-coordinates of the point cloud at the endpoints of chest width, hip width, and body length, respectively.
[0191] Body height is determined based on depth information from a depth camera, using the formula shown below:
[0192] (twenty two)
[0193] Where H is the 3D height; v0 is the pixel position of the image center on the Z-axis, with a value of 240; v is the Z-axis pixel coordinate of the target point on the image; Z is the depth value of the target point measured by the depth camera, i.e., the z-coordinate in the point cloud coordinates; f z It is the focal length of the camera in the Z-axis direction.
[0194] Step 4: Input the pig's body size data obtained in step S3, output the weight through the M3 model, and finally complete the non-contact measurement of the pig's weight.
[0195] The M3 nonlinear repeated measures regression model is based on the idea of fitting the relationship between phenotypic parameters and body weight through a nonlinear function. It is suitable for modeling biological phenotypic data with repeated measures characteristics and can effectively capture the nonlinear relationship between parameters such as body length and chest width and body weight.
[0196] The M3 model uses the body length (L) of pigs as a reference. i ), chest width (C i Hip width (H) i ), body height (T i Using ) as input features, construct weight (W) i The prediction formula is as follows:
[0197] (twenty three)
[0198] In the formula, a, b, c, d, and e are the parameters to be estimated in the model; It is the first The random error of each sample.
[0199] The model parameters are solved by minimizing the sum of squared errors (RSS) between the predicted and actual values. The specific steps are as follows:
[0200] Step 4.1: Place the first The residuals of each sample Defined as the difference between actual weight and predicted weight:
[0201] (twenty four)
[0202] (25)
[0203] In the formula, n is the number of samples, and the other variables are the same as above.
[0204] Step 4.2: Solve for the minimum objective function using the Levenberg-Marquardt algorithm, and calculate the partial derivatives of the residuals with respect to the parameters using the Jacobian matrix (J).
[0205] (26)
[0206] The parameter update formula is:
[0207] (27)
[0208] In the formula, is the damping factor; I is a 5×5 identity matrix, and r is the residual vector.
[0209] Experimental Results and Analysis
[0210] A. Experimental Environment
[0211] The data acquisition device uses an Intel RealSense D455 depth camera. Considering the dual requirements of accuracy and real-time performance in the pig phenotypic recognition scenario, the device parameters are configured as follows: depth stream resolution 640×480, frame rate 30fps, color stream resolution 1280×720, frame rate 30fps. This configuration can achieve simultaneous acquisition of 30 frames of depth images and 30 frames of color images per second.
[0212] The tripod used for data acquisition is 2.88m high and 3m long, with one tripod on each side, and connected by a 4m telescopic pole to form a gate shape. A depth camera is fixed at the midpoint of the telescopic pole and placed above the pig to be sampled for data acquisition.
[0213] The algorithm runs on a Windows 10 system with 32GB of RAM, an AMD Ryzen 7 5800H CPU with Radeon Graphics 32GHz, and an NVIDIA GeForce RTX 3070 GPU. The software environment is PyCharm Community Edition 2022.3, using Python 3.8.16. The neural network model runs on the PyTorch 2.0.0 framework, with CUDA 11.8 as the graphics card. Subsequent experiments will use the same environment.
[0214] The intrinsic parameters of the depth camera obtained by the MATLAB checkerboard calibration program are shown in the table below.
[0215] Table 1 Camera Internal Parameters Table 1 Camera Intrinsics
[0216] B. Noise Reduction Results and Analysis
[0217] To effectively remove noise, normal and curvature discrimination features are added to the NLM model. (See also...) Figure 5 The image shows a comparison of the denoising results. The original point cloud is shown below. Figure 5 As shown in figure a, the point cloud after NLM denoising is as follows: Figure 5 As shown in b, the improved NLM for point clouds is as follows: Figure 5 As shown in c:
[0218] The evaluation index data are shown in the table below:
[0219] Table 2 Results Table 2 results table
[0220] from Figure 6 Visualization reveals that while the NLM algorithm can initially reduce ghosting noise in the point cloud of the pig's back, significant ghosting artifacts still exist at the edges of the pig's head contour and on both sides of the spine. Point cloud "fragments" also appear in weak texture areas such as the ears. This is because the original algorithm only relies on simple geometric features such as position and normal vectors to calculate similarity, and cannot distinguish the difference between the real body surface structure and the motion ghosting. The improved NLM algorithm can effectively remove these ghosting artifacts, making the pig's head contour clear and continuous, the point cloud distribution on the back body surface uniform, and the point cloud density and structural integrity in the weak texture area of the ears significantly improved.
[0221] As shown in Table 2, the improved NLM significantly enhances denoising performance. Regarding the MSE (Mean Sequence of Expression), the NLM's MSE is 1.48, while the improved NLM's MSE decreases to 1.29, a cumulative reduction of 12.8%. This indicates that the introduction of curvature features further improves the similarity calculation logic, reduces misjudgments of ghosting and real object surfaces, and lowers the overall deviation of point cloud coordinates. In terms of PSNR (Power Response Rate), the NLM's PSNR is 0.88, while the improved NLM's PSNR increases to 0.93, a cumulative improvement of 5.7%.
[0222] C. Key Point Identification Results and Analysis
[0223] C1, Dataset
[0224] First, the pig's posture was identified, and then phenotypic key points were identified under different postures. For posture recognition training, a total of 1720 images were used (508 standing, 598 lying, and 614 sitting). These images were then expanded to 8412 images (2465 standing, 2841 lying, and 3106 sitting) using methods such as flipping, translation, and grayscale processing. The ratio of training, validation, and test sets was 7:2:1, and the training run consisted of 200 epochs. For key point recognition training, the ratio of training, validation, and test sets was also 7:2:1, and the training run consisted of 200 epochs. To compare the performance of different models, the posture recognition model and the key point recognition model were used interchangeably.
[0225] C2, Model Training
[0226] To verify the comprehensive performance of the SwinT-SimCC model in the task of identifying key points of phenotypic features in pigs, this study selected SimCC, YOLOv8s-Pose, YOLOX-Pose-tiny, and DarkPose as comparisons to objectively evaluate the convergence characteristics, generalization ability, and feature learning stability of the SwinT-SimCC model. Figure 7 7a represents a comparison of training loss, and 7b represents a comparison of validation loss.
[0227] Depend on Figure 6 The training and validation loss curves show that SwinT-SimCC outperforms other models in terms of convergence stability and generalization ability. After 100 training rounds, the fluctuation range of the training and validation loss curves of SwinT-SimCC is strictly controlled within 1%, and the two curves almost completely overlap. There is no overfitting problem, nor is there any loss fluctuation due to insufficient feature learning. This indicates that it has stably mastered the spatial feature mapping relationship of key points on the pig's body surface, providing a reliable foundation for subsequent parameter calculation.
[0228] Other models all exhibit significant performance shortcomings: YOLOv8s-Pose and YOLOX-Pose-tiny still show loss fluctuations of 3%~4% after 100 rounds, with an average difference of 2.2% between training and validation loss between 120 and 180 rounds, indicating they are trapped in local optima and are unstable in capturing keypoint features; DarkPose shows fluctuations of 2.5%~3%, with a difference of 1.8%~2.0% between training and validation loss, insufficient global spatial dependency modeling, and generalization limited by the pose of the training set; SimCC, due to the limitations of the local receptive field of ResNet-50, cannot capture long-range associations of keypoints, resulting in loss fluctuations of 2.8%~3.2% and poor localization consistency. SwinT-SimCC, relying on window attention and cross-window feature interaction, effectively overcomes the limitations of traditional models, demonstrating significant advantages in convergence stability and generalization ability.
[0229] Figure 7 The confusion matrix results of five models—SwinT-SimCC, SimCC, YOLOv8s-Pose, YOLOX-Pose-tiny, and DarkPose—in the pig pose recognition task are presented. Overall, all models show high consistency in recognizing the "Standing," "nest," and "sit" poses, with accuracy rates exceeding 90% for the diagonal portion of the pose. This indicates that all five models can stably complete the pose classification task, and the overall recognition performance differences are not significant.
[0230] To further verify the performance of the SwinT-SimCC model, 500 keypoint recognitions were performed in each of the three poses. The average keypoint recognition rates of different models in different poses are shown below:
[0231] Table 3 Recognition Rate Results Table 3 Accuracy results table
[0232] In summary, SwinT-SimCC achieves optimal recognition performance in standing, lying, and sitting postures. This is primarily due to its synergistic improvement of a triple architecture addressing the shortcomings of traditional models, with each improved module precisely adapted to the recognition requirements of different postures. The Swin Transformer's hierarchical shifting window architecture overcomes the limitations of local features in traditional CNNs, constructing global spatial dependencies through local window attention and cross-window interaction. Simultaneously, it enhances robustness through dynamic weight adjustment via ECA channel attention. This synergistic improvement of global modeling and dynamic robustness allows the model to adapt to the core requirements of the three posture types.
[0233] C3 Key Point Localization in Different Orthogonals
[0234] 1) Key point localization in standing position
[0235] In a standing position, the visualization results of different key point recognition models for each key point are as follows: Figure 8 As shown.
[0236] The standing posture is a relatively standard body position for pigs, with an extended body and a relatively regular spatial distribution of key points. However, occasional local light reflections in the environment can easily cause some models to have perceptual biases in key points with low texture features. Models such as SimCC and YOLOv8s-Pose have missed some key points. In contrast, SwinT-SimCC, relying on the global information interaction mechanism of Swin Transformer, can simultaneously capture the spatial correlation and local texture features of 7 types of key points. It enhances the perception of key points in low-texture areas through window attention and ensures the synchronous localization accuracy of multiple key points through cross-window interaction. All key points are detected without omission, and the spatial distribution perfectly matches the pig's body structure, fully adapting to the multi-target recognition needs under the standing posture.
[0237] After identifying the key points of each phenotypic of pigs in a standing posture using SwinT-SimCC, the camera coordinates of each key point are obtained. Then, the camera coordinates are substituted into equations (19) and (20) in sequence to calculate the point cloud coordinates corresponding to each key point. The specific data is shown below:
[0238] Table 4 Key point coordinates in standing position Table 4 Key point coordinates in standing posture
[0239] 2) Key point localization in supine position
[0240] See Figure 9 The image shows the visualization results in the recumbent position. In this position, the pig's body is curled up, causing some key points to shift. At the same time, uneven ambient lighting can easily cause local features to be obscured. Traditional models often miss or misdetect key points due to insufficient multi-target recognition capabilities: models such as YOLOX-Pose-tiny and DarkPose generally miss the end point of the hip width. However, SwinT-SimCC, through relative position encoding and dynamic attention weight adjustment, can accurately distinguish different key point features within the compressed area. At the same time, it establishes spatial constraint relationships between multiple key points. Even in the face of light occlusion and feature compression, it can still completely identify 7 types of key points with no missed detections and minimal positioning deviation. Its multi-target recognition robustness is significantly better than the comparison models.
[0241] Using the same key point calculation method as in Table 4, the point cloud coordinates of each key point were calculated in the supine position as shown below:
[0242] Table 5 Key point coordinates in supine position Table 5 Key point coordinates in the lying position
[0243] 3) Key point localization in seated position
[0244] See Figure 10 The image shows the visualization results in a seated position. In this position, the pig's arched back causes a non-linear distribution of multiple keypoints, and the tilted torso easily triggers camera perspective shifts, resulting in some keypoint features exhibiting "non-standard shapes." Traditional models, limited by fixed receptive fields and single-target-first recognition logic, exhibit significant shortcomings in multi-target recognition: the SimCC model misses long endpoints, YOLOv8s-Pose fails to simultaneously adapt to changes in chest and hip width, leading to confusion between the two types of keypoint localization, and DarkPose misses high measurement points due to interference from multi-target features. In contrast, SwinT-SimCC, through layered window attention and global feature fusion, can simultaneously process multi-keypoint features under non-linear distributions, dynamically adjusting the attention weights of different keypoints, and can still recognize all keypoints in this position.
[0245] Using the same keypoint calculation method as in Table 4, the point cloud coordinates of each keypoint are calculated under the seated posture as shown below:
[0246] Table 6 Key point coordinates under pose state Table 6 Keypoint coordinates in pose state
[0247] C4 phenotypic parameter calculation
[0248] Substituting the data from Tables 4, 5, and 6 into equations (21) and (22), the phenotypic parameters are calculated. The calculated phenotypic parameters and the results of manual measurement are shown below:
[0249] Table 7 Comparison of phenotypic parameters under different postures Table 7 Comparison Table of Phenotypic Parameters in Different Postures
[0250] In the standing position, the errors of various phenotypic parameters calculated by the model were all at a low level. However, in the lying and sitting positions, although the calculation errors of chest width, hip width, and body length were slightly higher than in the standing position, these errors were still within a reasonable range and met the requirements for subsequent modeling of phenotypic parameters. The body height parameter, on the other hand, showed significant error fluctuations, with the calculation error reaching as high as 51.61% in the lying position and 13.82% in the sitting position. The core reason for this phenomenon is that in the lying position, the pig's body is curled up close to the ground, which changes the body height measurement benchmark, resulting in a reduced correlation between the Z-axis depth value in the point cloud coordinates and the actual body height. In the sitting position, the pig's back is arched and its torso is tilted to the side, causing a shift in the camera's viewing angle, which leads to deviations in the calculation of the pixel coordinates of the body height measurement points.
[0251] Significant errors in body height parameters under non-standard postures can interfere with the accuracy of weight prediction. To counteract this effect, differentiated M3 nonlinear regression models are constructed for three typical postures: standing, lying, and sitting. By optimizing the model parameters for each posture, the model dynamically adapts to the characteristic differences in phenotypic parameters under different postures, thereby avoiding the weight prediction bias caused by insufficient posture adaptation of a single model. Ultimately, this achieves accurate non-contact measurement of pig weight.
[0252] D. Weight Prediction Results and Analysis
[0253] D1. Model Parameter Calculation
[0254] To ensure the accuracy and robustness of the regression model training, this study constructed a sample dataset containing 160 pigs. Phenotypic parameters and weight data for each pig were manually measured in three postures: standing, lying, and sitting. This resulted in three independent subsets of sample data, providing high-quality labeled samples for subsequent model training. Some of the manually measured data in different postures are shown in the table below, where P indicates that the information in this set was provided by the Pth pig; Xn represents the nth set of data in posture X.
[0255] Table 8. Some manually measured values Table 8 Partial Manual Measurements
[0256] The model parameters are solved using equations (23) to (27), and the model parameters under different attitudes are shown below:
[0257] Table 9 Regression Model Parameter Table Table 9 Regression Model Parameter Table
[0258] D2. Experimental Results
[0259] The fitted curve and residual distribution are core metrics for evaluating the performance of a regression model. They jointly reflect the model's fit to the relationship between phenotypic parameters and body weight from two dimensions: "overall correlation effectiveness" and "local error stability." For visualization results of the fitted curve and residual distribution in the standing posture state, please refer to [link to relevant documentation]. Figure 11 As shown.
[0260] Figure 11 'a' represents the fitted curve; the blue scatter points represent the original data points; the green curve represents the true functional relationship; the red curve represents the fitted curve; and the pink transparent band represents the uncertainty range (95% confidence interval) of the fitted curve. Figure 11 b represents the residual distribution, which closely approximates a normal distribution, indicating a better fit. The weight predicted based on the phenotypic parameters is closer to the true value. The following figure shows the residual distribution of the regression model under lying and sitting postures:
[0261] See Figure 12 It can be seen that, Figure 12 'a' represents the lying position. Figure 12 b represents the residuals in both the sitting and lying / sitting positions, which exhibit an approximately normal distribution. The data are concentrated in the near-zero interval, with no obvious skewness or extreme outlier residuals. Furthermore, the high-frequency residuals are concentrated in a relatively small range. This indicates that the model can effectively offset the interference caused by changes in body shape and angle shifts in phenotypic parameter measurements under both non-standard postures. Therefore, it proves that the M3 regression model has reliable fitting accuracy under different postures.
[0262] The following are the results of regression calculation and manual measurement of the weight of the sample in C3 and some samples in Table 8:
[0263] Table 10 Calculation results of pig weight under different postures Table 10 Pig Weight Calculation Results in Different Postures
[0264] From the quantitative results of weight prediction error, the prediction errors of all test samples under the corresponding posture-specific models remained at a low level: the weight prediction errors of sample B under the standing, lying, and sitting posture models were 2.97%, 5.30%, and 3.70%, respectively; the errors of manual measurement group ① for the three posture models were 5.36%, 2.76%, and 4.50%, respectively; and the errors of manual measurement group ② were 3.46%, 3.58%, and 7.88%. Overall, the weight prediction errors under all postures did not exceed 8%, which is significantly lower than the weight measurement accuracy threshold in large-scale pig farming scenarios, fully meeting the technical requirements for weight measurement accuracy in core aspects such as pig growth and development monitoring, precise feeding control, and improved breed selection evaluation.
[0265] The core reason for this excellent predictive performance lies in the fact that this study did not use a single regression model to fit all postures. Instead, it constructed posture-specific M3 regression models for the differences in phenotypic characteristics among the three postures. By independently optimizing the parameters to be estimated for each posture model, each model can accurately capture the intrinsic correlation between phenotypic parameters and body weight under the corresponding posture. Even in the face of potential fluctuations in parameter measurements under non-standard postures, error compensation can still be achieved through the posture adaptability of the model parameters. Ultimately, this ensures the accuracy and stability of body weight prediction results under all postures, providing reliable model support for the practical application of non-contact body weight measurement in pigs.
[0266] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or terminal device that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or terminal device. Unless otherwise specified, an element defined by the phrase "comprising..." or "including..." does not exclude the presence of additional elements in the process, method, article, or terminal device that includes said element. Additionally, in this document, "greater than," "less than," "exceeding," etc., are understood to exclude the stated number; "above," "below," "within," etc., are understood to include the stated number.
[0267] Although the above embodiments have been described, those skilled in the art, once they understand the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the above descriptions are merely embodiments of the present invention and do not limit the scope of patent protection of the present invention. Any equivalent structural or procedural transformations made using the content of the present invention's specification and drawings, or direct or indirect applications in other related technical fields, are similarly included within the scope of patent protection of the present invention.
Claims
1. A method for measuring body size and weight of pigs under optimized feed composition conditions, characterized in that, Includes the following steps: Step 1: First, obtain the pig point cloud and denoise the point cloud by improving NLM. That is, after the depth camera takes pictures of the pig, 3D reconstruction is performed using the depth information to obtain the pig point cloud data, and point cloud denoising is performed based on the improved NLM. Step 2: Use SwinT-SimCC to perform pose recognition and phenotypic key point recognition. Obtain the point cloud coordinates of the phenotypic key points through coordinate transformation and calculate the corresponding phenotypic parameters. Step 3: Based on the recognition results of Step 2, obtain the camera coordinates, convert the camera coordinates into point cloud coordinates, and output the pig's body size based on the point cloud coordinates. Step 4: Input the pig's body size data obtained in step S3, output the weight through the M3 model, and finally complete the non-contact measurement of the pig's weight.
2. The method for measuring body size and weight of pigs under optimized feed composition conditions as described in claim 1, characterized in that, The improved NLM for point cloud denoising in step 1 includes: The core formula of NLM is as follows: (1) (2) (3) In the formula, It is a point Denoising the 3D coordinate vector; It is a point The original three-dimensional coordinate vector; It is a point The set of candidate points; It is a point Point Similarity weights; It is a point Local descriptors; It is the filter strength; It is the normalization coefficient. Since the NLM algorithm may only rely on simple geometric features for similarity calculation without considering more complex geometric features or higher-order structural information, it leads to insufficient denoising. To address this issue, the following two improvement strategies are proposed: 1) Add outlier measurement features. The core steps and formulas after the update are as follows: Step 1.1.1: Let I, N, and K be the three types of input channels, representing the point cloud projection intensity channel, normal channel, and curvature channel, respectively. The intermediate features obtained at layer t are as follows: (4) In the formula, K I (t) K N (t) K K (t) These are the convolution kernels for the three input channels of the t-th layer; F I (t) F N (t) F K (t) These are the feature tensors of the three input channels at the t-th layer; Step 1.1.2: Perform fusion using weighted summation, as shown in the formula below: (5) In the formula, w I (t) w N (t) w K (t) These are the weights of the three input channels in the t-th layer; b (t) σ is the bias term; σ is the activation function. It is the output feature tensor of the t-th layer network, and the other variables are consistent with equation (4); Step 1.1.3: Add residual terms to the output layer, as shown in the formula below: (6) In the formula, skip(t) is the residual of the skip connection, which prevents information loss. It is the intermediate feature tensor obtained by weighted fusion of multi-channel features in the t-th layer network, and the other variables are consistent with equation (4-5); 2) Weight the mean squared error loss with the normal constraint loss and curvature constraint loss to construct a new loss function. The improved loss function... As shown below: (7) (8) (9) (10) In the formula, L MSE L normal L curv These are the mean square error constraint term, the normal constraint term, and the curvature constraint term; λ mse , λ n , λ k They are L MSE L normal L curv The weighting coefficients, These are the actual 3D coordinates and the predicted 3D coordinates of the i-th point, respectively. These are the true normal vector and the predicted normal vector at the i-th point, respectively. These are the actual curvature value and the predicted curvature value at the i-th point, respectively, and the remaining variables are consistent with equations (1-3).
3. The method for measuring body size and weight of pigs under optimized feed composition conditions as described in claim 1, characterized in that, The point cloud data of the pigs in step 1 includes: In daily pig farming, standing, lying, and sitting are the three most common postures of pigs. The spatial positions of phenotypic key points differ significantly under different postures, directly affecting the accuracy of phenotypic parameter calculation. To ensure that the model can accurately adapt to common scenarios in actual farming, it is necessary to train the model for these three common postures. Therefore, images of pigs with clear and complete back contours are first selected. Key phenotypic features in pigs are divided into 7 categories, and their specific locations are shown below: Chest width starting point CW0, chest width ending point CW1: These two points are located at the junction of the left and right forelimbs and the trunk, approximately one-third of the way up the height of the foreleg; The starting point of hip width HW0 and the ending point of hip width HW1 are located at the top of the hip joint where the left and right hind limbs connect to the trunk, approximately at the upper half of the height of the hind leg. Length0: This point is located at the junction of the head and neck and the trunk, that is, behind the ear root and the junction of the neck and back. It is about the first 1 / 4 of the total body length from the tip of the pig's snout to the base of the tail. Length1: This point is located at the junction of the tail root and the rump, that is, at the upper edge of the tail starting point, approximately 1 / 6 of the total body length. Body height measurement point H0: This point is located at the highest point of the spine on the back, usually near the midpoint of the line connecting the two scapulae, about halfway between the body length Length0 and Length1; Complete the corresponding annotation of pose category and phenotypic key points in the Labelme annotation tool.
4. The method for measuring body size and weight of pigs under optimized feed composition conditions as described in claim 1, characterized in that, Step 2 includes: Constructing the Swin-TSimCC model Step 2.1: The input image is first divided into non-overlapping image patches of fixed size. Each patch is flattened and mapped to the embedding dimension through a linear projection layer to obtain an initial low-dimensional feature vector token sequence, which is used as the input to the subsequent Transformer-block. The core formula is as follows: (14) In the formula, patch p It is the p-th image patch; vec is the column vector flattened from the patch; W e b e These are the weights and biases of the linear projection; z p It is the projected representation of the token; Step 2.2: Each stage maintains the same number of token sequences and performs several Swing blocks. Between stages, the sampling space size is halved and the channel width is expanded through Patch Merging. The core formula is as follows: (15) In the formula, It is the th in the current feature map A sequence of tokens at each position; W merge b merge It is a downsampling linear layer; It is the location feature after downsampling; Step 2.3: To reduce computational complexity, Swing performs multi-head self-attention within locally non-overlapping windows, thereby reducing the global O(N) complexity. 2 The complexity is reduced to be linearly related to the input size. A "learnable relative position bias" is added to the similarity term of attention to encode relative position information. The core formula is as follows: (16) In the formula, d is the dimension; Q, K, and V are value matrices; B is the relative position bias matrix; and Softmax is the normalization process. It is a multi-head output linear mapping matrix; Step 2.4: Swin alternates between regular window division W-MSA and shifted window SW-MSA between two adjacent Transformer blocks: the window is spatially shifted downwards / right by halving, so that some tokens fall into the same window as tokens in different windows above. The core formula is as follows: (17) In the formula, M mask It is the mask matrix, and the other variables are consistent with equation (16); Step 2.5: Swin uses a learnable relative position bias table to fill B with the relative coordinate index values between patches within the window, so that attention can perceive relative position information within the local window. After feature extraction is completed by the Swing Transformer, the fused features are fed into the SimCC coordinate encoding branch. For the three-dimensional coordinates of the key points of the pig phenotype, linear projection discretization is performed. Through fully connected layers and the Softmax activation function, the coordinate category probability distributions in the x, y, and z dimensions are output respectively, so that the coordinate prediction in each dimension has probabilistic interpretability. This effectively reduces the impact of key point position fluctuations in lying and sitting postures on the prediction stability. Finally, through coordinate decoding and sub-pixel interpolation calculation, the three-dimensional coordinates of each core phenotype key point are accurately restored.
5. The method for measuring body size and weight of pigs under optimized feed composition conditions as described in claim 1, characterized in that, Step 3, in order to achieve accurate calculation of phenotypic parameters, requires converting the pixel coordinates in the depth map into camera coordinates and point cloud coordinates sequentially. The specific steps and formulas are as follows: In the depth map, the coordinates of each pixel are represented by (u, v), where u is the horizontal pixel index and v is the vertical pixel index, corresponding to the depth value Z, which is directly output by the depth camera. Based on the pinhole camera model, the pixel coordinates (u, v) and the camera coordinates (X) are... c ,Y c Z c The conversion relationship is as follows: (19) In the formula, (X c Y c Z c (c) represents the three-dimensional coordinates in the camera coordinate system. x , c y (f) represents the principal point coordinates in the camera's intrinsic parameters. x f y ) represents the horizontal and vertical focal lengths in the camera intrinsics, and Z represents the depth value corresponding to pixel (u, v) in the depth map; The camera coordinates need to be further converted to world coordinates. The origin of the world coordinate system can be defined according to requirements. The conversion relationship is achieved through the rotation matrix R and the translation vector T. (20) In the formula, (X w Y w Z w ) represents the three-dimensional coordinates in the world coordinate system; R is a 3×3 rotation matrix that describes the rotation attitude of the camera coordinate system relative to the world coordinate system; T is a 3×1 translation vector, describing the amount of translation of the origin of the camera coordinate system relative to the origin of the world coordinate system; Chest width, hip width, and body length are calculated using the formula for the distance between two points in space, as shown below: (21) Where d1, d2, and d3 are chest width, hip width, and body length, respectively. , These are the X coordinates of the point cloud at the starting points of chest width, hip width, and body length, respectively. These are the X coordinates of the point cloud at the endpoints of chest width, hip width, and body length, respectively. , These are the Y-coordinates of the point cloud starting points for chest width, hip width, and body length, respectively. These are the point cloud Y coordinates of the endpoints for chest width, hip width, and body length, respectively. , These are the Z-coordinates of the point cloud at the starting points of chest width, hip width, and body length, respectively. These are the Z-coordinates of the point cloud at the endpoints of chest width, hip width, and body length, respectively. Body height is determined based on depth information from a depth camera, using the formula shown below: (22) Where H is the 3D height; v0 is the pixel position of the image center on the Z-axis, with a value of 240; v is the Z-axis pixel coordinate of the target point on the image; Z is the depth value of the target point measured by the depth camera, i.e., the z-coordinate in the point cloud coordinates; f z It is the focal length of the camera in the Z-axis direction.
6. The method for measuring body size and weight of pigs under optimized feed composition conditions as described in claim 1, characterized in that, Step 4 includes: The M3 model uses the body length of pigs... Chest width Hip width , body height Construct weight as input feature The prediction formula is as follows: (23) In the formula, a, b, c, d, and e are the parameters to be estimated in the model; It is the random error of the i-th sample; The model parameters are solved by minimizing the sum of squared errors between the predicted and actual values. The specific steps are as follows: Step 4.1: Calculate the residual r of the i-th sample. i Defined as the difference between actual weight and predicted weight: (24) (25) In the formula, n is the sample size, and the other variables are the same as above; Step 4.2: Use the Levenberg-Marquardt algorithm to find the minimum value of the objective function, and calculate the partial derivatives of the residuals with respect to the parameters using the Jacobian matrix (J). (26) The parameter update formula is: (27) In the formula, It is the damping factor; It is a 5×5 identity matrix, and r is the residual vector.