A method and system for calculating egg shape index and identifying varieties
By training the image segmentation model and building the egg classification model, the problem of low accuracy and efficiency in egg shape index calculation and variety classification is solved, and high-precision and high-efficiency automated calculation and classification are achieved.
Patent Information
- Application Number
- CN202410219252.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-02-28
- Publication Date
- 2025-01-21
- Estimated Expiration
- 2044-02-28
AI Technical Summary
In the prior art, the calculation method of egg-shaped index of eggs is not accurate enough and has low calculation efficiency. The classification of egg varieties depends on manual detection, and there are subjective judgments and artificial errors, resulting in inaccurate classification and low efficiency.
An egg shape index calculation and breed identification method is adopted, including collecting egg breed image data, preprocessing, training an image segmentation model to segment the egg outline, using a variety of computer image processing techniques to optimize the image, and using a convex hull algorithm to calculate egg shape parameters. , and build an egg classification model to classify varieties.
It realizes the automatic calculation of egg contour and egg shape index, improves calculation accuracy and efficiency, reduces manual participation, improves the accuracy and automation of variety classification, and meets the needs of agricultural production.
Smart Images

Figure CN118230025B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of egg variety identification, and in particular to an egg shape index calculation and variety identification method and system. Background Art
[0002] Eggs are an indispensable food in people's daily diets around the world. They are nutritious and cheap, and are deeply loved by the general public. The egg shape index of eggs can reflect the quality of eggs, thus providing help when selecting eggs; in addition, there are many types of eggs, and many varieties have extremely similar appearances, which are difficult to distinguish. Therefore, the calculation of the egg shape index of eggs and the classification of their varieties can not only provide convenience for people in daily life, but also have important significance for the agricultural and food industries. At present, there is no clear calculation method for the egg shape index of eggs that can be widely used in the field of agricultural production, and most of the classification of egg varieties adopts manual detection. Due to the existence of subjective judgment and human error, this method wastes a lot of manpower and material resources, and cannot guarantee the accuracy of classification. It is inefficient and difficult to meet the needs of the current industry.
[0003] The methods of automatic processing of computer images mainly include the following:
[0004] 1. Threshold segmentation method: This method divides the pixels in the image into two or more different categories according to their grayscale value or color value by setting one or more thresholds. It has problems such as low recall rate, poor noise resistance, and high requirements for image quality.
[0005] 2. Methods based on target detection: This method usually extracts the features and category information of the target object from the input image, thereby learning the features of the target and the category of the associated target, and accurately identifying and locating the test image. This method has the advantages of high accuracy and resistance to background interference, but it has problems such as large data requirements and high computing costs.
[0006] 3. Region growing-based method: This method is an image segmentation method based on similarity properties. It takes the seed pixel as the starting point and gradually merges similar pixels to form a connected region until there are no more pixels to be merged, thus achieving image region segmentation. This method requires a seed point to be manually given for each region to be extracted in advance, and is sensitive to noise data.
[0007] 4. Methods based on artificial intelligence such as deep learning: After building a network model, this method can automatically extract features from images through large-scale data training, thereby achieving more accurate image processing, and can be applied to tasks such as image classification, object detection and segmentation. However, it requires a large amount of data and computing resources, and the model is a black box and difficult to explain.
[0008] There are few methods that can be directly applied to calculate the egg shape index and classify egg varieties. The existing methods have low automation, poor scalability, and low classification accuracy and efficiency. Summary of the invention
[0009] In view of the above problems existing in the prior art, the present invention is proposed.
[0010] Therefore, the present invention provides a method for calculating the egg shape index and identifying the variety, which can solve the problem that the traditional method for calculating the egg shape index is not accurate enough and the calculation efficiency needs to be improved. Egg varieties are complex, and the similarity between individuals of different varieties is high. It is difficult for existing detection methods to accurately classify the targets in the pictures. The content of the captured pictures is complex, and there is interference in the background information, which requires deep processing of the data to eliminate the interference of the background information. The existing detection methods have low automation and efficiency, high manual participation, and the recognition process takes a long time, which is difficult to meet the actual needs of agricultural production.
[0011] In order to solve the above technical problems, the present invention provides the following technical solutions: an egg shape index calculation and variety identification method, comprising: collecting egg variety image data, and preprocessing the collected egg variety image data; training an image segmentation model to segment the target egg in the input image according to the contour, and using a variety of computer image processing technologies to further operate the segmented image; according to the obtained egg contour label, using a convex hull algorithm to calculate all contour points contained in the convex hull of the egg contour, and the length-to-width ratio of the obtained minimum circumscribed rectangle is the egg shape parameter of the egg; constructing an egg classification model, classifying the segmented egg image data according to the egg classification model, and optimizing the egg classification model.
[0012] As a preferred solution of the egg shape index calculation and variety identification method described in the present invention, the preprocessing uses manual labeling to label the position of the eggs in the image, and uses a polygonal anchor frame to label the contour of the target egg. The label format of the generated annotation file is:
[0013] ;
[0014] in, The label number of the target, the default is 0; Represents the nth vertex coordinate of the polygon anchor box. Connecting the n vertex coordinates in sequence is the label outline of the image;
[0015] Processing the labeled images using data augmentation techniques, including image scaling, random cropping, and random rotation;
[0016] The image scaling and random cropping utilize parameters , and To achieve this, set The parameter specifies the image size of the input model, setting The parameters control the cropping ratio of the image. Control the range of random cropping aspect ratio;
[0017] The random rotation randomly rotates the image within a certain range, allowing the model to learn the features of objects at different angles. The parameter controls the range of random rotation angles, making the model adaptable to input targets at different angles.
[0018] As a preferred solution of the egg shape index calculation and variety identification method described in the present invention, the image segmentation model includes training an image segmentation model to segment the target egg in the input image according to its contour, marking the clear egg contour information from the image in the form of a label after image preprocessing, dividing the labeled image data into a training set and a test set, the model learns and generalizes the knowledge learned on the training set, uses the test set as the input of the trained model for testing, the model gives the predicted egg contour in the test set image, uses the predicted egg contour to compare with the real contour of the label, uses the loss function to calculate the prediction error of the model, and performs back propagation to update the model parameters. The loss function used in the image segmentation model consists of a classification branch and a regression branch, and the calculation formula is expressed as:
[0019] ;
[0020] in, is the overall loss of this image segmentation model, and are the loss functions used for the classification branch and the regression branch respectively, , are the weights of the two losses respectively;
[0021] The loss of the classification branch is measured by binary cross entropy loss, which compares the true label with the model's prediction, maps them to the same scale through logarithmic transformation, and then calculates the loss of the prediction process. The calculation formula is as follows: ;
[0022] in, is the true label of the current sample, which takes a value of 0 or 1. is the predicted output of the model. After the sigmoid function, it indicates the probability that the sample is a positive class, and the loss of the current classification branch is obtained;
[0023] The loss of the regression branch is measured by two loss functions, DFL Loss and CIoU Loss, and different weights are assigned to the two loss functions. CIoU Loss calculates the positioning loss in the regression branch, and measures the quality of the prediction box by considering the overlapping area between the prediction box and the target box, the center point comparison information between the two boxes, and calculates the positioning loss. , the calculation formula is as follows:
[0024] ;
[0025] in, and Refers to the area of the true annotation box and the predicted polygon box. Represents the intersection area of the real annotation box and the predicted polygon box, Represents the union area of the true annotation box and the predicted polygon box,
[0026] The positioning loss is calculated based on the intersection-and-union ratio. The calculation formula is as follows:
[0027] ;
[0028] in, Expressed as weight coefficient, is the center point of the real annotation box, is the center point of the predicted polygonal box, is the Euclidean distance between the center point of the real annotation box and the predicted polygon box, is the diagonal distance between the minimum closure area of the real annotation box and the predicted polygon box, To predict the consistency parameter representation of the aspect ratio of the annotation box, the calculation formula is:
[0029] ;
[0030] in, , is the width and height of the real label box, To predict the width and height of the polygonal box, the weight parameters can be calculated , which represents the proportion of the aspect ratio of the predicted annotation box in the loss, and the calculation formula is as follows:
[0031] ;
[0032] According to the proportion of the aspect ratio of the predicted annotation box in the loss, a part of the CIoULoss in the regression branch loss is obtained. The model uses the DFL Loss loss function as another part of the regression branch loss. The calculation formula is as follows:
[0033] ;
[0034] in, Represents the true label value of the current sample, for different predicted values in the model With different predicted probabilities , and Representative and true labels The two most adjacent labels of and are the current probability values corresponding to the two adjacent labels respectively. By introducing the loss function, the model is allowed to compete among the adjacent labels, so that the model focuses on the true label and prompts the model to concentrate the predicted value on the true label more quickly.
[0035] The image input model will output the location information of the egg in the image after calculation. It will be output in the form of an annotation file and visualized as a polygonal anchor box of the predicted location in the image.
[0036] As a preferred solution of the egg shape index calculation and variety identification method described in the present invention, the further operation of the segmented image using multiple computer image processing techniques includes grayscale processing of the original image, using the canny operator to calculate the edge of the grayscale image, fine-tuning the parameter low threshold and high threshold If the gradient value of an edge pixel is higher than the high threshold, it is marked as a strong edge pixel. If the gradient value of an edge pixel is lower than the low threshold, it is considered that the gradient value of the edge pixel is not an edge. If the gradient value of an edge pixel is between the two thresholds, whether the gradient value of the edge pixel is an edge is determined based on whether there are strong edge pixels around the gradient value of the edge pixel.
[0037] Using the closing operation in morphological operation, after determining the kernel shape kernel and the number of iterations, the processing is performed according to the following expression:
[0038] ;
[0039] in, Represents the input source image; The shapes representing the kernels of the two operations; Represents the dilation operation of the image, which is used to fill some holes in the target area and eliminate small particle noise contained in the target area; Represents the expansion operation of the image, which is used to eliminate small and meaningless objects. The original image content within the contour is output as the final result of image segmentation. Represented as a closing operation in morphological operations.
[0040] As a preferred solution of the egg shape index calculation and variety identification method described in the present invention, wherein: the aspect ratio of the minimum circumscribed rectangle is the egg shape parameter of the egg, and the minimum circumscribed rectangle of the egg outline is calculated and constructed according to the parameters of the obtained polygon anchor frame, and the egg shape parameters of the egg in the image are determined by this, and the point with the smallest ordinate in the point set is selected, and the next point is found in turn in a counterclockwise direction, and finally a closed polygon is constructed, which is the convex hull of the point set, and the newly added point in the current convex hull is set as and , For the next vertex to be added, calculate the vector and Angle ,when When it is the minimum value, it is the next contour point to be added to the convex hull. The calculation formula is as follows:
[0041] ;
[0042] Repeat the steps of constructing a closed polygon to calculate the convex hull of the current point set, find the minimum enclosing rectangle and then calculate the egg-shaped parameters. To find the minimum enclosing rectangle of the convex hull point set, find the four coordinate points representing the maximum values of the horizontal and vertical coordinates according to the coordinate information, then make a horizontal tangent to the maximum value point of the vertical coordinate and a vertical tangent to the maximum value point of the horizontal coordinate. Four original tangents are obtained in the image, and the current rectangular area is recorded. , the calculation process is:
[0043] Rotate the tangent lines clockwise until one of them coincides with an edge of the polygon, and calculate the area of the new rectangle. Take the smaller value of the two (the smaller one after comparing the two, or the minimum value, but the comparison only occurs between the two, so the smaller value is used), and update the rectangle information. Stop updating the rectangle after the rotation angle is greater than 90°. The rectangle obtained at this time is the minimum circumscribed rectangle of the egg contour point set. Calculate the height and width of the minimum circumscribed rectangle according to the distance formula between two points in the plane. After obtaining the height and width of the minimum circumscribed rectangle, divide the height and width to get the value of the egg shape parameter.
[0044] As a preferred solution of the egg shape index calculation and variety identification method described in the present invention, the egg classification model is constructed by using information in the input image data to extract deep image features, and a convolutional neural network is used as the main network architecture for extracting image features. The original image is subjected to a layer-by-layer convolution process to gradually extract deep image information as key information for subsequent network classification. By stacking multiple convolutional layers, the resolution of the image is reduced and the number of channels for extracting feature maps is increased. The network structure is formed by stacking multiple convolutional blocks, and each convolutional block sequentially includes three processes: a convolutional layer, a batch normalization layer, and an activation function processing. The convolutional layer is the core part of the convolutional block, and is used to gradually extract deep feature maps from the image and obtain image information; batch normalization is used to normalize the output of the convolutional layer; and the activation function processing is used to introduce nonlinear transformations to help the network learn more complex mapping relationships.
[0045] Add the original input of the module to the output of the convolution block. The specific formula is as follows:
[0046] ;
[0047] in, represents the original input of the residual block, represents the output of the convolution branch, Represents the final output of the residual block;
[0048] The self-attention mechanism is introduced to enable the network to dynamically adjust the attention to different areas of the image during the learning process. The self-attention mechanism is integrated into the residual block to adjust the weight of the feature sequence. The self-attention mechanism considers the relationship between different positions, allowing the network to focus on the features that are more important for the classification task.
[0049] After the convolution block of the residual structure, the output feature sequence of the convolution block is input into the self-attention mechanism layer, and the feature sequence adjusted according to the context information is output. The input image sequence will calculate the attention weight corresponding to each position. The attention weight represents the importance of the position to the entire sequence. The calculated attention weight is used to perform weighted summation on the output of the convolution block to obtain the feature representation after attention adjustment.
[0050] Through fusion and stacking, the main components of the model network structure are formed. A fully connected layer is added at the end of the network to map the final features to each egg variety, and the classification probability is output through the Softmax activation function. The variety with the highest classification probability among multiple varieties is the variety predicted by the network.
[0051] As a preferred solution of the egg shape index calculation and variety identification method described in the present invention, the segmented egg image data is classified according to the egg classification model, including: after completing the network model construction process, training, evaluating and optimizing the network model, through training and adjustment, the model is adapted to specific needs, and the processed image data is divided into a training set, a validation set and a test set, and the three sets contain data of different types of eggs. In the model, the optimizer selects the Adam optimizer, and the loss function used is the cross entropy loss function, and its calculation formula is:
[0052] ;
[0053] in, is the number of samples, is the number of varieties in the current classification, It is a sample The true label category The value of is 0 or 1. It is a sample The probability distribution of the corresponding model output The predicted probability of Represents the cross entropy loss function adopted by the model;
[0054] The output of the model is calculated through forward propagation, and then the cross entropy loss function is used to calculate the difference between the predicted value and the true value. The gradient is calculated through back propagation, and the optimizer is used to update the model parameters. The optimization process is repeated continuously, the network parameters are updated iteratively, and finally the network gradually converges to the optimal performance.
[0055] Another object of the present invention is to provide an egg shape index calculation and variety identification system, which systematically collects image data of multiple egg varieties through the acquisition and processing module, and performs preprocessing to ensure data quality. The image segmentation processing module uses advanced technology to accurately segment egg images from complex backgrounds, and further optimizes these images through multiple image processing techniques. The contour convex hull analysis module then analyzes the egg contour in detail, accurately calculates the shape parameters, and provides key features for classification. The variety classification module uses these features to quickly and accurately classify egg varieties through a well-trained model, and continuously optimizes to improve accuracy and robustness.
[0056] As a preferred solution of the egg shape index calculation and variety identification system of the present invention, it includes: an acquisition processing module, an image segmentation processing module, a contour convex hull analysis module and a variety classification module;
[0057] An acquisition and processing module is used to acquire egg variety image data and pre-process the acquired egg variety image data;
[0058] The image segmentation processing module trains the image segmentation model to segment the target egg in the input image according to its contour, and uses a variety of computer image processing techniques to further operate on the segmented image;
[0059] The contour convex hull analysis module uses the convex hull algorithm to find all contour points contained in the convex hull of the egg contour according to the obtained egg contour label, and the aspect ratio of the obtained minimum circumscribed rectangle is the egg shape parameter;
[0060] The variety classification module builds an egg classification model, classifies the segmented egg image data according to the egg classification model, and optimizes the egg classification model.
[0061] A computer device comprises a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the steps of any one of the methods for calculating an egg shape index and identifying a variety of an egg are implemented.
[0062] A computer-readable storage medium stores a computer program, which, when executed by a processor, implements the steps of any one of the methods for calculating an egg shape index and identifying a variety.
[0063] The beneficial effects of the present invention are as follows: the present method realizes automatic calculation, and only needs to input an image to identify the outline of the egg and accurately calculate the egg shape index, with low manual participation and high automation. The present method has accurate target recognition, can efficiently identify the target in the input image and classify it, and has the advantages of high accuracy and high recall rate. The present method uses a variety of computer image processing methods to process the output results of the image segmentation neural network, so that the segmentation results are more refined, the accuracy of image segmentation is improved, and the accuracy of egg shape parameter calculation and image classification is improved. The present method uses a circumscribed rectangle to fit the outline of the egg in the image, and does not require manual annotation by the staff, which saves a lot of time and cost and improves processing efficiency. In order to solve the problem of classifying egg varieties, the present method designs and trains a convolutional neural network that integrates a self-attention mechanism, which can accurately and efficiently identify and classify the input image. The present method allows deployment in mobile devices, and users can upload the image to be identified by means of mobile phone apps or small programs, and after background calculation, the target egg shape index can be obtained. The present method can accurately identify and classify the eggs in the image according to the given image size, and efficiently calculate its egg shape index. Moreover, this method uses image segmentation algorithms to overcome the interference caused by background information and process various complex images. In addition, the recognition process is mainly completed by the equipment itself, with a high degree of automation, which greatly reduces labor and time costs. BRIEF DESCRIPTION OF THE DRAWINGS
[0064] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying creative work.
[0065] Figure 1 A schematic flow chart of a method for calculating an egg shape index and identifying a variety of an egg provided by an embodiment of the present invention.
[0066] Figure 2 An example of collected images of a method for calculating an egg shape index and identifying a variety of an egg provided by an embodiment of the present invention.
[0067] Figure 3 This is a diagram of the processing results of an image segmentation model for an egg shape index calculation and variety identification method provided by an embodiment of the present invention.
[0068] Figure 4 This is a result graph of an egg shape index calculation and variety identification method provided by an embodiment of the present invention, processed based on the output of an image segmentation model.
[0069] Figure 5 A result diagram of calculating the minimum circumscribed rectangle of an egg in an image in a method for calculating an egg shape index and identifying a variety provided by an embodiment of the present invention.
[0070] Figure 6 The main network model architecture used in an example of a method for calculating an egg shape index and identifying a variety provided by an embodiment of the present invention.
[0071] Figure 7 An egg shape index calculation and variety identification method provided by an embodiment of the present invention is the main network model architecture used in the example.
[0072] Figure 8 A confusion matrix obtained by testing a classification model of a method for calculating an egg shape index and identifying a variety provided by an embodiment of the present invention.
[0073] Fig. 9 A schematic diagram of the architecture of an egg shape index calculation and variety identification system provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0074] In order to make the above-mentioned purposes, features and advantages of the present invention more obvious and easy to understand, the specific implementation methods of the present invention are described in detail below in conjunction with the drawings of the specification. Obviously, the described embodiments are part of the embodiments of the present invention, but not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary persons in the art without creative work should fall within the scope of protection of the present invention.
[0075] Example 1
[0076] Reference Figure 1 , which is the first embodiment of the present invention, provides a method for calculating an egg shape index and identifying a variety of eggs, comprising:
[0077] S1. Collect egg variety image data, and pre-process the collected egg variety image data.
[0078] In this method, it is necessary not only to efficiently calculate the egg shape index, but also to accurately classify the egg varieties in the image. Therefore, it is necessary to collect image data of multiple egg varieties. In order to facilitate subsequent operations, the same hardware device is used when collecting images to ensure uniform image specifications; secondly, bright spots caused by strong light interference in the shooting environment should be avoided when collecting images, which will affect the recognition accuracy to a certain extent; in addition, the collected data set should contain egg images of different varieties, different angles, and different backgrounds as much as possible to improve the generalization of the trained model.
[0079] After completing the image acquisition, preprocessing operations are required. The purpose of this step is to screen the collected images one by one and remove sample data that does not meet the requirements. In order to ensure the quality and consistency of the data used, images with blur, severe exposure, etc. are removed from the data set. Then the image is denoised to improve the clarity of the image. In addition, in order to meet the needs of the subsequent image segmentation process and ensure the accuracy of the label, this method uses manual annotation to label the position of the eggs in the image, and uses a polygonal anchor frame to mark the outline of the target egg. The label format of the generated annotation file is:
[0080] ;
[0081] in, The label number of the target, the default is 0; Represents the nth vertex coordinate of the polygonal anchor box. Connecting the n vertex coordinates in sequence is the label outline of the image.
[0082] In order to better improve the training effect of the subsequent network model, it is necessary to use data enhancement technology to process the above-mentioned annotated images, which helps to improve the robustness of the model and reduce the risk of overfitting. In this process, two data enhancement technologies are mainly used: image scaling, random cropping and random rotation.
[0083] Image scaling and random cropping: mainly using parameters , and To achieve this, set The parameter specifies the image size of the input model. At the same time, set Parameters can control the cropping ratio of the image, setting The range of random cropping aspect ratio can be controlled. These two data enhancement techniques help the model learn to adapt to targets of different sizes and increase data diversity.
[0084] Random rotation: By randomly rotating the image within a certain range, the model can better learn the features of objects at different angles. In this method, by setting Parameters are used to control the range of random rotation angles so that the model can adapt to input targets at different angles.
[0085] S2. Train the image segmentation model to segment the target egg in the input image according to its contour.
[0086] This step is used to train an image segmentation model to segment the target egg in the input image according to its contour. In the previous step, the image is preprocessed to mark the clear egg contour information from the image in the form of a label, which is shown in formula (1). The labeled image data in the previous step is divided into a training set and a test set. The model learns and generalizes the knowledge learned on the training set. Then the test set is used as the input of the trained model for testing. The model gives the predicted egg contour in the test set image. Finally, the predicted egg contour is compared with the true contour of the label. The prediction error of the model is calculated using the loss function, and the model parameters are updated by back propagation. The loss function used in the image segmentation model consists of two branches: classification and regression branches. Its calculation formula is as follows:
[0087] ;
[0088] in, is the overall loss of this image segmentation model, and are the loss functions used for classification and regression branches respectively, , are the weights of the two losses respectively.
[0089] For the loss of the classification branch, the binary cross entropy loss (BCE Loss) is used to measure it. It compares the true label with the model's prediction, maps it to the same scale through a logarithmic transformation, and then calculates the loss of the prediction process. The calculation formula is as follows: ;
[0090] in, is the true label of the current sample, which takes a value of 0 or 1. is the predicted output of the model. After the sigmoid function, it represents the probability that the sample belongs to the positive category. According to the above formula, the loss of the current classification branch can be obtained.
[0091] As for the loss of the regression branch, this model uses two loss functions, DFL Loss and CIoU Loss, to measure it and assign different weights to them. CIoU Loss is used to calculate the positioning loss in the regression branch. It measures the quality of the prediction box by considering the overlapping area between the prediction box and the target box, the center point comparison between the two boxes, and other information. To calculate the positioning loss, you first need to calculate the intersection over union (IoU) of the prediction box and the target box. The calculation formula is as follows:
[0092] ;
[0093] in, and Refers to the area of the true annotation box and the predicted polygon box. Represents the intersection area of the real annotation box and the predicted polygon box, Represents the union area of the true annotation box and the predicted polygon box.
[0094] The positioning loss can be calculated based on the intersection-and-union ratio, and the calculation formula is as follows:
[0095] ;
[0096] in, is the center point of the real annotation box, is the center point of the predicted polygonal box, is the Euclidean distance between the center point of the real annotation box and the predicted polygon box, It is the diagonal distance of the minimum closure area between the true annotation box and the predicted polygon box. To predict the consistency parameter representation of the aspect ratio of the annotation box, the calculation formula is:
[0097] ;
[0098] in, , is the width and height of the real label box, , To predict the width and height of the polygonal box, the weight parameters in formula (5) can be calculated , which represents the proportion of the aspect ratio of the predicted annotation box in the loss, and its calculation formula is as follows:
[0099] ;
[0100] in, represents the aspect ratio parameter obtained in formula (6), is the intersection-union ratio obtained in formula (4).
[0101] After using the above formula to calculate part of the regression branch, in order to allow the network to quickly focus on the value near the label, this model uses the DFL Loss function as another part of the loss of the regression branch. The calculation formula is as follows:
[0102] ;
[0103] in, Represents the true label value of the current sample. In this model, for different predicted values With different predicted probabilities , in the above formula and That is, the representative and true labels The two most adjacent labels of and are the current probability values corresponding to the two adjacent labels. By introducing the above loss function, the model can generate competition among adjacent labels, making the model pay more attention to the real label and prompting the model to focus the predicted value on the real label more quickly, which helps to improve the model's segmentation ability for images with blurred edges.
[0104] After calculating the loss obtained in one round of training using the above series of loss function formulas, the model parameters are updated through the back-propagation process and iterative training is performed to continuously improve the model performance, so that the predicted position is gradually close to the actual label position. After calculation, the image input model will output the position information of the egg in the image, and its content will be output in the form of an annotation file of formula (1), which can be visualized as a polygonal anchor box of the predicted position in the image.
[0105] After the above process, the labeled data can be used to train and optimize the constructed image segmentation model, and then this model can be used to perform more accurate image segmentation. However, the output image obtained by image segmentation using this model always has positioning ambiguity at the edges, which is manifested as more severe jagged edges. Considering that both the calculation of egg shape parameters and the classification of egg varieties require images with better edge segmentation and more refined processing as a basis, in this method, not only a deep learning model is used for segmentation of egg images, but also a variety of computer image processing techniques are used to further operate on the segmented images. The specific steps are as follows:
[0106] First, grayscale the original image to reduce computational complexity and improve processing speed. Then, the canny operator is used to calculate the possible edges in the grayscale image. In this process, two main parameters need to be fine-tuned: the low threshold ( ) and high threshold ( ), where if the gradient value of an edge pixel is higher than a high threshold, it is marked as a strong edge pixel; if the gradient is lower than a low threshold, it is considered not to be an edge; if it is between the two thresholds, it is judged whether it is an edge based on whether there are strong edge pixels around it.
[0107] After the above operation, the main edge information in the image can be found, but the specific edge of the egg needs to be further determined. Therefore, the image is processed using the above image segmentation model to obtain the bounding box information of the egg in the image. Since the shape of the egg is similar to an ellipse, an ellipse mask is used in this method to filter the edge obtained in the previous step. The specific operation is: the center of the target frame is used as the center of the ellipse, and one-third of its length and width are used as the major and minor semi-axes of the ellipse respectively. Then the radius of the ellipse is continuously expanded until the number of edge pixels of the newly added ellipse is less than 5 for several consecutive rounds, and the current radius increment is recorded. Then the middle part of the two ellipses at the beginning and end is bitwise ANDed with the original edge to update the edge information.
[0108] In addition, since the contour obtained by the above process is likely to have an unclear edge or small internal holes, the closing operation in the morphological operation is used. After determining the kernel shape kernel and its iteration number iteration, the iteration number iteration represents the round of iterative calculation according to the following formula. It is an adjustable parameter, not a calculated variable in the following formula. It is processed according to the following expression:
[0109] ;
[0110] in, Represents the input source image; The shapes representing the kernels of the two operations; Represents the dilation operation of the image, which is used to fill some holes in the target area and eliminate small particle noise contained in the target area; Represents the expansion operation of the image, which is used to eliminate small and meaningless objects. After the above operation, the edge structure of the egg in the image is optimized, and the original image content within the outline is output as the final result of image segmentation. This step combines the deep learning model and traditional image processing technology to obtain more accurate egg segmentation results. At the same time, in order to solve the problem of blurred edges in the model output image, the refinement and optimization operation of the ellipse edge is used to obtain a clearer segmentation effect.
[0111] S3. According to the obtained egg contour label, all contour points contained in the convex hull of the egg contour are obtained using a convex hull algorithm, and the aspect ratio of the obtained minimum circumscribed rectangle is the egg shape parameter of the egg.
[0112] The main function of this step is to calculate the minimum bounding rectangle of the egg contour based on the parameters of the polygon anchor box obtained in the previous step, and use this to determine the egg shape parameters of the egg in the image. In order to calculate the minimum bounding rectangle of the point set representing the egg contour, the convex hull of this point set must be calculated first. First, select the point with the smallest ordinate in the point set, find the next point in a counterclockwise direction, and finally construct a closed polygon as its convex hull. Let the newly added point in the current convex hull be and , For the next vertex to be added, calculate the vector and Angle ,when When it is the minimum value, it is the next contour point to be added to the convex hull. The calculation formula is as follows:
[0113] ;
[0114] Repeat the above steps to calculate the convex hull of the current point set, and then use this to find the minimum enclosing rectangle and then calculate the egg-shaped parameters. To find the minimum enclosing rectangle of the convex hull point set in the previous step, first find the four coordinate points representing the maximum values of the horizontal and vertical coordinates based on the coordinate information, then make a horizontal tangent to the maximum value point of the vertical coordinate and a vertical tangent to the maximum value point of the horizontal coordinate, so that four original tangents are obtained in the image. Then record the current rectangular area , and its calculation formula is:
[0115] Then rotate the tangent line clockwise until one of the tangent lines coincides with an edge of the polygon. At this time, calculate the area of the new rectangle. Take the smaller value of the two and update the rectangle information. Stop updating the rectangle after the rotation angle is greater than 90°. The rectangle obtained at this time is the minimum circumscribed rectangle of the egg outline point set. Calculate the height and width of the minimum circumscribed rectangle according to the distance formula between two points in the plane. After obtaining the height and width of the minimum circumscribed rectangle, divide the two to get the value of the egg shape parameter.
[0116] S4. Construct an egg classification model, classify the segmented egg image data according to the egg classification model, and optimize the egg classification model.
[0117] The main purpose of this step is to build a network model for classifying multiple egg varieties. The image to be classified has been accurately segmented by the trained image instance segmentation model, which greatly eliminates the influence of background environment on the classification of egg varieties. In this method, a convolutional neural network model with self-attention mechanism is designed for the egg classification task for the image instance to be classified.
[0118] First, to make full use of the information in the input image data, it is necessary to extract deep image features from the image. In this method, a convolutional neural network is used as the main network architecture for extracting image features. The original image is subjected to a layer-by-layer convolution process to gradually extract deep image information as the key information for subsequent network classification. In this network structure, by stacking multiple convolutional layers, the resolution of the image is reduced, the number of channels of the extracted feature map is increased, and the receptive field is gradually expanded, so that more abstract and more complex features can be extracted. The main structure of the network is composed of multiple convolutional blocks stacked together, and each convolutional block contains three processes in sequence: convolutional layer, batch normalization layer, and activation function processing. The convolutional layer is the core part of the convolutional block, which is used to gradually extract deep feature maps from the image and obtain image information; batch normalization is used to normalize the output of the convolutional layer, accelerate the training convergence process of the network, and improve the stability of the model; activation function processing is used to introduce nonlinear transformations to help the network learn more complex mapping relationships, so that it has stronger expression capabilities.
[0119] Secondly, in order to prevent the gradient from disappearing or exploding during the training of deep neural networks, and to speed up the convergence of the network and improve the generalization performance of the model, the concept of residual is introduced on the basis of convolutional neural networks. By adding a skip connection branch to the original convolution block branch, the specific operation is to add the original input of the module to the output result of the convolution block. The specific formula is as follows:
[0120] ;
[0121] in, represents the original input of the residual block, represents the output of the convolution branch, Represents the final output of the residual block. The addition of the residual block enables the neural network to easily train and optimize deep networks, learn feature changes more intensively, and improve the network's ability to represent data.
[0122] In addition, since the contextual information in the image obviously plays an important role in correctly classifying the egg varieties in this task, the self-attention mechanism is introduced in the network model structure to enable the network to dynamically adjust the attention to different areas of the image during the learning process, thereby improving the classification accuracy. The self-attention mechanism is integrated into the residual block to adjust the weight of the feature sequence. The introduction of this self-attention mechanism allows the network to dynamically learn the correlation between different egg parts, thereby improving the sensitivity of egg variety classification. By considering the relationship between different positions, the attention mechanism enables the network to focus more specifically on features that are more important for the classification task.
[0123] The specific implementation process is to input the output feature sequence of the convolution block into the self-attention mechanism layer after the convolution block of the residual structure, and output the feature sequence adjusted according to the context information. In this process, the input image sequence will calculate the attention weight corresponding to each position. These weights represent the importance of the position to the entire sequence. Then, the calculated attention weights are used to perform weighted summation on the output of the convolution block to obtain the feature representation after attention adjustment. This weighted summation process allows the network to focus more on the parts that are more critical to the classification task, and improves the network's sensitivity to the correlation between different egg parts.
[0124] Finally, the fusion and stacking of the above parts constitute the main components of the network structure of this model. A fully connected layer is added at the end of the network to map the final features to each possible egg variety, and the classification probability is output through the Softmax activation function. Finally, the variety with the highest classification probability among multiple varieties is the variety predicted by the network.
[0125] After completing the process of building the network model, the next key step is to train, evaluate and optimize the network model. This process is an indispensable part of this method, and effective training and adjustment are required to enable the model to better adapt to the specific needs of this task. First, the processed image data is divided into training set, validation set and test set to ensure that these three sets contain data of different types of eggs, which helps the model learn the characteristics of different categories of eggs and improve its generalization ability. Subsequently, the appropriate loss function, optimizer, learning rate and batch size and other training hyperparameters are selected to complete the initialization process. Among them, the optimizer in this model uses the Adam optimizer. In addition, since the main purpose of this method is to complete multi-classification tasks, the loss function used in this model is the cross entropy loss function, and its calculation formula is:
[0126] ;
[0127] in, is the number of samples, is the number of varieties in the current classification, It is a sample The true label category The value of is 0 or 1. It is a sample The probability distribution of the corresponding model output The predicted probability of .
[0128] Then, multiple rounds of iterative training are started, and the output of the model is calculated continuously through forward propagation. Then, the cross entropy loss function is used to calculate the difference between the predicted value and the true value. Then, the gradient is calculated through back propagation, and the optimizer is used to update the model parameters. The above process is repeated continuously, the network parameters are updated iteratively, and finally the network gradually converges to the best performance. In order to test the performance of the trained model, the model is evaluated using multiple evaluation criteria such as accuracy and F1. Then, various parameters of the model are fine-tuned and retrained according to the evaluation results. After continuous optimization, the network model finally obtained by this method is obtained, and its classification accuracy and recall rate exceed the existing methods, which meets the needs of this problem and provides solutions to other similar problems.
[0129] Example 2
[0130] The second embodiment of the present invention is different from the previous embodiment in that:
[0131] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, server, or network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, RandomAccess Memory), disk or optical disk, etc., which can store program codes.
[0132] The logic and / or steps represented in the flowchart or otherwise described herein, for example, can be considered as an ordered list of executable instructions for implementing logical functions, and can be embodied in any computer-readable medium for use by an instruction execution system, device or apparatus (such as a computer-based system, a system including a processor, or other system that can fetch instructions from an instruction execution system, device or apparatus and execute instructions), or in conjunction with such instruction execution systems, devices or apparatuses. For the purposes of this specification, "computer-readable medium" can be any device that can contain, store, communicate, propagate or transmit a program for use by an instruction execution system, device or apparatus, or in conjunction with such instruction execution systems, devices or apparatuses.
[0133] More specific examples of computer-readable media (a non-exhaustive list) include the following: an electrical connection with one or more wires (electronic device), a portable computer disk case (magnetic device), a random access memory (RAM), a read-only memory (ROM), an erasable and programmable read-only memory (EPROM or flash memory), an optical fiber device, and a portable compact disk read-only memory (CDROM). In addition, the computer-readable medium may even be a paper or other suitable medium on which the program is printed, since the program may be obtained electronically, for example, by optically scanning the paper or other medium, followed by editing, deciphering or, if necessary, processing in another suitable manner, and then stored in a computer memory.
[0134] It should be understood that the various parts of the present invention can be implemented by hardware, software, firmware or a combination thereof. In the above-mentioned embodiments, multiple steps or methods can be implemented by software or firmware stored in a memory and executed by a suitable instruction execution system. For example, if implemented by hardware, as in another embodiment, it can be implemented by any one of the following technologies known in the art or their combination: a discrete logic circuit having a logic gate circuit for implementing a logic function for a data signal, a dedicated integrated circuit having a suitable combination of logic gate circuits, a programmable gate array (PGA), a field programmable gate array (FPGA), etc.
[0135] Example 3
[0136] Reference Figure 2-Figure 8 The third embodiment of the present invention provides a method for calculating the egg shape index and identifying the variety of an egg. In order to verify the beneficial effects of the present invention, scientific demonstration is carried out through experiments.
[0137] 1) Image acquisition: Select image data of eggs of different varieties according to the task requirements, and ensure that the image data covers various postures, lighting conditions and background conditions to ensure that the model can have good generalization ability in practical applications. In addition, in order to further improve the generalization, the resolution of the collected images is not consistent, so that the model can adapt to inputs of various resolutions. According to statistics, this example includes three common egg varieties: ordinary, Taihe, and bamboo silk eggs. A total of 2,651 images were taken. Figure 2 Take 4 sample images.
[0138] 2) Image preprocessing: After completing the image acquisition, the next step is to remove images that are not in compliance with the standards, such as blurry images and bright spots, to ensure the quality of the data set. In addition, in order to increase the number of images and improve the generalization performance of the model, the images are further processed using data augmentation technology, mainly using two algorithms: image scaling, random cropping, and random rotation. The parameters is the size of the image after cropping. Specifies the relative size range of the cropping area. The two values of the tuple represent the ratio of the minimum size to the maximum size. Specify the aspect ratio range of the cropped image. Specify the angle range of random rotation. The parameters are as follows: ;
[0139] By using these three data augmentation techniques, the model’s performance for images of different angles and sizes is improved, thereby improving the generalization of the model.
[0140] 3) Image segmentation: Considering that the collected egg images have complex backgrounds and inconsistent shooting conditions, this example uses a deep learning model to segment the image data, thereby greatly eliminating the impact of the background on classification, thereby improving the classification accuracy of the model. First, the image data needs to be labeled. This example uses the Labelme software package to label the images. Automatic labeling generally has errors. The best method is to use manual labeling methods to generate high-quality label files. Compared with automatic labeling, manual labeling can greatly improve the accuracy of the model. Therefore, this example uses manual labeling methods to label a total of 500 images, which are divided into training sets, validation sets, and test sets using a 7:1:2 division method.
[0141] The image segmentation model structure is shown in the attached Figure 3 As shown, the model parameters is the number of training rounds, is the batch size for training, For the training image size, the parameters are set as follows:
[0142] ;
[0143] Then, we use formula (2) to calculate the difference between the image segmentation prediction result and the true label, and then adjust the model parameters and weights through the model back propagation process. After multiple rounds of repeated iterations, we get the optimal model segmentation effect. After continuous adjustment and optimization, the model can finally accurately segment the egg image from the image. Some of the segmentation results are as follows: Figure 4 On this basis, this example also combines a variety of computer image processing technologies with the image segmentation model. After the image is grayed out, the canny operator is used to extract all possible edges in the image. In this process, two gradient thresholds need to be restricted. represents the low threshold, Represents the high threshold, both define the gradient boundary, and the parameters are set as follows:
[0144] ;
[0145] After extracting the edge, we first use the trained deep learning model to get the target frame information, then iteratively update the ellipse radius using the number of pixels that intersect the original edge with the ellipse mask with the target frame information as the initial value, and finally calculate the original edge within the two ellipses and use it to update the edge information to eliminate the interference of non-egg edges. Then, we use the erosion and dilation process in the morphological operation to further process the edge. The parameters in this process are mainly the shape of the operation kernel. and the number of iterations, the parameters are set as follows:
[0146] ;
[0147] in represent is a 5×5 matrix of all 1s. After the above series of operations, the image is segmented completely and smoothly. The segmentation result is as follows Figure 5 shown.
[0148] 4) Calculation of egg shape parameters: In order to better understand the parameters of eggs and provide convenience for users to calculate egg shape parameters, this example includes the process of calculating egg shape parameters in the method. First, based on the egg contour labels obtained from all images, the convex hull algorithm is used to first calculate all the contour points contained in the convex hull of the contour. On this basis, the rotating caliper algorithm is used to calculate the length and width of the minimum circumscribed rectangle of the convex hull. The length-to-width ratio of the minimum circumscribed rectangle is the egg shape parameter of the egg. The result of calculating the minimum circumscribed rectangle is shown in the figure below. Figure 6 shown.
[0149] 5) Model building: In this step, the core network architecture of this example is constructed to quickly and accurately classify the segmented egg image data. The network model designed in this example is based on a convolutional neural network and integrates the residual concept. The final output of the convolution block is integrated with the original input of this module. This not only avoids the occurrence of gradient vanishing and gradient explosion phenomena, but also speeds up the convergence of the network. In addition, considering that contextual information plays an important role in the accuracy of egg classification, a self-attention mechanism is introduced on the basis of the residual network architecture to reasonably adjust the feature sequence, thereby improving the classification accuracy. Finally, these modules are stacked as needed and fully connected layers, activation function layers, etc. are added to the end of the network to output the classification results, thereby completing the core network used for egg variety classification in this example. The network model architecture is shown in the attached figure. Figure 6 and Figure 7 shown.
[0150] 6) Model training, evaluation and optimization: After completing the construction of the network model, the next step is to use the data obtained in the previous step to train the constructed network model. During the training process, the initialization hyperparameter values and training parameters must be specified, among which, is the training image size, is the training round, is the batch size for training, The type of optimizer used for the training process, To control the number of rounds required for early stopping, the above parameters are set as follows:
[0151] ;
[0152] After the classification result is obtained according to the current model, the classification loss is calculated using the loss function, as shown in formula (10). Then, back propagation is used to update the parameters and weights of each layer of the model, and the model is gradually optimized. In the multiple rounds of iterations, the model effect is measured by the value obtained by the loss function, the prediction accuracy, etc., so that the loss is gradually reduced and the accuracy is gradually improved during the continuous training process. The iteration is repeated until the model gradually converges during the gradient descent process, and the performance and classification accuracy are gradually improved, thereby obtaining the final classification model. The final model evaluation result is displayed in the form of a confusion matrix as shown below: Figure 8 , it can be seen that the classification accuracy has reached the actual production requirements.
[0153] Example 4
[0154] refer to Fig. 9 , which is the fourth embodiment of the present invention, and provides an egg shape index calculation and variety identification system, characterized in that it includes: an acquisition processing module, an image segmentation processing module, a contour convex hull analysis module and a variety classification module;
[0155] An acquisition and processing module is used to acquire egg variety image data and pre-process the acquired egg variety image data;
[0156] The image segmentation processing module trains the image segmentation model to segment the target egg in the input image according to its contour, and uses a variety of computer image processing techniques to further operate on the segmented image;
[0157] The contour convex hull analysis module uses the convex hull algorithm to find all contour points contained in the convex hull of the egg contour according to the obtained egg contour label, and the aspect ratio of the obtained minimum circumscribed rectangle is the egg shape parameter;
[0158] The variety classification module builds an egg classification model, classifies the segmented egg image data according to the egg classification model, and optimizes the egg classification model.
[0159] It should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit the present invention. Although the present invention has been described in detail with reference to the preferred embodiments, those skilled in the art should understand that the technical solutions of the present invention may be modified or replaced by equivalents without departing from the spirit and scope of the technical solutions of the present invention, which should all be included in the scope of the claims of the present invention.
Claims
1. A method for calculating egg shape index and identifying varieties of eggs, characterized by: include, Collecting egg variety image data, and preprocessing the collected egg variety image data; Train the image segmentation model and segment the target egg in the input image according to its contours, and use a variety of computer image processing techniques to further operate on the segmented image to improve segmentation accuracy; According to the obtained egg contour label, the convex hull algorithm is used to find all the contour points contained in the convex hull of the egg contour, and the minimum circumscribed rectangle of the convex hull is obtained by the rotating caliper algorithm. Finally, the aspect ratio of the minimum circumscribed rectangle is calculated to obtain the egg shape parameters. Build and train an egg classification model, use the egg classification model to classify the segmented egg image data, and optimize the performance of the egg classification model; The preprocessing uses manual labeling to label the position of the eggs in the image, and uses polygonal anchor frames to label the contours of the target eggs. The label format of the generated label file is: (s obj ,x pos1 ,and pos1 ;x pos2 ,and pos2 ;x pos3 ,and pos3 ;…) Among them, s obj is the label number of the target, the default value is 0, which refers to the egg marked in the image; (x posn ,y posn ) represents the nth vertex coordinate of the polygon anchor box. Connecting the n vertex coordinates in sequence is the label outline of the image, that is, the location information of the egg in the image; Processing the labeled images using data augmentation techniques, including image scaling, random cropping, and random rotation; The image scaling and random cropping are achieved by using parameters imgsz, ratio and scale. The imgsz parameter is set to specify the image size of the input model, the scale parameter is set to control the image cropping ratio, and the ratio is set to control the range of the random cropping aspect ratio. The random rotation allows the model to learn the features of objects at different angles by randomly rotating the image within a specified range. The range of the random rotation angle is controlled by setting the degree parameter so that the model can adapt to input targets at different angles. The image segmentation model includes training an image segmentation model to segment the target egg in the input image according to its contour, marking the clear egg contour information from the image in the form of a label after image preprocessing, dividing the labeled image data into a training set and a test set, the model learns and generalizes the knowledge learned on the training set, uses the test set as the input of the trained model for testing, the model gives the predicted egg contour in the test set image, uses the predicted egg contour to compare with the real contour of the label, uses the loss function to calculate the prediction error of the model, and performs back propagation to update the model parameters. The loss function used in the image segmentation model consists of a classification branch and a regression branch, and the calculation formula is expressed as: Loss seg =λ1L cls +λ2L reg Among them, Loss seg is the overall loss of the image segmentation model, L cls and L reg are the loss functions used for the classification branch and the regression branch respectively, and λ1 and λ2 are the weights of the two losses respectively; The loss of the classification branch is measured by binary cross entropy loss, which compares the true label with the model's prediction, maps them to the same scale through logarithmic transformation, and then calculates the loss of the prediction process. The calculation formula is as follows: L cls =-(y*log(p)+(1-y)*log(1-p)) Among them, y is the true label of the current sample, indicating whether there is an egg in the image. If so, it is 1, otherwise it is 0; p is the predicted output of the model, which is the model's predicted probability of eggs in the sample. After the sigmoid function, it represents the probability that the sample is a positive class, and the loss of the current classification branch is obtained; The loss of the regression branch is measured by two loss functions, DFL Loss and CIoU Loss, and different weights are assigned to the two loss functions. CIoU Loss calculates the positioning loss in the regression branch, and measures the quality of the prediction frame by considering the overlapping area between the prediction frame and the target frame and the center point comparison information between the two frames. The positioning loss is calculated, and the intersection-over-union ratio I0U of the prediction frame and the target frame is calculated. The calculation formula is as follows: Among them, x a and a Refers to the area between the real annotation box and the predicted polygon box, Intersection(x a ,y a ) represents the intersection area of the real annotation box and the predicted polygon box, Union(x a ,y a ) represents the union area of the true annotation box and the predicted polygon box, The positioning loss is calculated based on the intersection-and-union ratio, and the calculation formula is as follows: Among them, α l Expressed as weight coefficient, b obj is the center point of the true annotation box, that is, the image contour label marked in each image during the data annotation stage; b gt is the center point of the egg outline predicted by the model, ρ obj is the Euclidean distance between the center point of the real annotation box and the predicted polygon box, c obj is the diagonal distance between the minimum closure area of the real annotation box and the predicted polygonal box, vl is the consistency parameter representation of the aspect ratio of the predicted annotation box, and its calculation formula is: Among them, w and h are the width and height of the center point of the egg outline, W gt 、h gt Calculate the weight parameter α for the width and height of the egg outline predicted by the model l , which represents the proportion of the egg outline aspect ratio predicted by the model in the loss, and the calculation formula is as follows: According to the proportion of the aspect ratio of the predicted annotation box in the loss, a part of the CIoU Loss in the regression branch loss is obtained. The model uses the DFL Loss loss function as another part of the loss of the regression branch. The calculation formula is as follows: Among them, y represents the true label value of the current sample, and in the model, for different predicted values y a With different predicted probabilities y i With y i+1 represents the two most adjacent labels to the true label y, and are the current probability values corresponding to the two adjacent labels respectively. By introducing the loss function, the model is allowed to compete among the adjacent labels, so that the model focuses on the true label and prompts the model to concentrate the predicted value on the true label more quickly. The image input model will output the location information of the egg in the image after calculation. It will be output in the form of an annotation file and visualized as a polygonal anchor box of the predicted location in the image.
2. A method for calculating egg shape index and identifying varieties of eggs as claimed in claim 1, characterized in that: The further operation of the segmented image using a variety of computer image processing techniques includes grayscale processing of the original image, using the canny operator to calculate the edge of the grayscale image, and fine-tuning the parameter low threshold threshold and high threshold high threshold If the gradient value of an edge pixel is higher than the high threshold, it is marked as a strong edge pixel. If the gradient value of an edge pixel is lower than the low threshold, it is considered that the gradient value of the edge pixel is not an edge. If the gradient value of an edge pixel is between the two thresholds, whether the gradient value of the edge pixel is an edge is determined based on whether there are strong edge pixels around the gradient value of the edge pixel. Using the closing operation in morphological operation, after determining the kernel shape kernel and the number of iterations, the processing is performed according to the following expression: close(src,kernel)=erode(dilate(src,kernel)) Among them, src represents the input source image; kernel represents the shape of the kernel of the two operations; dilate() represents the dilation operation of the image, which is used to fill some holes in the target area and eliminate the small particle noise contained in the target area; erode() represents the dilation operation of the image, which is used to eliminate small and meaningless targets. The output of the original image content within the contour is the final result of the image segmentation, and close() represents the closing operation in the morphological operation.
3. A method for calculating egg shape index and identifying varieties of eggs as claimed in claim 2, characterized in that: The aspect ratio of the minimum circumscribed rectangle is the egg-shaped parameter of the egg, and the minimum circumscribed rectangle of the egg outline is calculated and constructed according to the parameters of the obtained polygonal anchor frame, and the egg-shaped parameters of the egg in the image are determined by this, and the point with the smallest ordinate in the point set is selected, and the next point is found in turn in a counterclockwise direction, and finally a closed polygon is constructed, that is, the point set package, and the newly added points in the current convex hull are set as P0 (x0, y0) and P1 (x1, y1), and P2 (x2, y2) is the next vertex to be added, and the angle β between the vectors P0P1 and P1P2 is calculated. P , when β P When it is the minimum value, it is the next contour point to be added to the convex hull, β P The calculation formula is as follows: Repeat the steps of constructing a closed polygon to calculate the convex hull of the current point set, find the minimum enclosing rectangle and then calculate the egg-shaped parameters. To find the minimum enclosing rectangle of the convex hull point set, find the four coordinate points representing the maximum values of the horizontal and vertical coordinates according to the coordinate information, then make a horizontal tangent to the maximum value point of the vertical coordinate and a vertical tangent to the maximum value point of the horizontal coordinate. Four original tangents are obtained in the image, and the current rectangular area S is recorded. min , the calculation process is: Rotate the tangents clockwise until one of them coincides with an edge of the polygon, and calculate the area of the new rectangle, S min Take the smaller value of the two and update the rectangle information. Stop updating the rectangle after the rotation angle is greater than 90°. The rectangle obtained at this time is the minimum circumscribed rectangle of the egg contour point set. Calculate the height and width of the minimum circumscribed rectangle according to the distance formula between two points in the plane. After obtaining the height and width of the minimum circumscribed rectangle, divide the height and width to get the value of the egg shape parameter.
4. A method for calculating egg shape index and identifying varieties of eggs as claimed in claim 3, characterized in that: The egg classification model constructed and trained utilizes information in input image data to extract deep image features, adopts convolutional neural network as the main network architecture for extracting image features, and subjecting the original image to a layer-by-layer convolution process to gradually extract deep image information as key information for subsequent network classification. By stacking multiple convolutional layers, the resolution of the image is reduced, and the number of channels for extracting feature maps is increased. The network structure is stacked with multiple convolutional blocks, and each convolutional block sequentially includes three processes: convolutional layer, batch normalization layer, and activation function processing. The convolutional layer is the core part of the convolutional block, and is used to gradually extract deep feature maps from the image and obtain image information; batch normalization is used to normalize the output of the convolutional layer; and activation function processing is used to introduce nonlinear transformation to help the network learn more complex mapping relationships. Add the original input of the module to the output of the convolution block. The specific formula is as follows: Res(X i )=Conv(X i )+X i Among them, X i Represents the original input of the residual block, Conv(X i ) represents the output of the convolution branch, Res(X i ) represents the final output of the residual block; The self-attention mechanism is introduced to enable the network to dynamically adjust the attention to different areas of the image during the learning process. The self-attention mechanism is integrated into the residual block to adjust the weight of the feature sequence. The self-attention mechanism considers the relationship between different positions, allowing the network to focus on the features that are more important for the classification task. After the convolution block of the residual structure, the output feature sequence of the convolution block is input into the self-attention mechanism layer, and the feature sequence adjusted according to the context information is output. The input image sequence will calculate the attention weight corresponding to each position. The attention weight represents the importance of the position to the entire sequence. The calculated attention weight is used to perform weighted summation on the output of the convolution block to obtain the feature representation after attention adjustment. Through fusion and stacking, the main components of the model network structure are formed. A fully connected layer is added at the end of the network to map the final features to each egg variety, and the classification probability is output through the Softmax activation function. The variety with the highest classification probability among multiple varieties is the variety predicted by the network.
5. A method for calculating egg shape index and identifying varieties of eggs as claimed in claim 4, characterized in that: The egg classification model classifies the segmented egg image data. After completing the network model building process, the network model is trained, evaluated and optimized. Through training and adjustment, the model is adapted to specific needs. The processed image data is divided into a training set, a validation set and a test set. The three sets contain data of different types of eggs. In the model, the optimizer uses the Adam optimizer, and the loss function used is the cross entropy loss function, which is calculated as follows: Among them, N is the number of samples, C is the number of varieties currently classified, and yi j is the value of category j in the true label of sample i, which takes a value of 0 or 1. is the predicted probability of category j in the probability distribution of the model output corresponding to sample i, Loss ent Represents the cross entropy loss function adopted by the model; The output of the model is calculated through forward propagation, and then the cross entropy loss function is used to calculate the difference between the predicted value and the true value. The gradient is calculated through back propagation, and the optimizer is used to update the model parameters. The optimization process is repeated continuously, the network parameters are updated iteratively, and finally the network gradually converges to the optimal performance.
6. A system based on the egg shape index calculation and variety identification method according to any one of claims 1 to 5, characterized in that: Including, acquisition processing module, image segmentation processing module, contour convex hull analysis module and variety classification module; An acquisition and processing module is used to acquire egg variety image data and pre-process the acquired egg variety image data; The image segmentation processing module trains the image segmentation model to segment the target egg in the input image according to its contour, and uses a variety of computer image processing techniques to further operate on the segmented image; The contour convex hull analysis module uses the convex hull algorithm to find all contour points contained in the convex hull of the egg contour according to the obtained egg contour label, and the aspect ratio of the obtained minimum circumscribed rectangle is the egg shape parameter; The variety classification module builds an egg classification model, classifies the segmented egg image data according to the egg classification model, and optimizes the egg classification model.
7. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 5 are implemented.
8. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 5 are implemented.
Citation Information
Patent Citations
Spherical workpiece identification method and system based on contour edge number fitting and convex hull processing
CN112750107A
Vehicle model identification method based on YOLOv5 and MobileNetV2
CN113743233A
Diaphragm capsule pressure gauge pressure reading method and system based on deep learning
CN114119980A