High-precision image background removing method and device, medium and equipment
By preprocessing images and performing multi-scale, multi-view model processing, the problem of poor background removal performance in the jewelry industry using traditional techniques has been solved, achieving high-quality background separation results.
Patent Information
- Application Number
- CN202510871157.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-26
- Publication Date
- 2025-11-21
AI Technical Summary
Traditional image background removal techniques perform poorly on complex backgrounds, blurred boundaries, or detailed areas, especially in the jewelry industry where they are less effective.
By preprocessing the original image into a unified image tensor, post-processing of the image is performed using a pre-trained model structure. Multi-scale and multi-view strategies are combined to classify and segment the foreground and background, generating a high-quality mask image.
It achieves fast, stable and high-quality background removal for images in the jewelry industry, ensuring the preservation of image structure and texture information and improving background separation effect.
Smart Images

Figure CN120997247A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of image processing technology, and in particular to a high-precision image background removal method, apparatus, medium, and device. Background Technology
[0002] With the rapid development of e-commerce, virtual try-on, and post-processing of photography, image background removal technology, as an important branch of intelligent image understanding and reconstruction, is gradually becoming one of the core functions of image processing systems.
[0003] Traditional image background removal techniques rely on manual operation or heuristic algorithms based on image edges. They often perform poorly in complex backgrounds, blurred boundaries, or detailed areas (such as hair strands or metal chains). For example, when applied to the field of jewelry, the processed images have low accuracy.
[0004] Therefore, there is an urgent need for a new high-precision image background removal method or device to achieve fast, stable and high-quality background removal in the field of jewelry. Summary of the Invention
[0005] In view of the above problems, the present invention is proposed to provide a high-precision image background removal method, apparatus, medium and device that overcomes or at least partially solves the above problems.
[0006] Other features and advantages of the invention will become apparent from the following detailed description, or may be learned in part by practice of the invention.
[0007] According to a first aspect of the present invention, a high-precision image background removal method is provided, the high-precision image background removal method comprising:
[0008] The uploaded original image is preprocessed to obtain an image tensor;
[0009] The model structure is trained based on a preset dataset or historical data of image background removal, and the trained model structure is loaded.
[0010] Based on the loaded model structure, the image tensor is post-processed to obtain a white background image of the original image.
[0011] In some embodiments of the present invention, the preprocessing of the uploaded original image to obtain an image tensor includes:
[0012] The acquired original image is converted to a new format to obtain a first image with a unified color space.
[0013] Based on a preset target resolution, the first image is scaled to obtain a second image;
[0014] The second image is normalized to obtain the image tensor.
[0015] In some embodiments of the present invention, the loaded trained model structure includes:
[0016] The model structure is built using Python class definitions based on the configuration file;
[0017] Read and load the weight file obtained from model training, and map the weights to the model structure according to the weight file to align the model structure with the weights;
[0018] The model structure, after being aligned with the weights, is loaded and automatically allocated to the target terminal according to the resource configuration.
[0019] In some embodiments of the present invention, the image post-processing of the image tensor based on the loaded model structure to obtain the white background image of the original image includes:
[0020] Based on the model structure, foreground and background are classified and determined for each pixel position in the image to obtain a single-channel probability map;
[0021] The probability map is converted to grayscale format and resampled to the exact same size as the original image using bilinear interpolation to obtain the mask image.
[0022] Based on the mask image, the original image is segmented into foreground and background to extract the foreground image, thus obtaining the white background image of the original image.
[0023] According to a second aspect of the present invention, a high-precision image background removal apparatus is provided, the high-precision image background removal apparatus comprising:
[0024] The image preprocessing module is used to preprocess the uploaded raw image to obtain an image tensor;
[0025] The model loading module is used to train the model structure based on a preset dataset or historical data of image background removal, and to load the trained model structure.
[0026] The image post-processing module is used to perform image post-processing on the image tensor based on the loaded model structure to obtain a white background image of the original image.
[0027] In some embodiments of the present invention, the image preprocessing module is used for:
[0028] The acquired original image is converted to a new format to obtain a first image with a unified color space.
[0029] Based on a preset target resolution, the first image is scaled to obtain a second image;
[0030] The second image is normalized to obtain the image tensor.
[0031] In some embodiments of the present invention, the model loading module is used for:
[0032] The model structure is built using Python class definitions based on the configuration file;
[0033] Read and load the weight file obtained from model training, and map the weights to the model structure according to the weight file to align the model structure with the weights;
[0034] The model structure, after being aligned with the weights, is loaded and automatically allocated to the target terminal according to the resource configuration.
[0035] In some embodiments of the present invention, the image post-processing module is used for:
[0036] Based on the model structure, foreground and background are classified and determined for each pixel position in the image to obtain a single-channel probability map;
[0037] The probability map is converted to grayscale format and resampled to the exact same size as the original image using bilinear interpolation to obtain the mask image.
[0038] Based on the mask image, the original image is segmented into foreground and background to extract the foreground image, thus obtaining the white background image of the original image.
[0039] According to a third aspect of the present invention, a computer-readable storage medium is provided, wherein computer program instructions are stored therein, the computer program instructions being loaded and executed by a processor to perform the operations performed by the method described in any of the preceding claims.
[0040] According to a fourth aspect of the present invention, an electronic device is provided, including a processor and a memory, the memory storing computer program instructions executable by the processor, wherein when the processor executes the computer program instructions, it implements the instructions of any of the methods described above.
[0041] The technical solutions provided in the embodiments of the present invention have at least the following technical effects or advantages:
[0042] This invention provides a high-precision image background removal method, apparatus, medium, and device. The high-precision image background removal method preprocesses the original image, converting it into a unified and standardized image tensor to meet the input requirements of downstream segmentation models while preserving the image's structure and texture information to the greatest extent. Through loading and initializing the model structure, it ensures the model is in a consistent, stable, and controllable operating state before inference. For the jewelry industry, this invention trains the model structure using a preset dataset or a general dataset, synthetic dataset, and manually calibrated dataset obtained from historical image background removal data. Based on the loaded and trained model structure, post-processing is performed on the image tensor to segment the foreground and background images, obtaining the desired white background image. Combined with segmentation inference processes, mask generation strategies, and view fusion mechanisms, it ensures high-quality background separation under different scenarios and conditions, achieving fast, stable, and high-quality background removal from original images in the jewelry industry.
[0043] The above description is merely an overview of the technical solution of the present invention. In order to better understand the technical means of the present invention and to implement it in accordance with the contents of the specification, and to make the above and other objects, features and advantages of the present invention more apparent and understandable, specific embodiments of the present invention are described below. Attached Figure Description
[0044] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0045] Figure 1 A flowchart illustrating a high-precision image background removal method provided in an embodiment of the present invention;
[0046] Figure 2 This is a schematic diagram illustrating the principle structure of a high-precision image background removal device provided in an embodiment of the present invention. Detailed Implementation
[0047] Exemplary embodiments of the present disclosure will now be described in more detail with reference to the accompanying drawings.
[0048] The accompanying drawings illustrate various structural schematics according to embodiments of the present disclosure. These drawings are not to scale, and some details have been enlarged for clarity, and some details may have been omitted. The shapes of the various regions and layers shown in the drawings, as well as their relative sizes and positional relationships, are merely exemplary and may deviate from reality due to manufacturing tolerances or technical limitations. Furthermore, those skilled in the art can design regions / layers with different shapes, sizes, and relative positions as needed.
[0049] In the context of this disclosure, when a layer / component is referred to as being "above" another layer / component, that layer / component may be directly above the other layer / component, or there may be an intermediate layer / component between them. Additionally, if a layer / component is "above" another layer / component in one orientation, then when the orientation is reversed, that layer / component may be "below" the other layer / component. In the context of this disclosure, similar or identical components may be denoted by the same or similar reference numerals.
[0050] To better understand the above technical solutions, the following will describe the above technical solutions in detail with reference to specific implementation methods. It should be understood that the embodiments of this disclosure and the specific features in the embodiments are detailed descriptions of the technical solutions of the present invention, rather than limitations on the technical solutions of the present invention. In the absence of conflict, the embodiments of the present invention and the technical features in the embodiments can be combined with each other.
[0051] Figure 1 This is a flowchart illustrating a high-precision image background removal method provided in an embodiment of the present invention, as shown below. Figure 1 As shown, this high-precision image background removal method includes the following steps:
[0052] S1. Preprocess the uploaded original image to obtain the image tensor;
[0053] In this embodiment of the invention, the above step S1 preprocesses the uploaded original image to obtain an image tensor, including: converting the format of the acquired original image to obtain a first image with a unified color space; scaling the first image based on a preset target resolution to obtain a second image; and normalizing the second image to obtain the image tensor.
[0054] Since the preprocessing of the original image determines the quality and consistency of the input data for the model structure, this embodiment of the invention can convert the uploaded original image into a unified and standardized image tensor to meet the input requirements of the downstream segmentation model and preserve the structural and texture information of the image to the greatest extent.
[0055] For example, the original image can be an image file with a predetermined format, predetermined size, and predetermined dimensions. The predetermined format of the original image can be, for example, JPEG, PNG, etc., the predetermined size of the original image can be, for example, not less than 600×600 pixels, and the predetermined size of the original image can be, for example, not more than 30MB. Depending on the actual application requirements, and based on different system configurations, hardware configurations, and applications in different fields or scenarios, the predetermined format, predetermined size, and predetermined dimensions of the original image can also be other formats, sizes, or dimensions.
[0056] In this embodiment of the invention, the original image can be decoded using OpenCV and PIL libraries. The original image is first converted to the standard RGB color space. In some formats (such as PNG), the original image may have an alpha channel. This embodiment explicitly removes the alpha channel, retaining only the RGB three-channel data for subsequent processing, ensuring consistent model input dimensions. Specifically, OpenCV image reading defaults to BGR channel order. This embodiment automatically performs the `cv2.cvtColor(image, cv2.COLOR_BGR2RGB)` operation for channel conversion. All image data is unified to uint8 format, and pixel values are restricted to the [0, 255] range. For input images with color space anomalies (such as CMYK or grayscale), automatic channel expansion or color conversion is performed, ultimately obtaining the first image with a unified color space. Through this step, this embodiment ensures color consistency of the input data and eliminates interference from abnormal formats on model inference.
[0057] It should be noted that, since the part of the uploaded original image that actually needs to be segmented into foreground and background may only be a part of the original image, this embodiment of the invention can also crop the uploaded original image to obtain a target image, and convert the target image into a format to obtain a first image with a unified color space, thereby improving the efficiency and accuracy of subsequent background removal.
[0058] To match the input dimension requirements of the model architecture, the first image will be adjusted to the standard resolution specified by the model structure during the preprocessing stage. This embodiment of the invention sets a target size that matches the model structure and performs the adjustment operation. For example, based on a preset target resolution, this embodiment of the invention scales the first image to obtain a second image. The scaling operation can use bilinear interpolation to preserve detail information to the greatest extent while ensuring smooth edges. When scaling the first image according to the aspect ratio of the original image and the preset target resolution, if the aspect ratio of the first image is inconsistent with the target resolution, center cropping or edge padding (zero-padding) is performed first to avoid deformation. If the original image size is smaller than the target resolution, proportional scaling is performed followed by padding. All original image output sizes are strictly aligned with the input dimension of the model structure to prevent input shapemismatch errors. This step ensures model input consistency, inference stability, and edge prediction quality. After scaling the first image to adjust its size, this embodiment of the invention performs normalization on the second image. The second image is converted to a tensor format image tensor using the `transforms.ToTensor()` method in PyTorch. Pixel values are converted from uint8 to float32 and normalized to the range of 0.0, 1.0. The channel order is converted from (H, W, C) to the (C, H, W) format required by PyTorch. Then, a normalization operation is performed, subtracting the mean from each channel and dividing by the standard deviation. In the specified embodiment, the normalization parameters used are, for example: mean: 0.485, 0.456, 0.406; standard deviation: 0.229, 0.224, 0.225; the normalization formula is as follows: normalized_image = (image_tensor - mean) / std, where image_tensor is the channel pixel value of the second image; this step can eliminate the differences in brightness, color temperature, etc. of the image, so that the input data is in a uniform distribution range in the model structure, which helps to improve the generalization ability and inference stability of the model structure.
[0059] S2. Train the model structure based on the preset dataset or historical data of image background removal, and load the trained model structure.
[0060] Before loading the model structure, this embodiment of the invention trains the model structure based on a preset dataset or historical data of image background removal. Specifically, in the initial training stage and the model pre-training stage, this embodiment of the invention trains the model structure based on a preset dataset or historical data of image background removal. The preset dataset can be a current mainstream image segmentation and saliency detection dataset, covering rich scenes, complex backgrounds, and high-quality annotations, which facilitates the model to learn a stable foreground / background judgment pattern. Examples include the DIS5K (Deep Image Matting Dataset) series, the COD (Camouflaged Object Detection Dataset) series, and the HRSOD (High-Resolution Salient Object Detection) dataset. The DIS5K series contains 5000 sets of high-quality images and their corresponding alpha matte masks, with rich edge details, which is suitable for training a delicate matting model. It is particularly valuable for reference in areas such as hair strands and transparent objects; the COD series contains images where the target is extremely concealed or blended into the background, which helps improve the model's segmentation perception ability in complex backgrounds and reduce misjudgments; the HRSOD dataset is characterized by high image resolution and clear edges of foreground targets, providing high-fidelity supervision signals for the model; the aforementioned preset datasets are general datasets, mainly used for building basic model capabilities, edge perception training, and improving scale generalization ability, playing a key role in the early pre-training stage of the model; the historical data for image background removal refers to various historical data generated during the image background removal process based on the embodiments of the present invention, including but not limited to various parameters of the model structure, as well as the original image, image tensor, and the final obtained white background image.
[0061] Based on practical application areas, embodiments of the present invention can also synthesize customized datasets according to the domain of the image background removal object. For example, for background removal of images of jade jewelry and jewelry wearing, embodiments of the present invention use a general jade synthesis dataset, a metal specular training set, and a hand wearing dataset, etc. The general jade synthesis dataset uses text-based image tools such as Stable Diffusion (SD) to synthesize high-quality jade images, combined with manual correction of masks, to form training samples with a certain degree of realism and style diversity, which is particularly suitable for images with complex foreground edges and large texture variations. The metal specular training set focuses on highly reflective metal surfaces (such as silver and gold jewelry) to improve the model's ability to separate backgrounds under bright and reflective interference. This dataset is primarily constructed by combining real-world images with high-precision masks. The hand-wearing dataset contains a large number of images of hands wearing jewelry. Considering factors such as skin color diversity, background complexity, and occlusion relationships, it is constructed by mixing synthetic data with some real-world images to address the model's recognition error in the "hand-jewelry" boundary area under wearing scenarios. In this embodiment of the invention, the labeled masks for the customized dataset can be generated and verified in the following ways: after generating images using SD, an internal lightweight segmentation model is used to predict the initial mask, followed by rapid manual review and local refinement. The mask's rationality is verified through image mixing (e.g., pseudo-synthetic alpha fusion) to confirm its quality. It should be noted that the customized dataset described in this embodiment of the invention is not limited to the jade industry. When it needs to be applied to other fields, the synthesis method of the customized dataset can be adjusted accordingly, as long as it matches the target field and covers the target field. This embodiment of the invention does not impose any restrictions on this.
[0062] In the process of model structure iteration, some evaluation results reveal weaknesses in recognition under specific scenarios, such as low-light or overexposed images, inconsistent edge processing of transparent jade materials, or fusion errors in finger-occluded areas. This invention can generate a manual calibration set based on historical data of image background removal. By analyzing and collecting model error cases in the historical data of image background removal, professional image annotation tools such as Photoshop are used to perform pixel-by-pixel masking and construct a specific correction training set as the manual calibration set for model fine-tuning. The creation of the manual calibration set can improve the masking quality, achieve pixel-level edge fitting, unify annotation rules, avoid subjective bias, and cover known weak areas of the model with image types.
[0063] In this embodiment of the invention, the model structure is trained using the preset dataset or a general dataset, synthetic dataset, and artificially calibrated dataset obtained from historical data of image background removal. After the model structure is trained, the trained model structure is loaded.
[0064] This invention provides a preparatory process before entering the inference flow through model loading and initialization. This process ensures that the model's architecture and weight parameters are correctly aligned, the running devices are reasonably allocated, and a stable execution foundation is provided for subsequent image segmentation tasks. Specifically, in this invention, step S2, loading the trained model structure, includes: constructing the model structure according to the configuration file using Python class definitions; reading and loading the weight file obtained from model training, mapping the weights to the model structure according to the weight file to align the model structure with the weights; loading the model structure aligned with the weights, and automatically allocating it to the target terminal according to resource configuration.
[0065] This invention first constructs the computational graph of the model using Python class definitions based on the configuration file (config.yaml). The model structure is implemented in a modular manner in the code, facilitating expansion and version management. The model definition includes an encoder, decoder, fusion module, and necessary attention units. After construction, it is stored in memory awaiting weight loading. In this invention, the model inherits from the PyTorch nn.Module class, supporting automatic graph construction and gradient control. The model construction process supports dynamic structure configuration (such as the number of channels, layers, dropout rate, etc.) to adapt to different deployment scenarios. It supports loading the model in the form of script modules (.pt) or weight files (.pth), possessing cross-platform portability.
[0066] In this embodiment of the invention, the weight file is saved in the form of state_dict during model training, containing the names and corresponding values of all parameter layers (such as convolutional kernel weights, bias terms, normalization parameters, etc.). The loading process of the weight file includes: using torch.load() to read the weight file from a local path or remote object storage; calling model.load_state_dict() to map the weights to the current model structure; if there are key name inconsistencies or dimension mismatches, the system will print detailed error log prompts. This embodiment of the invention enables strict loading mode (strict=True) by default to ensure that the model structure and weights are fully aligned, avoiding prediction anomalies caused by partial loading; to adapt to model version update scenarios, a non-strict mode is also supported (such as freezing some layers during fine-tuning), and unmatched parameters are recorded for analysis.
[0067] After the model structure and weight file are loaded, this embodiment of the invention automatically allocates them to the target terminal according to the system's resource configuration. The resource configuration includes CPU, GPU, etc. For example, the model structure is configured to a single CPU, a single GPU, or a multi-GPU mode. If an available GPU (such as NVIDIA CUDA) is detected, the model will be bound to the specified GPU (cuda:0). If there is no GPU, it will automatically revert to CPU mode for execution. When deploying a multi-GPU model, distributed loading is supported, which allocates different models to different GPU cores to improve inference throughput. This invention allocates the model structure to the corresponding target terminal according to the determined resource configuration. The target terminal is a computing device with CPU and GPU.
[0068] Since the input of the model structure includes image tensors during the inference phase and weight files during the model training phase, this embodiment of the invention switches the working mode of the model structure according to the input of the model structure. The working mode includes training mode and evaluation mode. Specifically, this embodiment of the invention defaults to training mode (model.train()). To prevent gradient recording and BatchNorm state fluctuations, when the input of the model structure is the image tensors during the inference phase, it switches to evaluation mode (model.eval()). Through the switching operation of the working mode, random behaviors such as Dropout are disabled, the mean and variance of BatchNorm are fixed, unstable changes are prevented from being introduced during inference, computational efficiency is improved, and unnecessary graph structure tracking is avoided. In other embodiments, this embodiment of the invention is also encapsulated in the torch.no_grad() context to completely disable the backpropagation path, saving memory and time overhead.
[0069] During the loading and initialization of the model structure, this embodiment of the invention also sets up multiple fault tolerance mechanisms. This embodiment of the invention generates corresponding log files based on the loading status and loading results of the model structure, obtains loading errors of the model structure according to the log files, and the loading errors include, but are not limited to, file path errors, file corruption, structural inconsistencies, etc. When it is determined that the model structure has a loading error or loading failure, a fallback mechanism is supported to automatically load a simplified model or spare weights to continue execution.
[0070] S3. Based on the loaded model structure, perform image post-processing on the image tensor to obtain the white background image of the original image.
[0071] After loading and initializing the model structure, this embodiment of the invention inputs the standardized and resized image tensor into the neural network of the model structure for inference calculation. The entire inference process is encapsulated in the torch.no_grad() context environment, avoiding unnecessary gradient calculation, thereby saving video memory resources and improving inference efficiency, which is suitable for large-scale batch processing scenarios.
[0072] In this embodiment of the invention, step S3, which performs image post-processing on the image tensor based on the loaded model structure to obtain the white background image of the original image, includes: classifying and determining the foreground and background at each pixel position in the image based on the model structure to obtain a single-channel probability map; converting the probability map into a grayscale image format and resampling it to the exact same size as the original image through bilinear interpolation to obtain a mask image; and performing foreground and background segmentation processing on the original image based on the mask image to extract the foreground image and obtain the white background image of the original image.
[0073] The model structure described in this embodiment of the invention adopts a fully convolutional structure, combining feature extraction and contextual information modeling capabilities to classify each pixel position in the image tensor as foreground or background; the output of the model structure is a single-channel probability map, where each pixel value represents the confidence that the position belongs to the foreground (i.e., the object to be retained); to enhance the nonlinear expression capability and limit the output to the [0,1] interval, the final layer uses the Sigmoid activation function to normalize the output.
[0074] In this embodiment of the invention, the probability map is further converted into a visualized and operable mask image. The probability map is converted into a format (i.e., pixel values range from 0 to 255) to ensure compatibility with traditional image processing tools. Meanwhile, to maintain input-output consistency, the grayscale image is resampled to the exact same size as the original image through bilinear interpolation to obtain the mask image. The interpolation process ensures a smooth transition of details during size changes, avoiding jagged edges and breaks. The generated high-fidelity mask can be used as input for subsequent processing steps such as image fusion, background replacement, or image matting, and has good foreground contour accuracy and segmentation quality.
[0075] To address the potential biases in detail restoration or edge detection caused by single-scale input, this invention introduces a multi-scale and multi-view strategy to improve the stability and accuracy of prediction results. The strategy includes:
[0076] Multi-scale input: The original image is scaled to different resolutions (e.g., 0.75x, 1.0x, 1.25x) and then fed into the model for prediction to capture semantic and texture information at different levels;
[0077] Multi-view transformation: The original image is mirrored to enhance the model's ability to model symmetry, which is especially suitable for objects with complex shapes or strong directionality.
[0078] Each of the above-mentioned scale inputs or viewpoint transformations will generate an independent probability map. In this embodiment of the invention, a fusion strategy (such as weighted averaging, maximum value merging, or confidence assessment) is used to integrate multiple prediction results (i.e., the probability maps) into a final mask map. This fusion mechanism effectively reduces prediction jitter and errors caused by input changes or model sensitivity, and enhances the system's adaptability and robustness to diverse images.
[0079] After obtaining a high-quality foreground segmentation mask image, this embodiment of the invention performs image fusion and background removal processing. Based on the mask image, the original image is segmented into foreground and background, and the foreground image is extracted to obtain the white background image of the original image. That is, based on the mask information, the non-foreground areas in the original image are cleared or replaced with the specified background, while maintaining the clear edges and natural transitions of the foreground objects, thereby generating a visually clean and realistic output image.
[0080] To achieve precise background removal, this embodiment of the invention further binarizes the mask image of the probabilistic grayscale image. By setting a fixed threshold (e.g., 0.5) or using adaptive threshold selection based on image statistical features, the thresholded mask image clearly classifies each pixel as either foreground (preserved) or background (removed), forming a binary mask. This embodiment of the invention then uses the binary mask to perform a foreground extraction operation on the original image to extract the foreground image. Specifically, positions with a mask value of 255 are considered foreground regions, retaining their original pixel information; while background regions with a mask value of 0 are set to transparent or replaced with a specified color value, achieving a clean background removal effect.
[0081] To further enhance the naturalness of the edges, this embodiment of the invention adopts a soft mask strategy, i.e., incomplete binarization, using the original mask image as the alpha channel to achieve a smooth transition during fusion. This method can significantly reduce the jaggedness and halo effect of the cutout edges, and is especially suitable for processing complex edge structures such as hair and accessories.
[0082] After extracting the foreground image, this embodiment of the invention controls the output of the model structure based on the target requirements to obtain the white background image of the original image. The target requirements include, but are not limited to, outputting a transparent background image (RGBA) and maintaining the image size and aspect ratio. For the output of a transparent background image, this embodiment of the invention replaces the background area with a transparent area with alpha=0 and outputs it as a PNG format with an alpha channel, which is convenient for users to freely combine it with other backgrounds. For maintaining the image size and aspect ratio, this embodiment of the invention retains the size and aspect ratio of the original image to ensure the consistency and controllability of the output image in subsequent processes (such as e-commerce display and image compression).
[0083] In the process of blending the foreground layer and the target background layer by masking, to avoid image edge tearing or color overflow, embodiments of the present invention perform optimization steps such as edge smoothing and color restoration. For example, guided filtering or edge feathering techniques are used to enhance the transition area of the mask boundary, thereby obtaining a more natural and flawless foreground image.
[0084] To adapt to different downstream applications (such as image enhancement, image search, 3D modeling input, etc.), embodiments of the present invention can also output multi-channel results including the following: foreground image (RGB), segmentation mask (grayscale or binary), alpha image (used as a transparency channel), comparison image of the original image and the synthesized image, etc.; the multi-channel output mode provides a flexible interface for subsequent processing and facilitates visualization and error analysis during the model training and evaluation stages.
[0085] The high-precision image background removal method described in this embodiment of the invention has the following advantages compared with the prior art:
[0086] 1. Preprocessing the original image can convert it into a unified and normalized image tensor, which can meet the input requirements of the downstream segmentation model and preserve the structural and texture information of the image to the greatest extent.
[0087] 2. By loading and initializing the model structure, ensure that the model is in a consistent, stable, and controllable operating state before inference;
[0088] 3. In the field of jewelry, this embodiment of the invention trains the model structure using the preset dataset or historical data of image background removal, a general dataset, a synthetic dataset, and a manually calibrated dataset. Based on the loaded and trained model structure, the image tensor is post-processed to achieve the segmentation of the foreground and background images, resulting in the desired white background image. Combined with the segmentation inference process, mask generation strategy, and view fusion mechanism, high-quality background separation effect is ensured under different scenarios and conditions, achieving fast, stable, and high-quality background removal of original images in the field of jewelry.
[0089] Based on the above embodiments, as a supplement to the above... Figure 1 The present invention provides an embodiment of a high-precision image background removal device, which is similar to the method shown. Figure 1 Corresponding to the method embodiments shown, this device can be specifically applied to various electronic devices, see reference. Figure 2 As shown, the high-precision image background removal device includes:
[0090] The image preprocessing module 100 is used to preprocess the uploaded raw image to obtain an image tensor;
[0091] The model loading module 200 is used to train the model structure based on a preset dataset or historical data of image background removal, and to load the trained model structure.
[0092] The image post-processing module 300 is used to perform image post-processing on the image tensor based on the loaded model structure to obtain a white background image of the original image.
[0093] In this embodiment of the invention, the image preprocessing module 100 is used to: convert the format of the acquired original image to obtain a first image with a unified color space; scale the first image based on a preset target resolution to obtain a second image; and normalize the second image to obtain the image tensor.
[0094] In this embodiment of the invention, the model loading module 200 is used to: construct a model structure according to a configuration file using Python class definitions; read and load a weight file obtained from model training, map the weights to the model structure according to the weight file, so that the model structure is aligned with the weights; load the model structure aligned with the weights, and automatically allocate it to the target terminal according to resource configuration.
[0095] In this embodiment of the invention, the image post-processing module 300 is used to: classify and determine the foreground and background of each pixel position in the image based on the model structure to obtain a single-channel probability map; convert the probability map into a grayscale image format and resample it to the same size as the original image through bilinear interpolation to obtain a mask image; and perform foreground and background segmentation processing on the original image according to the mask image to extract the foreground image and obtain the white background image of the original image.
[0096] The high-precision image background removal device described in this embodiment can execute the high-precision image background removal method provided in the above embodiments. The high-precision image background removal device has the corresponding functional steps and beneficial effects of the high-precision image background removal method described in the above embodiments. For details, please refer to the embodiments of the high-precision image background removal method described above. The embodiments of this invention will not be repeated here.
[0097] This invention also provides an electronic device, which may include a processor and a memory, wherein the processor and memory can be connected via a bus or other means. The processor may be a Central Processing Unit (CPU). The processor may also be other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, or combinations thereof. The memory, as a non-transitory computer-readable storage medium, can be used to store non-transitory software programs, non-transitory computer-executable programs, and modules, such as the program instructions / modules corresponding to the high-precision image background removal method in this invention embodiment. The processor executes various functional applications and data processing by running the non-transitory software programs, instructions, and modules stored in the memory, thereby implementing the high-precision image background removal method in the above method embodiment.
[0098] The memory may include a program storage area and a data storage area. The program storage area may store the operating system and at least one application program required for a function; the data storage area may store data created by the processor, etc. Furthermore, the memory may include high-speed random access memory and non-transitory memory, such as at least one disk storage device, flash memory device, or other non-transitory solid-state storage device. One or more modules are stored in the memory and, when executed by the processor, perform the high-precision image background removal method as described in the above method embodiments. Specific details of the above electronic device can be understood by referring to the corresponding descriptions and effects in the above method embodiments, and will not be repeated here. Those skilled in the art will understand that all or part of the processes in the above embodiments can be implemented by a computer program instructing related hardware. The program can be stored in a computer-readable storage medium, and when executed, it may include the processes of the embodiments of the above methods. The storage medium may be a read-only memory (ROM), a random access memory (RAM), a flash memory, a hard disk drive (HDD), or a solid-state drive (SSD), etc.; the storage medium may also include a combination of the above types of memory.
[0099] Numerous specific details are set forth in the specification provided herein. However, it will be understood that embodiments of the invention may be practiced without these specific details. In some instances, well-known methods, structures, and techniques have not been shown in detail so as not to obscure the understanding of this specification.
[0100] Similarly, it should be understood that, in order to streamline this disclosure and aid in understanding one or more of the various inventive aspects, in the above description of exemplary embodiments of the invention, various features of the invention are sometimes grouped together in a single embodiment, figure, or description thereof. However, this method of disclosure should not be interpreted as reflecting an intention that the claimed invention requires more features than are expressly recited in each claim. Rather, as reflected in the claims, inventive aspects lie in fewer than all features of a single foregoing disclosed embodiment. Therefore, the claims following the detailed description are hereby expressly incorporated into that detailed description, wherein each claim itself is a separate embodiment of the invention.
[0101] It should be noted that the above embodiments are illustrative of the invention and not restrictive of the invention, and that those skilled in the art can devise alternative embodiments without departing from the scope of the appended claims.
Claims
1. A high-precision image background removal method, characterized in that, The high-precision image background removal method includes: The uploaded original image is preprocessed to obtain an image tensor; The model structure is trained based on a preset dataset or historical data of image background removal, and the trained model structure is loaded. Based on the loaded model structure, the image tensor is post-processed to obtain a white background image of the original image.
2. The high-precision image background removal method according to claim 1, characterized in that, The preprocessing of the uploaded original image to obtain the image tensor includes: The acquired original image is converted to a new format to obtain a first image with a unified color space. Based on a preset target resolution, the first image is scaled to obtain a second image; The second image is normalized to obtain the image tensor.
3. The high-precision image background removal method according to claim 1, characterized in that, The pre-trained model structure includes: The model structure is built using Python class definitions based on the configuration file; Read and load the weight file obtained from model training, and map the weights to the model structure according to the weight file to align the model structure with the weights; The model structure, after being aligned with the weights, is loaded and automatically allocated to the target terminal according to the resource configuration.
4. The high-precision image background removal method according to claim 1, characterized in that, The post-processing of the image tensor based on the loaded model structure to obtain the white background image of the original image includes: Based on the model structure, foreground and background are classified and determined for each pixel position in the image to obtain a single-channel probability map; The probability map is converted to grayscale format and resampled to the exact same size as the original image using bilinear interpolation to obtain the mask image. Based on the mask image, the original image is segmented into foreground and background to extract the foreground image, thus obtaining the white background image of the original image.
5. A high-precision image background removal device, characterized in that, The high-precision image background removal device includes: The image preprocessing module is used to preprocess the uploaded raw image to obtain an image tensor; The model loading module is used to train the model structure based on a preset dataset or historical data of image background removal, and to load the trained model structure. The image post-processing module is used to perform image post-processing on the image tensor based on the loaded model structure to obtain a white background image of the original image.
6. The high-precision image background removal device according to claim 5, characterized in that, The image preprocessing module is used for: The acquired original image is converted to a new format to obtain a first image with a unified color space. Based on a preset target resolution, the first image is scaled to obtain a second image; The second image is normalized to obtain the image tensor.
7. The high-precision image background removal device according to claim 5, characterized in that, The model loading module is used for: The model structure is built using Python class definitions based on the configuration file; Read and load the weight file obtained from model training, and map the weights to the model structure according to the weight file to align the model structure with the weights; The model structure, after being aligned with the weights, is loaded and automatically allocated to the target terminal according to the resource configuration.
8. The high-precision image background removal device according to claim 5, characterized in that, The image post-processing module is used for: Based on the model structure, foreground and background are classified and determined for each pixel position in the image to obtain a single-channel probability map; The probability map is converted to grayscale format and resampled to the exact same size as the original image using bilinear interpolation to obtain the mask image. Based on the mask image, the original image is segmented into foreground and background to extract the foreground image, thus obtaining the white background image of the original image.
9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer program instructions that are loaded and executed by a processor to perform the operations described in any one of claims 1-4.
10. An electronic device comprising a processor and a memory, characterized in that, The memory stores computer program instructions that can be executed by the processor, and when the processor executes the computer program instructions, it implements the instructions of the method as described in any one of claims 1-4.