Image segmentation method and device, equipment and storage medium
By compressing binary masks and using a binary search algorithm, the method addresses inefficiencies in traditional image segmentation, achieving faster and more accurate results for e-commerce image processing.
Patent Information
- Application Number
- CN202510483664.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-17
- Publication Date
- 2025-07-15
AI Technical Summary
When applied to the browser, the traditional image segmentation method is inefficient and insufficiently accurate, which cannot meet the batch processing needs of e-commerce scenarios, and the segmentation edges are rough, making it difficult to meet the refined needs of product images.
By using the preset image segmentation model to generate compressed encoding of the target binary mask, and based on binary search algorithm and dynamic rendering technology, the image area can be quickly positioned and segmented, reducing the data transmission amount and calculation complexity.
It improves the efficiency and accuracy of image segmentation, reduces network latency and time complexity, and supports real-time batch processing of e-commerce scenarios.
Smart Images

Figure CN120318253A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of automatic segmentation technology, and in particular to an image segmentation method, device, equipment and storage medium. Background Art
[0002] Traditional image segmentation relies on tools or client software, which requires manual operation by users and is costly, making it difficult to adapt to the needs of efficient batch processing. Large models such as the SAM model (Segment Anything Model) have improved segmentation accuracy, but due to the model size of more than 1GB and high computational complexity, their application on the browser side is limited and real-time processing cannot be achieved.
[0003] In e-commerce scenarios, merchants need to process hundreds of product images every day, such as changing models and backgrounds, which places extremely high demands on the speed, accuracy, and interactive experience of image segmentation. Loading the SAM model on the browser side requires parsing the complex model structure, which consumes a lot of memory and computing resources. The segmentation takes several seconds and cannot support the batch processing requirements of e-commerce scenarios. In addition, the segmentation edges are rough and it is difficult to meet the refinement requirements of product images. After the server completes the segmentation, it directly transmits the original mask data to the front end, which increases the server load.
[0004] To sum up, how to improve the efficiency and accuracy of image segmentation is a technical problem that needs to be solved urgently. Summary of the invention
[0005] In view of this, the purpose of the present invention is to provide an image segmentation method, device, equipment and storage medium, which can improve the efficiency and accuracy of image segmentation. The specific scheme is as follows:
[0006] In a first aspect, the present application provides an image segmentation method, comprising:
[0007] Determine a target binary mask corresponding to a target image using a preset image segmentation model, and generate a target compression code corresponding to the target binary mask based on a preset data structure;
[0008] Generate a target one-dimensional array corresponding to the target compression code based on a preset typed array condition, and determine a target two-dimensional array corresponding to the target one-dimensional array based on a preset segmentation method;
[0009] Determine the target one-dimensional position corresponding to the current mouse position, and determine the target matching index based on a preset binary search algorithm, the target compression code and the target one-dimensional position, so as to determine the target segmentation area based on the target matching index and the target two-dimensional array, and segment the target image based on a preset dynamic rendering technology and the target segmentation area.
[0010] Optionally, determining a target binary mask corresponding to the target image by using a preset image segmentation model and generating a target compression code corresponding to the target binary mask based on a preset data structure includes:
[0011] Determining the target binary mask corresponding to the target image by using the preset image segmentation model based on a preset edge closing strategy and a preset post-processing algorithm;
[0012] Scanning the target binary mask based on a preset scanning condition and determining a target quantity corresponding to consecutive and identical binary masks in the target binary mask according to the corresponding scanning result;
[0013] Generating a target coding sequence corresponding to the target binary mask according to the target quantity and generating a corresponding target coding cumulative sum array based on the target coding sequence, so as to generate the target compression code based on the target size corresponding to the target image, the target coding sequence, the target coding cumulative sum array and the preset data structure.
[0014] Optionally, generating a target one-dimensional array corresponding to the target compression code based on a preset typed array condition includes:
[0015] Creating an initial one-dimensional array based on the target size corresponding to the target image and the preset typed array condition and initializing a target index and a target state corresponding to the initial one-dimensional array;
[0016] Determining a current target code in the target coding sequence according to a preset order condition, determining a first target loop number corresponding to the current target code, and determining each target element in the initial one-dimensional array based on the first target loop number and the current target state;
[0017] Determining the target index corresponding to each target element based on a preset increment rule, updating the target state based on a preset state switching rule, and determining the next target code as the current target code, and jumping to the step of determining the first target loop number corresponding to the current target code until the target one-dimensional array corresponding to the target coding sequence is determined based on each target element.
[0018] Optionally, determining a target two-dimensional array corresponding to the target one-dimensional array based on a preset segmentation method includes:
[0019] Creating an initial two-dimensional array corresponding to the target one-dimensional array and determining a second target loop number based on the target width corresponding to the target image;
[0020] Determine the target segmentation interval corresponding to the target one-dimensional array based on a preset column splitting method and the target height corresponding to the target image, and determine each target element in the initial two-dimensional array based on a preset intercepting method, the target segmentation interval, and the target one-dimensional array, so as to determine the target two-dimensional array corresponding to the target one-dimensional array based on the second target loop count and each target element.
[0021] Optionally, the determining the target one-dimensional position corresponding to the current mouse position includes:
[0022] Determine the current mouse position based on a preset mouse event listening and the target size corresponding to the target image, and determine the current display size of the target image;
[0023] Determine a target scaling ratio based on the display size and the target size, and determine the target one-dimensional position corresponding to the current mouse position based on the target scaling ratio, the target size, and the current mouse position.
[0024] Optionally, the determining the target matching index based on a preset binary search algorithm, the target compression encoding, and the target one-dimensional position includes:
[0025] Determine a first target pointer and a second target pointer corresponding to the target coding cumulative sum array in the target compression encoding; the first target pointer points to the starting element of the target coding cumulative sum array, and the second target pointer points to the ending element of the target coding cumulative sum array;
[0026] If it is determined that the first initial pointer is not greater than the second initial pointer, determine an intermediate index based on the first initial pointer and the second initial pointer, and determine a corresponding target intermediate element in the target coding cumulative sum array based on the intermediate index;
[0027] If it is determined that the target intermediate element is less than the target one-dimensional position, update the first target pointer based on a first preset pointer update condition;
[0028] If it is determined that the target intermediate element is not less than the target one-dimensional position, update the second target pointer based on a second preset pointer update condition;
[0029] Jump to the step of determining the intermediate index based on the first initial pointer and the second initial pointer until the first initial pointer is greater than the second initial pointer, and determine the target matching index based on the first initial pointer.
[0030] Optionally, determining a target segmentation region based on the target matching index and the target two-dimensional array, and segmenting the target image based on a preset dynamic rendering technique and the target segmentation region includes:
[0031] Determining corresponding target segmentation pixels in the target one-dimensional array based on the target matching index, and determining the corresponding target segmentation region in the target two-dimensional array based on the target segmentation pixels and the current target viewport of the target image;
[0032] Segmenting the target image based on a preset shader program, the preset dynamic rendering technique, and the target segmentation region, and highlighting the target segmentation region in the target image.
[0033] In a second aspect, the present application provides an image segmentation device, including:
[0034] A target compression encoding generation module, configured to determine a target binary mask corresponding to a target image by using a preset image segmentation model, and generate a target compression encoding corresponding to the target binary mask based on a preset data structure;
[0035] A target two-dimensional array determination module, configured to generate a target one-dimensional array corresponding to the target compression encoding based on a preset typed array condition, and determine a target two-dimensional array corresponding to the target one-dimensional array based on a preset segmentation method;
[0036] A target image segmentation module, configured to determine a target one-dimensional position corresponding to the current mouse position, and determine a target matching index based on a preset binary search algorithm, the target compression encoding, and the target one-dimensional position, so as to determine a target segmentation region based on the target matching index and the target two-dimensional array, and segment the target image based on a preset dynamic rendering technique and the target segmentation region.
[0037] In a third aspect, the present application provides an electronic device, including:
[0038] A memory, configured to store a computer program;
[0039] A processor, configured to execute the computer program to implement the foregoing image segmentation method.
[0040] In a fourth aspect, the present application provides a computer-readable storage medium, configured to store a computer program; wherein, when the computer program is executed by a processor, the foregoing image segmentation method is implemented.
[0041] In this application, first, a preset image segmentation model is used to determine the target binary mask corresponding to the target image, and a target compression code corresponding to the target binary mask is generated based on a preset data structure; then, a target one-dimensional array corresponding to the target compression code is generated based on a preset typed array condition, and a target two-dimensional array corresponding to the target one-dimensional array is determined based on a preset segmentation method; finally, a target one-dimensional position corresponding to the current mouse position is determined, and a target matching index is determined based on a preset binary search algorithm, the target compression code, and the target one-dimensional position, so as to determine a target segmentation area based on the target matching index and the target two-dimensional array, and segment the target image based on a preset dynamic rendering technique and the target segmentation area. As can be seen from the above, in this application, the target binary mask corresponding to the target image is compressed into a target compression code, and a corresponding target one-dimensional array is generated to determine the target two-dimensional array corresponding to the target one-dimensional array. The target matching index is determined according to the current mouse position, the preset binary search algorithm, and the target compression code, so as to determine the target segmentation area according to the target matching index and the target two-dimensional array, and segment the target image based on the preset dynamic rendering technique and the target segmentation area. In this way, this application compresses the target binary mask of the target image, reduces the data transmission volume, reduces the network latency, and directly reduces the time complexity by positioning the target segmentation area based on the binary search algorithm, improving the image segmentation response speed. In this way, this application can improve the efficiency and accuracy of image segmentation. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, the following drawings are only the embodiments of the present invention. For those of ordinary skill in the art, other drawings can be obtained according to the provided drawings without creative efforts.
[0043] Figure 1 It is a flowchart of an image segmentation method provided by this application;
[0044] Figure 2 It is a specific image segmentation schematic diagram provided by this application;
[0045] Figure 3 It is a flowchart of a specific image segmentation method provided by this application;
[0046] Figure 4 It is a schematic structural diagram of an image segmentation device provided by this application;
[0047] Figure 5 It is a structural diagram of an electronic device provided by this application. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0048] The following will clearly and completely describe the technical solutions in the embodiments of the present invention with reference to the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all of the embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts fall within the scope of protection of the present invention.
[0049] In the e-commerce scenario, merchants need to process hundreds of product images every day, such as replacing models, backgrounds, etc. This poses extremely high requirements for the speed, accuracy, and interaction experience of image segmentation. Loading the SAM model on the browser side requires parsing complex model structures, consuming a large amount of memory and computing resources. The segmentation takes up to several seconds, which cannot support the batch processing requirements of the e-commerce scenario, and the segmentation edges are rough, making it difficult to meet the refined requirements of product images. After the server-side completes the segmentation, directly transmitting the original mask data to the front-end exacerbates the server load. Therefore, this application provides an image segmentation solution that can improve the efficiency and accuracy of image segmentation.
[0050] See Figure 1 As shown, an embodiment of the present invention discloses an image segmentation method, which may include:
[0051] Step S11: Use a preset image segmentation model to determine the target binary mask corresponding to the target image, and generate a target compression code corresponding to the target binary mask based on a preset data structure.
[0052] In this embodiment, the high-performance hardware of the server can be utilized to run the complete segmentation model to ensure the accuracy of image segmentation. The steps of determining the target binary mask corresponding to the target image by using the preset image segmentation model and generating the target compression encoding corresponding to the target binary mask based on the preset data structure may include: First, use the preset image segmentation model to determine the target binary mask corresponding to the target image based on the preset edge closing strategy and the preset post-processing algorithm; then scan the target binary mask based on the preset scanning conditions, and determine the target quantity corresponding to the consecutive and identical binary masks in the target binary mask according to the corresponding scanning results; finally, generate the target encoding sequence corresponding to the target binary mask according to the target quantity, and generate the corresponding target encoding cumulative sum array based on the target encoding sequence, so as to generate the target compression encoding based on the target size corresponding to the target image, the target encoding sequence, the target encoding cumulative sum array and the preset data structure. Specifically, for the characteristics of commodity pictures, such as clear main body and single background, the SAM model can be lightly fine-tuned on the server side. In a specific implementation, to balance the image segmentation accuracy and the image segmentation speed, the resolution of the input image can be set to 1024px×1024px; to avoid fragmented regions during image segmentation, the edges can be forced to close when outputting the target binary mask of the target image based on the edge closing strategy, and at the same time, the edges can be smoothed through the post-processing algorithm to ensure that the clothing contour has no jaggedness. In this embodiment, the target binary mask corresponding to the target image can be compressed by RLE encoding (Run-Length Encoding). In a specific implementation, first scan the target binary mask row by row, and then determine the number of consecutive identical binary masks, that is, record the length of the interval with the same consecutive pixel values, to obtain the RLE encoding sequence corresponding to the target binary mask. For example, the binary mask [0,0,0,0,0,1,1....] can be compressed into [5,0,100,1,3,0], and the volume is reduced by more than 90%. Then, generate the cumulative sum array of the RLE encoding on the server side. The encoding cumulative sum array records the cumulative length of the consecutive foreground regions in the mask data. For example, in [0,100,250,...], 0 represents the starting position; 100 means that from the 0th bit to the 99th bit are all the foreground of the image; 250 means that from the 100th bit to the 249th bit is the background of the image, and the 250th bit starts again as the foreground of the image. It should be noted that the data structure of the target compression encoding can be specifically represented as:
[0053] interface SegmentationData {
[0054] size: [number, number]; / / Image size [h,w]
[0055] counts: number[]; / / RLE encoded sequence
[0056] cums: number[]; / / Cumulative sum array (pre - calculated)
[0057] }
[0058] In this embodiment, 0 represents the background area of the image, and 1 represents the foreground object of the image, such as the main body of the commodity. In a specific implementation, in the RLE encoding, counts = [5, 100, 3] means that the first 5 pixels are the background of the image; the next 100 pixels are the foreground of the image; and the last 3 pixels are the background of the image. After obtaining the target compression encoding corresponding to the target binary mask according to the above data structure, the target compression encoding can be sent to the front - end through the HTTP protocol (HyperText Transfer Protocol) or the WebSocket protocol. It should be noted that in this embodiment, the SAM model can be replaced with other segmentation models, and the RLE encoding rule can be adjusted synchronously. And in this embodiment, a chained pointer can be added on the basis of the RLE encoding to mark the boundaries of different segmentation regions, so as to further compress the fragmented regions. For example: , the compression rate is increased to more than 95%.
[0059] Step S12: Generate a target one - dimensional array corresponding to the target compression encoding based on a preset typed - array condition, and determine a target two - dimensional array corresponding to the target one - dimensional array based on a preset segmentation method.
[0060] It can be understood that in order to restore the target compression encoding of the target image into an image mask format that can be understood and operated by the front end, generating the target one-dimensional array corresponding to the target compression encoding based on the preset typed array condition may include: First, create an initial one-dimensional array based on the target size corresponding to the target image and the preset typed array condition, and initialize the target index and target state corresponding to the initial one-dimensional array; then determine the current target encoding in the target encoding sequence according to the preset order condition, and determine the first target loop count corresponding to the current target encoding, and determine each target element in the initial one-dimensional array based on the first target loop count and the current target state; then determine the target index corresponding to each target element based on the preset increment rule, update the target state based on the preset state switching rule, and determine the next target encoding as the current target encoding, and jump to the step of determining the first target loop count corresponding to the current target encoding until the target one-dimensional array corresponding to the target encoding sequence is determined based on each target element. In this embodiment, the target compression encoding sent by the server can be processed to generate a Uint8Array one-dimensional array, that is, 0 and 1 can be used to represent the background and foreground of the image respectively. Specifically, the parity variable can be used to switch between 0 and 1 to identify the start of a continuous area: the initial value is 0, indicating the start of the background area of the image; when the count loop ends, it switches to 1, indicating the start of the foreground area of the image. For example, when counts = [5, 100], the code will first write 5 zeros, indicating that the current is the background of the image, and then switch to 1 and write 100 ones, indicating that the current is the foreground of the image. At the same time, using the parity variable can avoid confusion between the foreground and background. If the parity is not switched, continuous areas may be incorrectly merged. For example, if only zeros are written all the time, it is impossible to distinguish the continuous areas of the background and foreground in the image. The code for generating the target one-dimensional array is as follows:
[0061] / / Generate a two-dimensional mask array according to RLE data
[0062] export function getMask(segmentation) {
[0063] const [h, w] = segmentation.size;
[0064] const mask = new Uint8Array(h * w); / / Use TypedArray to improve performance
[0065] let idx = 0;
[0066] let parity = 0;
[0067] for (const count of segmentation.counts) {
[0068] for (let i = 0; i < count; i++) {
[0069] mask[idx++] = parity; / / 0 or 1 represents background / foreground
[0070] }
[0071] parity = 1 - parity; / / Toggle state
[0072] }
[0073] It should be noted that when rendering an image on the browser side, it is usually necessary to use a two-dimensional array to represent pixel data. For example, the getImageData method of canvas.getContext('2d') is used to return a two-dimensional array. And to optimize data storage, determining the target two-dimensional array corresponding to the target one-dimensional array based on the preset segmentation method may include: first creating an initial two-dimensional array corresponding to the target one-dimensional array, and determining the second target loop count based on the target width of the target image; then determining the target segmentation interval corresponding to the target one-dimensional array based on the preset column segmentation method and the target height of the target image, and determining each target element in the initial two-dimensional array based on the preset interception method, the target segmentation interval, and the target one-dimensional array, so as to determine the target two-dimensional array corresponding to the target one-dimensional array based on the second target loop count and each target element. Specifically, Uint8Array can be used to store one-dimensional data, and the subarray method can be combined to dynamically generate a two-dimensional view, avoiding creating redundant independent arrays and saving memory occupancy. The code for generating the target two-dimensional array is as follows:
[0074] / / Convert to two-dimensional array (segment by column)
[0075] const result = [];
[0076] for (let col = 0; col < w; col++) {
[0077] result[col] = mask.subarray(col * h, (col + 1) * h);
[0078] }
[0079] return result;
[0080] }
[0081] In this embodiment, the RLE encoding can be decoded to obtain a target one-dimensional array, and the target one-dimensional array can be converted into a target two-dimensional array to facilitate subsequent image processing.
[0082] Step S13: Determine the target one-dimensional position corresponding to the current mouse position, and determine a target matching index based on a preset binary search algorithm, the target compressed encoding, and the target one-dimensional position, so as to determine a target segmentation region based on the target matching index and the target two-dimensional array, and segment the target image based on a preset dynamic rendering technique and the target segmentation region.
[0083] In this embodiment, the determination of the target one-dimensional position corresponding to the current mouse position may include: First, determine the current mouse position based on a preset mouse event listening and the target size corresponding to the target image, and determine the current display size of the target image; then determine a target scaling ratio based on the display size and the target size, and determine the target one-dimensional position corresponding to the current mouse position based on the target scaling ratio, the target size, and the current mouse position. Specifically, the current mouse position (x, y) of the user can be obtained based on the mouse event listening. After scaling, it can be mapped to a corresponding target one-dimensional position. ; where scale represents the scaling ratio of the current display size of the target image to the target size corresponding to the target image. For example, if the viewport width is 800px and the actual width of the image is 1024px, then scale = 1024 / 800 = 1.28. By mapping the current mouse position to the target one-dimensional position, the current mouse position can be converted from two-dimensional coordinates (x, y) to a linear index, which is convenient for quickly locating the target segmentation region through binary search subsequently.
[0084] It should be noted that the determination of the target segmentation region based on the target matching index and the target two-dimensional array, and the segmentation of the target image based on a preset dynamic rendering technique and the target segmentation region may include: First, determine the corresponding target segmentation pixels in the target one-dimensional array based on the target matching index, and determine the corresponding target segmentation region in the target two-dimensional array based on the target segmentation pixels and the current target viewport of the target image; then segment the target image based on a preset shader program, the preset dynamic rendering technique, and the target segmentation region, and highlight the target segmentation region in the target image. The image segmentation result can be seen in Figure 2As shown, the face area is blurred due to privacy concerns. Specifically, to reduce the front-end rendering pressure, avoid rendering the entire image mask data at once, and improve the rendering efficiency and smoothness. First, it is necessary to obtain the position and size information of the user's current viewport. Then, calculate the corresponding pixel range based on the input target matching index. Next, traverse the target two-dimensional array to extract the pixels that are within the pixel range and within the current target viewport to determine the target segmentation area. Thus, only the area within the current viewport is rendered, and the Canvas drawing is accelerated using the corresponding shader program of WebGL (i.e., Web Graphics Library). That is, when the user continuously scrolls the page, the mask of the visible area is dynamically loaded to avoid rendering the entire image at once, which may cause lag.
[0085] As can be seen from the above, in this embodiment, first, a preset image segmentation model is used to determine the target binary mask corresponding to the target image, and a target compression code corresponding to the target binary mask is generated based on a preset data structure; then, a target one-dimensional array corresponding to the target compression code is generated based on a preset typed array condition, and a target two-dimensional array corresponding to the target one-dimensional array is determined based on a preset segmentation method; finally, the target one-dimensional position corresponding to the current mouse position is determined, and a target matching index is determined based on a preset binary search algorithm, the target compression code, and the target one-dimensional position, so as to determine the target segmentation area based on the target matching index and the target two-dimensional array, and segment the target image based on a preset dynamic rendering technique and the target segmentation area. As can be seen from the above, in this embodiment, the target binary mask corresponding to the target image is compressed into a target compression code, and a corresponding target one-dimensional array is generated to determine the target two-dimensional array corresponding to the target one-dimensional array. The target matching index is determined according to the current mouse position, the preset binary search algorithm, and the target compression code, so as to determine the target segmentation area according to the target matching index and the target two-dimensional array, and segment the target image based on the preset dynamic rendering technique and the target segmentation area. In this way, in this embodiment, by compressing the target binary mask of the target image, the data transmission volume is reduced, the network latency is reduced, and the time complexity is directly reduced by locating the target segmentation area based on the binary search algorithm, thereby improving the image segmentation response speed. In this way, this embodiment can improve the efficiency and accuracy of image segmentation.
[0086] See Figure 3 As shown, in order to locate the target segmentation area based on the binary search algorithm, an embodiment of the present invention further discloses an image segmentation method, which may include:
[0087] Step S21: Use a preset image segmentation model to determine the target binary mask corresponding to the target image, and generate a target compression code corresponding to the target binary mask based on a preset data structure.
[0088] Step S22: Generate a target one-dimensional array corresponding to the target compression code based on a preset typed array condition, and determine a target two-dimensional array corresponding to the target one-dimensional array based on a preset segmentation method.
[0089] Step S23: Determine a target one-dimensional position corresponding to the current mouse position, and determine a target matching index based on a preset binary search algorithm, the target compression code, and the target one-dimensional position, so as to determine a target segmentation area based on the target matching index and the target two-dimensional array, and segment the target image based on a preset dynamic rendering technique and the target segmentation area.
[0090] In this embodiment, first, determine a first target pointer and a second target pointer corresponding to the target encoding cumulative sum array in the target compression code; the first target pointer points to the starting element of the target encoding cumulative sum array, and the second target pointer points to the ending element of the target encoding cumulative sum array; then, if it is determined that the first initial pointer is not greater than the second initial pointer, determine an intermediate index based on the first initial pointer and the second initial pointer, and determine a target intermediate element corresponding to the target encoding cumulative sum array based on the intermediate index; then, if it is determined that the target intermediate element is less than the target one-dimensional position, update the first target pointer based on a first preset pointer update condition; if it is determined that the target intermediate element is not less than the target one-dimensional position, update the second target pointer based on a second preset pointer update condition; then jump to the step of determining the intermediate index based on the first initial pointer and the second initial pointer until the first initial pointer is greater than the second initial pointer, and determine the target matching index based on the first initial pointer. Specifically, the code for determining the target matching index is as follows:
[0091] function getIndex(cums: number[], position: number): number {
[0092] let left = 0, right = cums.length - 1; / / Initialize the pointers
[0093] while (left <= right) {
[0094] const mid = Math.floor((left + right) / 2);
[0095] if (cums[mid] < position) {
[0096] left = mid + 1;
[0097] } else {
[0098] right = mid - 1;
[0099] }
[0100] } / / Binary search loop
[0101] return left - 1;
[0102] }
[0103] In this embodiment, the target matching index is determined through the binary search algorithm, so that the time complexity can be reduced from to , ensuring real-time response when the mouse moves. It can be understood that this embodiment can introduce an LRU cache (Least Recently Used) based on the binary search to store the results of the last 10 queries. For example, when the user continuously operates in the same area, the results are directly read from the cache to avoid repeated calculations, thereby further improving the response speed in high-frequency operation scenarios.
[0104] Among them, for the more specific processing procedures of the above steps S21 and S22, reference can be made to the corresponding content disclosed in the foregoing embodiments, and details will not be elaborated here.
[0105] As can be seen from the above, in this embodiment, the target matching index can be determined based on the binary search algorithm, target compression encoding, and fast matching of the target one-dimensional position. Then, the target segmentation region is determined according to the target matching index and the target two-dimensional array, and the target image is segmented based on the preset dynamic rendering technology and the target segmentation region. In this way, this embodiment can reduce the time complexity of index positioning to , break through the performance bottleneck of traditional pixel-by-pixel traversal, and support a smooth mouse following effect.
[0106] Correspondingly, as shown in Figure 4 , this application embodiment also provides an image segmentation device, which may include:
[0107] A target compression encoding generation module 11, configured to use a preset image segmentation model to determine a target binary mask corresponding to a target image, and generate a target compression encoding corresponding to the target binary mask based on a preset data structure;
[0108] A target two-dimensional array determination module 12, configured to generate a target one-dimensional array corresponding to the target compression encoding based on a preset typed array condition, and determine a target two-dimensional array corresponding to the target one-dimensional array based on a preset segmentation method;
[0109] A target image segmentation module 13, configured to determine a target one-dimensional position corresponding to the current mouse position, and determine a target matching index based on a preset binary search algorithm, the target compression encoding, and the target one-dimensional position, so as to determine a target segmentation region based on the target matching index and the target two-dimensional array, and segment the target image based on a preset dynamic rendering technique and the target segmentation region.
[0110] As can be seen from the above, in this application, first, a preset image segmentation model is used to determine a target binary mask corresponding to a target image, and a target compression encoding corresponding to the target binary mask is generated based on a preset data structure; then, a target one-dimensional array corresponding to the target compression encoding is generated based on a preset typed array condition, and a target two-dimensional array corresponding to the target one-dimensional array is determined based on a preset segmentation method; finally, a target one-dimensional position corresponding to the current mouse position is determined, and a target matching index is determined based on a preset binary search algorithm, the target compression encoding, and the target one-dimensional position, so as to determine a target segmentation region based on the target matching index and the target two-dimensional array, and segment the target image based on a preset dynamic rendering technique and the target segmentation region. As can be seen from the above, in this application, the target binary mask corresponding to the target image is compressed into a target compression encoding, and a corresponding target one-dimensional array is generated to determine the target two-dimensional array corresponding to the target one-dimensional array. The target matching index is determined according to the current mouse position, the preset binary search algorithm, and the target compression encoding, so as to determine the target segmentation region according to the target matching index and the target two-dimensional array, and segment the target image based on the preset dynamic rendering technique and the target segmentation region. In this way, this application reduces the data transmission volume and network latency by compressing the target binary mask of the target image, and directly reduces the time complexity by positioning the target segmentation region based on the binary search algorithm, improving the image segmentation response speed. In this way, this application can improve the efficiency and accuracy of image segmentation.
[0111] In some specific embodiments, the target compression encoding generation module 11 may include:
[0112] A target binary mask determination unit, configured to use the preset image segmentation model to determine the target binary mask corresponding to the target image based on a preset edge closing strategy and a preset post-processing algorithm;
[0113] A target quantity determination unit, configured to scan the target binary mask based on a preset scanning condition, and determine the target quantity corresponding to the consecutive and identical binary masks in the target binary mask according to the corresponding scanning result;
[0114] A target compression encoding generation unit, configured to generate a target encoding sequence corresponding to the target binary mask according to the target quantity, and generate a corresponding target encoding cumulative sum array based on the target encoding sequence, so as to generate the target compression encoding based on the target size corresponding to the target image, the target encoding sequence, the target encoding cumulative sum array, and the preset data structure.
[0115] In some specific embodiments, the target two-dimensional array determination module 12 may include:
[0116] An initial one-dimensional array creation unit, configured to create an initial one-dimensional array based on the target size corresponding to the target image and the preset typed array condition, and initialize a target index and a target state corresponding to the initial one-dimensional array;
[0117] A first target loop count determination unit, configured to determine a current target encoding in the target encoding sequence according to a preset order condition, determine a first target loop count corresponding to the current target encoding, and determine each target element in the initial one-dimensional array based on the first target loop count and the current target state;
[0118] A target one-dimensional array determination unit, configured to determine the target index corresponding to each target element based on a preset increment rule, update the target state based on a preset state transition rule, and determine the next target encoding as the current target encoding, and jump to the step of determining the first target loop count corresponding to the current target encoding, until the target one-dimensional array corresponding to the target encoding sequence is determined based on each target element.
[0119] In some specific embodiments, the target two-dimensional array determination module 12 may include:
[0120] A second target loop count determination unit, configured to create an initial two-dimensional array corresponding to the target one-dimensional array, and determine a second target loop count based on the target width corresponding to the target image;
[0121] A target two-dimensional array determination unit, configured to determine a target segmentation interval corresponding to the target one-dimensional array based on a preset column segmentation method and the target height corresponding to the target image, and determine each target element in the initial two-dimensional array based on a preset interception method, the target segmentation interval, and the target one-dimensional array, so as to determine the target two-dimensional array corresponding to the target one-dimensional array based on the second target loop count and each target element.
[0122] In some specific embodiments, the target image segmentation module 13 may include:
[0123] A display size determination unit, configured to determine the current mouse position based on a preset mouse event monitoring and a target size corresponding to the target image, and determine a current display size of the target image;
[0124] A target one-dimensional position determination unit, configured to determine a target scaling ratio based on the display size and the target size, and determine a target one-dimensional position corresponding to the current mouse position based on the target scaling ratio, the target size, and the current mouse position.
[0125] In some specific embodiments, the target image segmentation module 13 may include:
[0126] A target pointer determination unit, configured to determine a first target pointer and a second target pointer corresponding to a target coding cumulative sum array in the target compression coding; the first target pointer points to a starting element of the target coding cumulative sum array, and the second target pointer points to an ending element of the target coding cumulative sum array;
[0127] A target middle element determination unit, configured to, if it is determined that the first initial pointer is not greater than the second initial pointer, determine a middle index based on the first initial pointer and the second initial pointer, and determine a target middle element corresponding to the target coding cumulative sum array based on the middle index;
[0128] A first target pointer update unit, configured to, when it is determined that the target middle element is less than the target one-dimensional position, update the first target pointer based on a first preset pointer update condition;
[0129] A second target pointer update unit, configured to, when it is determined that the target middle element is not less than the target one-dimensional position, update the second target pointer based on a second preset pointer update condition;
[0130] A target matching index determination unit, configured to jump to the step of determining the middle index based on the first initial pointer and the second initial pointer, until the first initial pointer is greater than the second initial pointer, and determine the target matching index based on the first initial pointer.
[0131] In some specific embodiments, the target image segmentation module 13 may include:
[0132] A target segmentation area determination unit, configured to determine corresponding target segmentation pixels in the target one-dimensional array based on the target matching index, and determine a corresponding target segmentation area in the target two-dimensional array based on the target segmentation pixels and a current target viewport of the target image;
[0133] A target image segmentation unit, configured to segment the target image based on a preset shader program, the preset dynamic rendering technique, and the target segmentation region, and highlight the target segmentation region in the target image.
[0134] Furthermore, an embodiment of the present application also discloses an electronic device. Figure 5 It is a structural diagram of an electronic device 20 shown according to an exemplary embodiment. The content in the figure should not be considered as any limitation to the scope of use of the present application. The electronic device 20 may specifically include: at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26. Among them, the memory 22 is used to store a computer program, and the computer program is loaded and executed by the processor 21 to implement the relevant steps in the image segmentation method disclosed in any of the foregoing embodiments. Additionally, the electronic device 20 in this embodiment may specifically be an electronic computer.
[0135] In this embodiment, the power supply 23 is used to provide operating voltages for each hardware device on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and external devices, and the communication protocol it follows can be any communication protocol applicable to the technical solution of the present application, and no specific limitation is imposed thereon herein; the input / output interface 25 is used to obtain external input data or output data to the outside, and the specific interface type thereof can be selected according to specific application requirements, and no specific limitation is made herein.
[0136] In addition, as a carrier for resource storage, the memory 22 may be a read-only memory, a random access memory, a magnetic disk, or an optical disc, etc. The resources stored thereon may include an operating system 221, a computer program 222, etc., and the storage method may be temporary storage or permanent storage.
[0137] Among them, the operating system 221 is used to manage and control each hardware device and the computer program 222 on the electronic device 20, and it may be Windows Server, Netware, Unix, Linux, etc. In addition to the computer program capable of implementing the image segmentation method executed by the electronic device 20 disclosed in any of the foregoing embodiments, the computer program 222 may further include a computer program capable of performing other specific tasks.
[0138] Furthermore, the present application also discloses a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, it implements the image segmentation method disclosed above. For the specific steps of the method, reference may be made to the corresponding content disclosed in the foregoing embodiments, and details are not repeated herein.
[0139] In this specification, the various embodiments are described in a progressive manner. Each embodiment focuses on the differences from other embodiments. For the same or similar parts among the embodiments, reference can be made to each other. For the devices disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the description is relatively simple. For related parts, reference can be made to the description in the method section.
[0140] Those skilled in the art can further realize that the units and algorithm steps of the examples described in combination with the embodiments disclosed in this article can be implemented by electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described according to functions in the above description. Whether these functions are executed in a hardware or software manner depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of this application.
[0141] The steps of the methods or algorithms described in combination with the embodiments disclosed in this article can be directly implemented by hardware, software modules executed by a processor, or a combination of both. The software modules can be placed in a random access memory (RAM), internal memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disk, removable disk, CD-ROM, or any other form of storage medium well-known in the technical field.
[0142] Finally, it should also be noted that in this article, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the term "comprising", "including" or any other variation thereof is intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements not only includes those elements, but also includes other elements not expressly listed, or elements inherent to such process, method, article or device. Without further limitation, an element defined by the statement "including a..." does not exclude the existence of additional identical elements in the process, method, article or device including the element.
[0143] The above has introduced the technical solutions provided in this application in detail. Specific examples are used in this article to elaborate on the principles and implementation manners of this application. The description of the above embodiments is only used to help understand the method and its core idea of this application; at the same time, for those of ordinary skill in the art, based on the idea of this application, there will be changes in the specific implementation manners and application scopes. In summary, the content of this specification should not be construed as a limitation to this application.
Claims
1. An image segmentation method, characterized in that, Including: Determine a target binary mask corresponding to a target image using a preset image segmentation model, and generate a target compression code corresponding to the target binary mask based on a preset data structure; Generate a target one-dimensional array corresponding to the target compression code based on a preset typed array condition, and determine a target two-dimensional array corresponding to the target one-dimensional array based on a preset segmentation method; Determine a target one-dimensional position corresponding to the current mouse position, and determine a target matching index based on a preset binary search algorithm, the target compression code, and the target one-dimensional position, so as to determine a target segmentation region based on the target matching index and the target two-dimensional array, and segment the target image based on a preset dynamic rendering technique and the target segmentation region.
2. The image segmentation method according to claim 1, wherein The determining a target binary mask corresponding to a target image using a preset image segmentation model, and generating a target compression code corresponding to the target binary mask based on a preset data structure includes: Determine the target binary mask corresponding to the target image using the preset image segmentation model based on a preset edge closing strategy and a preset post-processing algorithm; Scan the target binary mask based on a preset scanning condition, and determine a target number corresponding to consecutive and identical binary masks in the target binary mask according to the corresponding scanning result; Generate a target coding sequence corresponding to the target binary mask according to the target number, and generate a corresponding target coding cumulative sum array based on the target coding sequence, so as to generate the target compression code based on the target size corresponding to the target image, the target coding sequence, the target coding cumulative sum array, and the preset data structure.
3. The image segmentation method according to claim 2, wherein The generating a target one-dimensional array corresponding to the target compression code based on a preset typed array condition includes: Create an initial one-dimensional array based on the target size corresponding to the target image and the preset typed array condition, and initialize a target index and a target state corresponding to the initial one-dimensional array; Determine a current target code in the target coding sequence according to a preset order condition, determine a first target loop count corresponding to the current target code, and determine each target element in the initial one-dimensional array based on the first target loop count and the current target state; Determine the target index corresponding to each target element based on a preset increment rule, update the target state based on a preset state transition rule, and determine the next target code as the current target code, and jump to the step of determining the first target loop count corresponding to the current target code, until the target one-dimensional array corresponding to the target coding sequence is determined based on each target element.
4. The image segmentation method according to claim 1, wherein The determining a target two-dimensional array corresponding to the target one-dimensional array based on a preset segmentation method includes: Create an initial two-dimensional array corresponding to the target one-dimensional array, and determine a second target loop count based on the target width corresponding to the target image; Determine the target segmentation interval corresponding to the target one-dimensional array based on a preset column splitting method and the target height corresponding to the target image, and determine each target element in the initial two-dimensional array based on a preset truncation method, the target segmentation interval, and the target one-dimensional array, so as to determine the target two-dimensional array corresponding to the target one-dimensional array based on the second target loop count and each target element.
5. The image segmentation method according to claim 1, wherein The determining of the target one-dimensional position corresponding to the current mouse position includes: Determine the current mouse position based on a preset mouse event listening and the target size corresponding to the target image, and determine the current display size of the target image; Determine a target scaling ratio based on the display size and the target size, and determine the target one-dimensional position corresponding to the current mouse position based on the target scaling ratio, the target size, and the current mouse position.
6. The image segmentation method according to claim 2, characterized in that The determining of the target matching index based on a preset binary search algorithm, the target compression encoding, and the target one-dimensional position includes: Determine a first target pointer and a second target pointer corresponding to the target encoding cumulative sum array in the target compression encoding; the first target pointer points to the starting element of the target encoding cumulative sum array, and the second target pointer points to the ending element of the target encoding cumulative sum array; If it is determined that the first initial pointer is not greater than the second initial pointer, determine an intermediate index based on the first initial pointer and the second initial pointer, and determine a corresponding target intermediate element in the target encoding cumulative sum array based on the intermediate index; If it is determined that the target intermediate element is less than the target one-dimensional position, update the first target pointer based on a first preset pointer update condition; If it is determined that the target intermediate element is not less than the target one-dimensional position, update the second target pointer based on a second preset pointer update condition; Jump to the step of determining the intermediate index based on the first initial pointer and the second initial pointer until the first initial pointer is greater than the second initial pointer, and determine the target matching index based on the first initial pointer.
7. The image segmentation method according to any one of claims 1 to 6, characterized in that, The segmenting of the target image based on the target matching index and the target two-dimensional array and based on a preset dynamic rendering technique and the target segmentation region includes: Determine corresponding target segmentation pixels in the target one-dimensional array based on the target matching index, and determine the corresponding target segmentation region in the target two-dimensional array based on the target segmentation pixels and the current target viewport of the target image; Segment the target image based on a preset shader program, the preset dynamic rendering technique, and the target segmentation region, and highlight the target segmentation region in the target image.
8. An image segmentation device, characterized in that, Includes: A target compression encoding generation module, configured to use a preset image segmentation model to determine a target binary mask corresponding to a target image, and generate a target compression encoding corresponding to the target binary mask based on a preset data structure; A target two-dimensional array determination module, configured to generate a target one-dimensional array corresponding to the target compression code based on a preset typed array condition, and determine a target two-dimensional array corresponding to the target one-dimensional array based on a preset segmentation method; A target image segmentation module, configured to determine a target one-dimensional position corresponding to a current mouse position, and determine a target matching index based on a preset binary search algorithm, the target compression code, and the target one-dimensional position, so as to determine a target segmentation area based on the target matching index and the target two-dimensional array, and segment the target image based on a preset dynamic rendering technique and the target segmentation area.
9. An electronic device, characterized in that, Comprising: A memory, configured to store a computer program; A processor, configured to execute the computer program to implement the image segmentation method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, For storing a computer program; wherein, when the computer program is executed by the processor, the image segmentation method according to any one of claims 1 to 7 is implemented.