A water level identification method based on SAM large model
Through the water level segmentation method based on the SAM large model and the water level segmentation model using the visual attention mechanism, prompt information is automatically generated, which solves the problems of low efficiency and insufficient universality of the traditional water level monitoring method, and achieves high efficiency and accuracy in complex environments. Automated water level monitoring.
Patent Information
- Application Number
- CN202410842014.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-27
- Publication Date
- 2024-09-06
- Estimated Expiration
- 2044-06-27
AI Technical Summary
Traditional water level monitoring methods are inefficient, especially in complex environments or severe weather conditions, and it is difficult to obtain accurate readings. Moreover, the existing water level monitoring methods based on SAM models are not very universal and require manual setting of prompt point information.
Design a water level segmentation method based on the SAM large model. The water level segmentation model uses the visual attention mechanism to automatically generate prompt information through the image encoder, prompt word encoder and mask decoder to achieve automatic segmentation and elevation fitting of the water body area. , adapt to complex water level scenarios.
It achieves fast and automated water level monitoring without installing a physical water gauge, improves detection accuracy and robustness, reduces labor costs, and adapts to different water level scenarios to improve generalization capabilities.
Smart Images

Figure CN118411675B
Abstract
Description
Technical Field
[0001] The invention relates to the technical field of image recognition, and in particular to a water level recognition method based on a SAM large model. Background Art
[0002] Water level monitoring is a key link in many fields such as water conservancy, energy and environmental monitoring, and is of great significance for flood control, water supply, power generation and ecological protection. Traditional water level monitoring methods, such as using physical water gauges and manual readings, are not only inefficient, but also easily interfered with in complex environments or bad weather conditions, resulting in inaccurate or impossible readings.
[0003] With the rapid development of computer vision and information technology, the application of image processing and pattern recognition technology in the field of water level monitoring has gradually increased. Among them, water level image recognition technology based on virtual water gauge has become a research hotspot in recent years. This technology simulates the working principle of the physical water gauge and combines deep learning algorithms to realize the automatic analysis of water area images and the extraction of water level information.
[0004] Patent application number CN202311219761.1 discloses a water level monitoring method based on the SAM image segmentation model, which inputs the water gauge correction image and prompt point information into the SAM image segmentation model to obtain the water gauge area segmentation result. Since this method is based on SAM model reasoning, it has good accuracy and stability, but it is limited to being applicable only to scenarios where physical water gauges are installed, and prompt point information needs to be manually set for different scenarios, so it is not very universal. Summary of the invention
[0005] In view of this, the present invention provides a water level recognition method based on the SAM large model, and designs a water level segmentation model based on the visual attention mechanism SAM (Segment Anything Model), which can effectively segment the water area in complex water level scenes and has strong robustness and detection accuracy.
[0006] The technical purpose of the present invention is achieved in this way:
[0007] The present invention provides a water level identification method based on a SAM large model, comprising the following steps:
[0008] S1: Collect water level monitoring images in various water environments, annotate the water level monitoring images, and construct a water level image dataset;
[0009] S2: Build a water level segmentation model based on the SAM network. The water level segmentation model includes an image encoder, a prompt word encoder, and a mask encoder.
[0010] S3: training the water level segmentation model using the water level image dataset to obtain a trained water level segmentation model;
[0011] S4: obtaining a water level image to be measured, extracting calibration points from the water level image to be measured, and constructing an elevation fitting equation according to the calibration points;
[0012] S5: input the water level image to be measured into the trained water level segmentation model to obtain a water level mask result;
[0013] S6: extracting the water level line position information from the water level mask result, inputting the water level line position information into the elevation fitting equation, and obtaining the current water level elevation.
[0014] On the basis of the above technical solution, preferably, the image encoder includes a linear projection module and an attention encoding module, the linear projection module is used to generate linear embedding features and position embedding information, and the attention encoding module is used to extract water level image features according to the linear embedding features and the position embedding information to obtain image embedding information;
[0015] The prompt word encoder includes a first encoding module, a second encoding module and a third encoding module, which are used to generate dense prompts, sparse prompts and image position encodings respectively;
[0016] The mask decoder consists of two decoding layers to output the water level mask results.
[0017] Based on the above technical solution, preferably, the processing process of the image encoder is:
[0018] Split the input image into N non-overlapping image blocks;
[0019] Perform linear mapping on each image block and project it into D-dimensional space to obtain linear embedding features;
[0020] Add position embedding information, which represents the spatial position of the image patch in the original input image;
[0021] The linear embedding features and position embedding information are input into the attention encoding module to extract the water level image features and obtain the image embedding information.
[0022] On the basis of the above technical solution, preferably, the attention encoding module includes a multi-head self-attention layer and a multi-layer perceptron layer, each of which is preceded by a normalization layer and a residual connection, and the mathematical expression of the attention encoding module is: ; ;
[0023] Among them, F MSA represents the multi-head self-attention layer, F MLP represents the multi-layer perceptron layer, FLN represents the normalization layer, i represents the index of the embedded feature, Z i Represents embedded features, which include linear embedded features and position embedded information.
[0024] On the basis of the above technical solution, preferably, the first encoding module includes a 3×3 convolutional layer, and the mathematical expression of its processing process is: ;
[0025] In the formula, C dense Indicates dense prompts, F CB_3 Indicates the use of 3×3 convolution processing, C IE Indicates image embedded information;
[0026] The second encoding module includes a convolutional layer, an average pooling layer, a linear layer, and an activation function. The mathematical expression of its processing process is: ;
[0027] In the formula, C sparse represents sparse hint, F AvgPool represents average pooling processing, F CB_5 represents 5×5 convolution processing, F Linear represents linear layer processing, F Relu Indicates the use of activation function Relu processing;
[0028] The third encoding module includes a 5×5 convolutional layer, and the mathematical expression of its processing process is: ;
[0029] In the formula, C pe Represents the image position encoding.
[0030] On the basis of the above technical solution, preferably, the mathematical expression of the processing process of the mask decoder is: ; ; ; ;
[0031] Among them, C prompt Indicates input prompt information, C new_pro and C new_IE Respectively represent the output prompt information and image embedding information, F SA represents the self-attention mechanism, F CA represents the cross attention mechanism, F MLP represents multi-layer perceptron processing, C mask is the mask result, F FCA Represents the fusion cross attention mechanism.
[0032] Based on the above technical solution, preferably, the loss function used in the training of the water level segmentation model is: ;
[0033] Among them, L FL represents the loss function, p t Represents the probability that the model predicts that the water level image data belongs to the positive class, α t is the weight of the water level image data, and γ is an adjustable parameter.
[0034] On the basis of the above technical solution, preferably, step S3 further includes:
[0035] During the training process, random data augmentation is performed on the water level image dataset, including generating image noise, random image cropping, and image flipping, to expand the water level image dataset.
[0036] On the basis of the above technical solution, preferably, step S4 includes:
[0037] S41: Acquire a water level image to be measured, and extract calibration points in the water level image to be measured, including pixel position x and water level elevation data y;
[0038] S42: constructing a polynomial-based elevation fitting equation according to the extracted calibration points, wherein the degree of the polynomial is determined according to the number of calibration points and the fitting requirements;
[0039] S43: Solving the polynomial coefficients using the least square method to obtain the elevation fitting equation;
[0040] Among them, the mathematical expression of the elevation fitting equation is: ;
[0041] Among them, y is the water level elevation, x is the vertical coordinate position of the pixel, and a 0 ,a 1 ,...,a n are the coefficients of the polynomial, and n is the degree of the polynomial.
[0042] Based on the above technical solution, preferably, step S6 includes:
[0043] S61: Record the area marked as water body in the water level mask result as water surface area, and calculate the area of all water surface areas;
[0044] S62: Setting a preset area threshold, screening the water surface area according to the preset area threshold, and screening out the water surface area whose area is smaller than the preset area threshold;
[0045] S63: searching for a water surface area with the largest area in the screened-out water surface area as the area to be detected;
[0046] S64: performing edge detection on the area to be detected, extracting edge information of the water surface area, and obtaining a water level line;
[0047] S65: Input the average pixel ordinate of the water level line into the elevation fitting equation to obtain the current water level elevation.
[0048] The method of the present invention has the following beneficial effects compared with the prior art:
[0049] (1) Compared with the traditional manual water level monitoring method, the present invention does not need to install a physical water gauge. It only needs to configure the site elevation calibration information and input the water level scene video to output the current screen water level value, thus realizing uninterrupted, rapid and automatic monitoring of the water level in any water area, reducing the labor cost during the monitoring process;
[0050] (2) The present invention uses the SAM water level segmentation model to solve the problem that traditional convolutional neural networks are easily affected by the scene lighting, background, water transparency and other environmental factors, making it difficult to accurately segment the water surface. The present invention uses the powerful learning ability of the SAM model based on the visual attention mechanism to effectively segment the water area in complex water level scenes, and has strong robustness and detection accuracy.
[0051] (3) The traditional SAM model requires manual input of prompts such as points or boxes for different scenes, and then the model generates segmentation results according to the scope of the prompts. The present invention, by reconstructing the prompt word encoder, can automatically generate prompts directly through image embedding information and segment the water surface area. There is no need to manually input prompt information for different scenes. It can adaptively segment water level scene images and improve the scene generalization ability. BRIEF DESCRIPTION OF THE DRAWINGS
[0052] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0053] Figure 1 is a flow chart of a method according to an embodiment of the present invention;
[0054] Figure 2 A structural diagram of a water level segmentation model according to an embodiment of the present invention;
[0055] Figure 3 is a structural diagram of an image encoder according to an embodiment of the present invention;
[0056] Figure 4 A structural diagram of a prompt word encoder according to an embodiment of the present invention;
[0057] Figure 5 4 is a structural diagram of a mask decoder according to an embodiment of the present invention. DETAILED DESCRIPTION
[0058] The following will be combined with the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0059] like Figure 1 As shown, the present invention provides a water level identification method based on the SAM large model, comprising the following steps:
[0060] S1: Collect water level monitoring images in various water environments, annotate the water level monitoring images, and construct a water level image dataset;
[0061] S2: Build a water level segmentation model based on the SAM network. The water level segmentation model includes an image encoder, a prompt word encoder, and a mask encoder.
[0062] S3: training the water level segmentation model using the water level image dataset to obtain a trained water level segmentation model;
[0063] S4: obtaining a water level image to be measured, extracting calibration points from the water level image to be measured, and constructing an elevation fitting equation according to the calibration points;
[0064] S5: input the water level image to be measured into the trained water level segmentation model to obtain a water level mask result;
[0065] S6: extracting the water level line position information from the water level mask result, inputting the water level line position information into the elevation fitting equation, and obtaining the current water level elevation.
[0066] Specifically, in one embodiment of the present invention, step S1 includes:
[0067] Collect water level monitoring images of various water environments. Ensure that the collected images cover a wide range of water environments, provide diversity and representativeness, and pay special attention to factors such as different transparency, weather and lighting conditions, so that the model can effectively cope with various changes in the actual environment during training.
[0068] First, select the collection location, such as lakes, rivers, reservoirs, etc., and consider the different transparency, which are divided into high transparency water bodies, medium transparency water bodies and low transparency water bodies. The collected images must also cover different weather conditions, such as sunny days, rainy days, cloudy days, foggy days, etc., as well as different lighting conditions, such as morning, noon, evening, night, etc.
[0069] When collecting water level images, cameras of different types and resolutions can be used to ensure that images are collected from different camera angles.
[0070] After collecting the original water level image, generate corresponding mask label images for the water body and background areas in the image, and construct a high-quality water level segmentation image dataset to provide a basis for subsequent model training. Specifically, when labeling, manually label the water body area and the background area. Save the labeling result as a mask image in the form of a binary image, where the water body area is labeled with one color (such as white, with a value of 255). The background area is labeled with another color (such as black, with a value of 0). Pair the original water level image with its corresponding mask label image to construct a water level image dataset.
[0071] For details, please refer to Figure 2 In one embodiment of the present invention, a water level segmentation model based on a SAM network is constructed, which includes an image encoder, a prompt word encoder, and a mask encoder; the water level segmentation model is used to segment the water body and background parts in a water level monitoring image.
[0072] SAM is currently the most advanced basic model for image segmentation. Image segmentation is usually performed through prompt words (such as points or boxes). The present invention reconstructs the prompt word encoder so that it does not need to set prompt information for different scenes, and adaptively performs water level scene image segmentation.
[0073] Specifically, the image encoder includes a linear projection module and an attention encoding module, the linear projection module is used to generate linear embedding features and position embedding information, and the attention encoding module is used to extract water level image features according to the linear embedding features and position embedding information to obtain image embedding information;
[0074] The prompt word encoder includes a first encoding module, a second encoding module and a third encoding module, which are used to generate dense prompts, sparse prompts and image position encodings respectively;
[0075] The mask decoder consists of two decoding layers to output the water level mask results.
[0076] like Figure 3 As shown, in this embodiment, the processing process of the image encoder is:
[0077] Split the input image into N non-overlapping image blocks;
[0078] Perform linear mapping on each image block and project it into D-dimensional space to obtain linear embedding features;
[0079] Add position embedding information, which represents the spatial position of the image patch in the original input image;
[0080] The linear embedding features and position embedding information are input into the attention encoding module to extract the water level image features and obtain the image embedding information.
[0081] In a specific example:
[0082] The image encoder processes the original water level image and converts it into a continuous, low-dimensional vector representation, namely image embedding information. The purpose is to convert high-dimensional image data into lower-dimensional data that is easier to process while retaining as much original image information as possible. The image encoder uses the Vision Transformer (VIT) model as the framework. First, the image Converted into sequence data, that is, divided into N non-overlapping image blocks of size (P×P), where , H and W are the image height and width respectively, and C is the number of image color channels; then, for each image block X p Perform linear mapping and project it into D-dimensional space to obtain linear embedding feature Z p , convert each image block into a D-dimensional vector, add position information to each image block, that is, its spatial position in the original input image. The purpose of position embedding is to retain the spatial relationship of the image block in the original image. A fixed or trainable position encoding vector can be used to add the linear embedding feature to the position embedding information to form an embedding feature with position information. Finally, the linear embedding feature and the position embedding information are input into the attention encoding module to extract the water level image features and obtain the image embedding information.
[0083] Specifically, the attention encoding module includes a multi-head self-attention (MSA) layer and a multi-layer perceptron (MLP) layer. Each multi-head self-attention layer and multi-layer perceptron layer is preceded by a normalization layer and is provided with a residual connection. The mathematical expression is as follows: ; ;
[0084] Among them, F MSA represents the multi-head self-attention layer, F MLP represents the multi-layer perceptron layer, F LN represents the normalization layer, i represents the index of the embedded feature, Z i Represents embedded features, which include linear embedded features and position embedded information.
[0085] In this embodiment, the self-attention mechanism can capture the global relationship between different image blocks. The multi-head mechanism allows the model to learn attention in different subspaces, increasing the expressive power of the model. The multi-layer perceptron contains two fully connected layers and an activation function for further nonlinear transformation. There is a normalization layer (Layer Normalization) before each multi-head self-attention layer and multi-layer perceptron layer. Residual connections are used to alleviate the gradient vanishing problem in deep networks.
[0086] In this embodiment, the function of the image encoder is to convert the original high-dimensional image data into a low-dimensional vector representation, which is achieved by segmenting the image, linear mapping, adding position embedding, and attention encoding modules to extract features. Among them, the attention encoding module uses multi-head self-attention and multi-layer perception mechanisms to effectively capture the global relationship and nonlinear features between image blocks. Through these steps, the final image embedding information (Image Embedding) contains a lot of information about the original image, and is presented in a low-dimensional form that is easier to process and analyze, providing efficient and reliable feature representation for subsequent water level monitoring and deep learning tasks.
[0087] Specifically, the purpose of the cue word encoder is to embed the input cue words (such as points or boxes) into the model so that the model can understand and perform image segmentation based on these cues. In SAM, the cue word encoder and the image encoder are separated, and their outputs are integrated into the mask decoder. The mask decoder uses this integrated information to predict the segmentation mask, that is, the area in the image that the model should segment based on the cue words. The present invention reconstructs the cue word encoder part so that it can automatically generate cue word vector information based on the water level image embedding information.
[0088] See also Figure 4 In one embodiment of the present invention, the prompt word encoder is divided into three encoding modules, corresponding to three processing paths, and dense prompts, sparse prompts and image position encodings can be generated according to the three processing paths respectively.
[0089] Specifically, the first encoding module includes a 3×3 convolutional layer, and the mathematical expression of its processing process is: ;
[0090] In the formula, C dense Indicates dense prompts, F CB_3 Indicates the use of 3×3 convolution processing, C IE Indicates image embedded information;
[0091] This is a dense cue processing path that can capture dense feature information on local details and mainly captures local details of the image.
[0092] The second encoding module includes a convolutional layer, an average pooling layer, a linear layer, and an activation function. The mathematical expression of its processing process is: ;
[0093] In the formula, C sparse represents sparse hint, F AvgPool represents average pooling processing, F CB_5 represents 5×5 convolution processing, F Linear represents linear layer processing, F Relu Indicates the use of activation function Relu processing;
[0094] This is the sparse hint processing path, which extracts features through convolution, average pooling, linear layers, and activation functions, reduces the amount of computation in the spatial dimension while retaining important features and enhancing the robustness of the model.
[0095] The third encoding module includes a 5×5 convolutional layer, and the mathematical expression of its processing process is: ;
[0096] In the formula, C pe Represents the image position encoding.
[0097] This is the image position encoding processing path, which captures spatial correlations at different scales through convolution.
[0098] Specifically, the sparse hint processing path first uses a convolutional layer to extract local features; then, it uses average pooling to reduce the spatial dimension of the data, reduce the amount of calculation and parameters, and extract more important features, making the model more robust to small changes in the input data; then, a linear layer is used to convert the features extracted by the previous layer into the dimensions required for the final output; finally, a ReLU activation layer is used to increase the nonlinearity of the network and improve the expressiveness of the model. The sparse hint and image position encoding paths use 3×3 and 5×5 convolutions, respectively, to capture spatial correlations at different scales without changing the input and output dimensions.
[0099] Finally, the cue word encoder combines these cue feature information to provide efficient feature representation for the mask decoder to assist it in segmentation mask prediction.
[0100] In one embodiment of the present invention, the mask decoder is responsible for mapping the image embedding and a set of hint information to an output mask.
[0101] like Figure 5 As shown in Figure 1, the mask decoder contains two decoders. The processing process takes into account the cross-relationship between the prompt information and the image embedding to ensure full fusion of information. The processing process is as follows:
[0102] First, the prompt information is self-attentioned to capture the relationship between the prompt information. This step helps the model understand the internal structure between the prompt information. Next, the prompt information is cross-attentioned with the image embedding to capture the dynamic relationship between the prompt information and the image embedding information. Then, each prompt information is further enhanced by updating it through a multi-layer perceptron to make it more suitable for subsequent cross-attention operations. The image embedding and the prompt information are cross-attentioned to further combine this information so that the image embedding information contains the prompt information. Its mathematical expression is as follows: ; ; ;
[0103] Among them, C prompt Indicates input prompt information, C new_pro and C new_IE Respectively represent the output prompt information and image embedding information, F SA represents the self-attention mechanism, F CA represents the cross attention mechanism, F MLP Represents multi-layer perceptron processing.
[0104] In this embodiment, two decoders are operated alternately to achieve full integration of the prompt information and the image embedding information. After the two decoders are decoded, sufficient prompt information is integrated into the image embedding information to facilitate the subsequent mask prediction.
[0105] After two decoding layer operations, the fusion cross attention mechanism is used to capture the dynamic relationship between the prompt information and the image embedding information to obtain the final water level image mask result, whose mathematical expression is as follows: ;
[0106] Among them C mask is the mask result, F FCA Represents the fusion cross attention mechanism.
[0107] In this embodiment, the mask decoder can accurately convert the embedded information and prompt information of the input image into the output mask, effectively improving the accuracy and robustness of image segmentation.
[0108] In one embodiment of the present invention, step S3 further includes:
[0109] During the training process, random data augmentation is performed on the water level image dataset, including generating image noise, random image cropping and image flipping, to expand the water level image dataset, which can effectively prevent model overfitting.
[0110] Generating image noise can make the model more robust and better generalize to different noise environments. The specific method is to add additive Gaussian noise, whose probability density is equal to the probability density function of the Gaussian distribution. The noise intensity is controlled by the standard deviation σ, which is uniformly distributed in the range of [1,30] with a minimum interval of ,This approach can increase the diversity of the data and enable the model to adapt to different noise levels.
[0111] Random cropping enables the model to adapt to different image perspectives and focuses on local features, thereby enhancing the robustness of the model. By randomly selecting a sub-region from the water level image dataset for cropping and using these cropped images for training, the diversity of data samples can be effectively increased.
[0112] Random flipping means that the image is flipped horizontally or vertically according to a certain probability. Its purpose is to increase the diversity of data samples and make the model insensitive to image reversal, thereby improving the robustness of the model.
[0113] The above data augmentation methods can be randomly applied during the training process, so that the original data set is effectively expanded and the diversity is enhanced.
[0114] In this embodiment, the training of the water level segmentation model is fine-tuning training, which means using the water level segmentation dataset to perform end-to-end fine-tuning training on the pre-trained model, so that the prompt encoder learns the distribution characteristics of the water surface and further improves the segmentation effect for the water level scene.
[0115] Specifically, during the training process, Focal loss is used to calculate the difference between the label image and the predicted image. As a loss function improved on the basis of cross entropy, Focal loss can better handle the problem of class imbalance and focus on difficult samples compared to cross entropy loss. Due to the diversity and complexity of the content of water level images, Focal loss can make the model pay more attention to pixel areas with blurred boundaries and unclear features, such as high water transparency and residual water stains on the bank slope, so as to complete the water surface segmentation task more accurately. Its mathematical expression is as follows: ;
[0116] Among them, L FL represents the loss function, p t Represents the probability that the model predicts that the water level image data belongs to the positive class, α t is the weight of the water level image data, and γ is an adjustable parameter.
[0117] When the sample is easy to classify, that is, p t Near 1 o'clock, will be close to 0, thereby reducing the loss value of easy-to-classify samples; while for difficult samples, that is, p t When smaller, Close to 1, keep the original weight, and pay more attention to difficult samples.
[0118] Through the combination of the above augmentation and loss functions, the water level segmentation model can better adapt to different data samples and improve the performance of the model in actual scenarios.
[0119] Specifically, in one embodiment of the present invention, step S4 includes:
[0120] S41: Obtain the water level image to be measured, and extract the calibration points in the water level image to be measured. The calibration points refer to specific positions preset or identified in the image, and the actual water level elevation data of these positions are known. The calibration points include pixel position x and water level elevation data y; the pixel position x represents the ordinate position in the image; the water level elevation y is the actual water level height data corresponding to the pixel position.
[0121] S42: constructing a polynomial-based elevation fitting equation according to the extracted calibration points, wherein the degree of the polynomial is determined according to the number of calibration points and the fitting requirements;
[0122] The degree n of the polynomial refers to the highest power of the polynomial. The choice of fitting degree should be determined according to the number of calibration points and the accuracy requirements of fitting. The more calibration points there are, the higher the degree of polynomial can be selected for fitting, so as to obtain better fitting accuracy.
[0123] Based on the extracted calibration points (x, y), the elevation fitting equation can be constructed.
[0124] The mathematical expression of the elevation fitting equation is: ;
[0125] Among them, y is the water level elevation, x is the vertical coordinate position of the pixel, and a 0 ,a 1 ,...,a n are the coefficients of the polynomial, and n is the degree of the polynomial.
[0126] S43: Solving the polynomial coefficients using the least square method to obtain the elevation fitting equation;
[0127] The least squares method is to obtain the best fitting polynomial by minimizing the sum of squares of the error term. The error term refers to the difference between the actual value y and the fitted value.
[0128] The error function is defined as the sum of the squares of all errors. By the least squares method, the polynomial coefficient a is adjusted. 0 ,a 1 ,...,a n , so that the error function is minimized. Finally, the optimal polynomial coefficients are obtained.
[0129] Specifically, in one embodiment of the present invention, step S5 includes:
[0130] To prepare the water level image to be measured, necessary preprocessing operations may be performed on the water level image to be measured, such as resizing, normalization, and color space conversion, so that the image meets the input requirements of the model.
[0131] The preprocessed water level image to be measured is input into the trained water level segmentation model, and the model is inferred to obtain the water level segmentation mask of the water level area. The mask is a binary image, that is, the water level area and the background area are represented by different pixel values.
[0132] Specifically, in one embodiment of the present invention, step S6 includes:
[0133] S61: Record the area marked as water body in the water level mask result as water surface area, and calculate the area of all water surface areas;
[0134] The area marked as 1 in the water level mask result (i.e., binary mask image) is considered as the water surface area. Use the connected component labeling algorithm to identify all independent water surface areas. For each connected water surface area, calculate the number of its pixels as the area.
[0135] S62: Setting a preset area threshold, screening the water surface area according to the preset area threshold, and screening out the water surface area whose area is smaller than the preset area threshold;
[0136] Determine an area threshold, assuming it is threshold_area, to filter out water surface areas with smaller areas. Water surface areas with an area greater than or equal to threshold_area are retained, and the rest are filtered out.
[0137] S63: searching for a water surface area with the largest area in the screened-out water surface area as the area to be detected;
[0138] S64: performing edge detection on the area to be detected, extracting edge information of the water surface area, and obtaining a water level line;
[0139] The Canny edge detection algorithm is used to perform edge detection on the maximum water surface area mask and extract the edge of the water surface area to obtain the water level line.
[0140] S65: Input the average pixel ordinate of the water level line of the target area into the elevation fitting equation to obtain the current water level elevation.
[0141] The SAM network designed by the present invention includes an image encoder, a prompt word encoder and a mask decoder, which can more accurately segment the water level area in the image and improve the recognition accuracy of the water level in the water level monitoring image under different environments. By extracting calibration points and constructing an elevation fitting equation, the pixel position information of the image can be accurately matched with the actual elevation data, thereby improving the accuracy of the water level measurement data. By collecting and annotating water level monitoring images under various water environments, a water level image data set with wide coverage is constructed, so that the trained model can adapt to various water environments and improve versatility. Random data augmentation technology is used in the model training process, including image noise, random image cropping and image flipping, etc., to further enhance the generalization ability of the model and its adaptability to complex environments.
[0142] In the water level segmentation model provided by the present invention, the image encoder combining linear projection and attention encoding modules can more effectively extract and represent the water level information in the image by introducing position embedding information and multi-head self-attention mechanism. The prompt word encoder is composed of multiple encoding modules (including dense prompts, sparse prompts and image position encoding), which can encode contextual information more comprehensively and improve the model's understanding and processing capabilities of water level images. Through the double-layer decoder structure and cross-attention mechanism, the water level mask can be restored more accurately to ensure the accuracy of the segmentation result.
[0143] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the protection scope of the present invention.
Claims
1. A water level identification method based on the SAM large model, characterized in that: The steps include: S1: Collect water level monitoring images in various water environments, annotate the water level monitoring images, and construct a water level image dataset; S2: Build a water level segmentation model based on the SAM network. The water level segmentation model includes an image encoder, a prompt word encoder, and a mask encoder. The image encoder includes a linear projection module and an attention encoding module, the linear projection module is used to generate linear embedding features and position embedding information, and the attention encoding module is used to extract water level image features according to the linear embedding features and position embedding information to obtain image embedding information; The prompt word encoder includes a first encoding module, a second encoding module and a third encoding module, which are used to generate dense prompts, sparse prompts and image position encodings respectively; The mask decoder includes two decoding layers to output the water level mask results; The first encoding module includes a 3×3 convolutional layer, and the mathematical expression of its processing process is: ; In the formula, C dense Indicates dense prompts, F CB_3 Indicates the use of 3×3 convolution processing, C IE Indicates image embedded information; The second encoding module includes a convolutional layer, an average pooling layer, a linear layer, and an activation function. The mathematical expression of its processing process is: ; In the formula, C sparse represents sparse hint, F AvgPool represents average pooling processing, F CB_5 represents 5×5 convolution processing, F Linear represents linear layer processing, F Relu Indicates the use of activation function Relu processing; The third encoding module includes a 5×5 convolutional layer, and the mathematical expression of its processing process is: ; In the formula, C pe Represents image position encoding; The mathematical expression of the mask decoder processing is: ; ; ; Among them, C prompt Indicates input prompt information, C new_pro and C new_IE Respectively represent the output prompt information and image embedding information, F SA represents the self-attention mechanism, F CA represents the cross attention mechanism, F MLP represents multi-layer perceptron processing, C mask is the mask result, F FCA Indicates the fusion cross-attention mechanism; S3: training the water level segmentation model using the water level image dataset to obtain a trained water level segmentation model; S4: obtaining a water level image to be measured, extracting calibration points from the water level image to be measured, and constructing an elevation fitting equation according to the calibration points; S5: input the water level image to be measured into the trained water level segmentation model to obtain a water level mask result; S6: extracting the water level line position information from the water level mask result, inputting the water level line position information into the elevation fitting equation, and obtaining the current water level elevation.
2. A water level identification method based on the SAM large model as claimed in claim 1, characterized in that: The processing of the image encoder is: Split the input image into N non-overlapping image blocks; Perform linear mapping on each image block and project it into D-dimensional space to obtain linear embedding features; Add position embedding information, which represents the spatial position of the image patch in the original input image; The linear embedding features and position embedding information are input into the attention encoding module to extract the water level image features and obtain the image embedding information.
3. A water level identification method based on the SAM large model as claimed in claim 1, characterized in that: The attention encoding module includes a multi-head self-attention layer and a multi-layer perceptron layer. Each multi-head self-attention layer and multi-layer perceptron layer is preceded by a normalization layer and is provided with a residual connection. The mathematical expression of the attention encoding module is: ; ; Among them, F MSA represents the multi-head self-attention layer, F MLP represents the multi-layer perceptron layer, F LN represents the normalization layer, i represents the index of the embedded feature, Z i Represents embedded features, which include linear embedded features and position embedded information.
4. A water level identification method based on the SAM large model as claimed in claim 1, characterized in that: The loss function used in training the water level segmentation model is: ; Among them, L FL represents the loss function, p t Represents the probability that the model predicts that the water level image data belongs to the positive class, α t is the weight of the water level image data, and γ is an adjustable parameter.
5. A water level identification method based on the SAM large model as claimed in claim 1, characterized in that: Step S3 also includes: During the training process, random data augmentation is performed on the water level image dataset, including generating image noise, random image cropping, and image flipping, to expand the water level image dataset.
6. A water level identification method based on the SAM large model as claimed in claim 1, characterized in that: Step S4 includes: S41: Acquire a water level image to be measured, and extract calibration points in the water level image to be measured, including pixel position x and water level elevation data y; S42: constructing a polynomial-based elevation fitting equation according to the extracted calibration points, wherein the degree of the polynomial is determined according to the number of calibration points and the fitting requirements; S43: Solving the polynomial coefficients using the least square method to obtain the elevation fitting equation; Among them, the mathematical expression of the elevation fitting equation is: ; Among them, y is the water level elevation, x is the pixel ordinate position, a0, a1, ..., a n are the coefficients of the polynomial, and n is the degree of the polynomial.
7. A water level identification method based on the SAM large model as claimed in claim 1, characterized in that: Step S6 includes: S61: Record the area marked as water body in the water level mask result as water surface area, and calculate the area of all water surface areas; S62: Setting a preset area threshold, screening the water surface area according to the preset area threshold, and screening out the water surface area whose area is smaller than the preset area threshold; S63: searching for a water surface area with the largest area in the screened-out water surface area as the area to be detected; S64: performing edge detection on the area to be detected, extracting edge information of the water surface area, and obtaining a water level line; S65: Input the average pixel ordinate of the water level line into the elevation fitting equation to obtain the current water level elevation.
Citation Information
Cited By
Water level identification method and system based on pre-trained SAM large model
CN119380284A
A water level recognition method and system based on pre-trained SAM large model
CN119380284B
