Image processing method and related device

By performing noise processing and diffusion model processing on images in electronic devices, the problem of false edges in blurred images is solved and a clearer image effect is achieved.

CN117974493BActive Publication Date: 2024-08-27HONOR DEVICE CO LTD
View PDF 9 Cites 0 Cited by

Patent Information

Application Number
CN202410361591.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-03-28
Publication Date
2024-08-27
Estimated Expiration
2044-03-28

AI Technical Summary

Technical Problem

When electronic equipment captures images, edges may be blurred after blur processing, resulting in unclear images.

Method used

By acquiring an image containing the foreground and transition area, adding noise and using a diffusion model to remove the noise under specific conditions, ensure that the edges of the transition area are clear, thereby reducing edge blur and false blur in the blurred image.

Benefits of technology

It effectively improves the image processing effect, ensures the clarity of foreground edges in blurred images, reduces false edge blur, and improves the overall quality of the image.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117974493B
    Figure CN117974493B
Patent Text Reader

Abstract

The embodiments of the present application provide an image processing method and related devices, and relate to the field of terminal technology. The method includes: acquiring a first image; wherein the first image includes a foreground area and a transition area, the transition area is a transition area between the foreground area and the background area, and noise is added to the transition area in the first image; inputting the first image into a first model to obtain a second image; wherein the first model is used to remove noise in the first image under the constraint of a first condition, wherein the first condition is used to indicate the quality of the image after processing by the first model. In this way, the transition area in the image is denoised according to the quality of the image indicated by the first condition, so that the transition area in the image meets the quality requirements of the image, and the edge of the foreground in the transition area is clear.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of terminal technology, and in particular to an image processing method and related devices. Background Art

[0002] Currently, electronic devices can not only support shooting functions, but also support blurring of the shot images, so that users can see a blurred image with a clear foreground and a blurred background, which gives the shot images a better sense of space.

[0003] However, the blurred image may have blurred edges. Summary of the invention

[0004] The present application provides an image processing method and related device, which are applied to the field of terminal technology. The method is used to process the edge of the foreground to make the edge clear, thereby reducing the edge blurring and other unclear edge situations during background processing such as blurring, thereby improving the image processing effect.

[0005] In a first aspect, an embodiment of the present application proposes an image processing method. The method comprises: obtaining a first image; wherein the first image comprises a foreground area and a transition area, the transition area is a transition area between the foreground area and the background area, and noise is added to the transition area in the first image; inputting the first image into a first model to obtain a second image; wherein the first model is used to remove noise in the first image under the constraint of a first condition, wherein the first condition is used to indicate the quality of the image processed by the first model.

[0006] In this way, the transition area in the image is denoised according to the quality of the image indicated by the first condition, so that the transition area in the image meets the quality requirement of the image, and the edge of the foreground in the transition area is clear.

[0007] In a possible implementation, the first condition includes a text feature, and the text feature is used to indicate one or more of the following parameters of the image after being processed by the first model: image quality, resolution, authenticity, style, and an object in the image.

[0008] In this way, the image processed by the diffusion model can be made consistent with the expected image parameters.

[0009] In one possible implementation, obtaining a first image includes: obtaining a third image; when the size of the third image is larger than a preset size, downsampling the third image to obtain a fourth image, wherein the size of the fourth image is smaller than or equal to the preset size; inputting the fourth image into an encoder for encoding to obtain an encoded fourth image; adding noise to a transition region in the encoded fourth image to obtain the first image; after obtaining the second image, the method further includes: inputting the second image into a first decoder for decoding to obtain a fifth image, wherein the size of the fifth image is larger than the preset size.

[0010] Before the image is processed by the first model, the image can be downsampled to reduce the computational complexity of the subsequent denoising process of the first model. The downsampled image can meet the size requirements of the first model for image denoising, reducing the situation where the electronic device is stuck or not supported due to excessive computational complexity.

[0011] The downsampled image is compressed into the latent space by the encoder, and the first model can perform denoising in the latent space, further reducing the computational complexity of image processing. After the first model is processed, the processed image can also be decoded by the encoder, and the decoded image size is larger than the preset size. In this way, the image processed by the first model can be restored to the size before the downsampling process.

[0012] In one possible implementation, obtaining a first image includes: obtaining a third image; when the size of the third image is less than or equal to a preset size, inputting the third image into an encoder for encoding processing to obtain an encoded third image; adding noise to a transition area in the encoded third image to obtain the first image; after obtaining the second image, the method further includes: inputting the second image into a second decoder for decoding processing to obtain a fifth image, and the size of the fifth image is equal to the size of the third image.

[0013] When the size of the third image is less than or equal to the preset size, no downsampling process may be performed. Adaptively, the image processed by the first model is decoded by another decoder.

[0014] In one possible implementation, obtaining a third image includes: obtaining a sixth image, the sixth image including: a foreground area, a transition area, and a background area; cropping the sixth image to obtain a third image, the third image including: a foreground area, a transition area, and a portion of the background area; the method also includes: fusing the fifth image into the sixth image to obtain a target image.

[0015] In this way, before the image is processed by the first model, the image can be cropped to reduce the amount of calculation for the subsequent denoising process of the first model.

[0016] In a possible implementation, adding noise to a transition region in the encoded fourth image to obtain a first image includes: using a ternary map to determine a position of the transition region in the encoded fourth image; and adding noise to the transition region in the fourth image to obtain the first image.

[0017] The ternary map is used to indicate the transition area in the image. The ternary map may correspond to the ternary map C, ternary map B or ternary map A mentioned below. In some embodiments, the position of the transition area in the encoded fourth image may be determined based on the correspondence between the ternary map B and the images before and after downsampling. The position of the transition area in the encoded fourth image may be determined based on the ternary map A, the correspondence between the images before and after cropping, and the correspondence between the images before and after downsampling. The embodiments of the present application do not specifically limit the ternary map.

[0018] The position of the transition area in the image can be determined by the ternary map, and noise can be accurately added to the transition area in the image to reduce interference to areas other than the transition area in the image.

[0019] In a possible implementation manner, the method further includes: obtaining an eighth image in response to a photographing operation; and performing background processing on the eighth image to obtain the third image or the sixth image.

[0020] The eighth image may be an image obtained by shooting. The method may be applied in a photo shooting scene. For example, it may be applied in a photo shooting scene in a portrait mode to make the hair in the portrait clear.

[0021] In a possible implementation, in response to a photographing operation, obtaining an eighth image includes: in response to the photographing operation, acquiring at least two images with different exposure times, and performing image fusion on the at least two images with different exposure times to obtain the eighth image.

[0022] The electronic device can obtain a first image with HDR effect by fusing at least two images with different exposure times.

[0023] In a possible implementation, the method further includes: displaying a first interface, the first interface displaying a ninth image and a first control; and in response to a trigger operation on the first control, performing background processing on the ninth image to obtain a third image or a sixth image.

[0024] It can be applied in image editing scenarios.

[0025] In one possible implementation, the training process of the first model includes: adding noise to a first training image to generate a noise image; predicting the noise in the noise image under the constraint of a second condition corresponding to the first training image, and removing the predicted noise from the noise image; the second condition is used to indicate the quality of the first training image, and the second condition is also used to indicate the content in the first training image; when the difference between the predicted noise and the added noise contained in the noise image satisfies the first loss function, the first model is obtained.

[0026] The first training image may correspond to the training image B hereinafter. The second condition may be Figure 8 The text prompt or text feature shown. The first loss function may correspond to the loss function loss5 described below.

[0027] In one possible implementation, the training process of the first decoder includes: downsampling the third training image to obtain a fourth training image; inputting the fourth training image into the encoder for encoding to obtain a fifth training image; inputting the fifth training image into the first decoder to be trained for decoding to obtain a second predicted image; when the difference between the second predicted image and the fifth training image satisfies the second loss function, obtaining the first decoder.

[0028] The third training image may correspond to the training image C hereinafter, the fourth training image may correspond to the training image D hereinafter, and the fifth training image may correspond to the feature map of the training image D hereinafter. The second loss function may correspond to the loss function loss6 hereinafter.

[0029] In one possible implementation, before training the first decoder, the method also includes: training the second decoder to be trained to obtain the second decoder; setting the parameters of the first decoder to be trained to be the same as the parameters of the second decoder; wherein the training process of the second decoder includes: inputting the fourth training image into the encoder for encoding processing to obtain the fifth training image; inputting the fifth training image into the second decoder to be trained for decoding processing to obtain the third predicted image; when the difference between the third predicted image and the fourth training image satisfies the third loss function, obtaining the second decoder.

[0030] In this way, some parameters can be reused, which can reduce the computational complexity of the decoder and shorten the training time of the decoder.

[0031] In a possible implementation, the first image includes: a portrait with hair, which can be applied to hair processing of the portrait.

[0032] In a second aspect, an embodiment of the present application provides an image processing device, which may be an electronic device, or a chip or chip system in an electronic device. The image processing device may include a display unit and a processing unit. When the image processing device is an electronic device, the display unit may be a display screen. The display unit is used to perform the display step so that the electronic device implements an image processing method described in the first aspect or any possible implementation of the first aspect. When the image processing device is an electronic device, the processing unit may be a processor. The image processing device may also include a storage unit, which may be a memory. The storage unit is used to store instructions, and the processing unit executes the instructions stored in the storage unit so that the electronic device implements an image processing method described in the first aspect or any possible implementation of the first aspect. When the image processing device is a chip or chip system in an electronic device, the processing unit may be a processor. The processing unit executes the instructions stored in the storage unit so that the electronic device implements an image processing method described in the first aspect or any possible implementation of the first aspect. The storage unit may be a storage unit within the chip (eg, a register, a cache, etc.), or a storage unit within the electronic device that is located outside the chip (eg, a read-only memory, a random access memory, etc.).

[0033] Exemplarily, the display unit is used to process the steps of displaying data in the image processing device; the processing unit is used to process the steps of processing data in the image processing device.

[0034] In a third aspect, an embodiment of the present application provides an electronic device, including a processor and a memory, the memory being used to store code instructions, and the processor being used to run the code instructions to execute the method described in the first aspect or any possible implementation of the first aspect.

[0035] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, in which a computer program or instructions are stored. When the computer program or instructions are run on a computer, the computer executes the method described in the first aspect or any possible implementation of the first aspect.

[0036] In a fifth aspect, an embodiment of the present application provides a computer program product including a computer program. When the computer program runs on a computer, the computer executes the method described in the first aspect or any possible implementation manner of the first aspect.

[0037] In a sixth aspect, the present application provides a chip or a chip system, the chip or chip system comprising at least one processor and a communication interface, the communication interface and the at least one processor are interconnected by a line, and the at least one processor is used to run a computer program or instruction to execute the method described in the first aspect or any possible implementation of the first aspect. The communication interface in the chip can be an input / output interface, a pin or a circuit, etc.

[0038] In a possible implementation, the chip or chip system described above in the present application further includes at least one memory, in which instructions are stored. The memory may be a storage unit inside the chip, such as a register, a cache, etc., or a storage unit of the chip (such as a read-only memory, a random access memory, etc.).

[0039] It should be understood that the second to sixth aspects of the present application correspond to the technical solutions of the first aspect of the present application, and the beneficial effects achieved by each aspect and the corresponding feasible implementation methods are similar and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS

[0040] Figure 1A A schematic diagram of a scenario provided for an embodiment of the present application;

[0041] Figure 1B A schematic diagram of an interface of an electronic device in a scenario provided by an embodiment of the present application;

[0042] Figure 1C A schematic diagram of an interface of an electronic device in a scenario provided by an embodiment of the present application;

[0043] Figure 2A A schematic diagram of an interface of an electronic device in another scenario provided by an embodiment of the present application;

[0044] Figure 2B A schematic diagram of an interface of an electronic device in another scenario provided by an embodiment of the present application;

[0045] Figure 2C A schematic diagram of an interface of an electronic device in another scenario provided by an embodiment of the present application;

[0046] Figure 2D A schematic diagram of an interface of an electronic device in another scenario provided by an embodiment of the present application;

[0047] Figure 2E A schematic diagram of an interface of an electronic device in another scenario provided by an embodiment of the present application;

[0048] Figure 2F A schematic diagram of an interface of an electronic device in another scenario provided by an embodiment of the present application;

[0049] Figure 3 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application;

[0050] Figure 4 A schematic diagram of the software structure of an electronic device provided in an embodiment of the present application;

[0051] Figure 5 A flowchart of an image processing method provided in an embodiment of the present application;

[0052] Figure 6 A schematic diagram of the architecture of a neural network B provided in an embodiment of the present application;

[0053] Figure 7 A schematic diagram of the architecture of a neural network A provided in an embodiment of the present application;

[0054] Figure 8 A schematic diagram of training a diffusion model in a neural network B provided in an embodiment of the present application;

[0055] Fig. 9 A schematic diagram of training a dVAE decoder in a neural network B provided in an embodiment of the present application;

[0056] Fig.10 A schematic diagram of a diffusion model and a dVAE decoder training provided in an embodiment of the present application;

[0057] Fig.11 A schematic diagram of the steps for generating a ternary graph provided in an embodiment of the present application;

[0058] Fig.12 A structural schematic diagram of an image processing method provided in an embodiment of the present application;

[0059] Fig.13 A schematic diagram of the structure of an image processing device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0060] In order to clearly describe the technical solutions of the embodiments of the present application, some terms and technologies involved in the embodiments of the present application are briefly introduced below:

[0061] 1. Camera Depth of Field

[0062] Depth of field can be understood as the imaging range in which a clear image can be obtained in a camera lens or other imager, or it can be understood as the clear range formed before and after the focus point. Among them, the focus point can include the clearest point obtained when light is focused on the photosensitive element through the lens. The foreground depth of field can include the clear range before the focus point, and the background depth can include the clear range after the focus point. The background depth can also be called the back depth of field.

[0063] Important factors that affect depth of field include aperture, lens, and distance to the subject. When the aperture is larger (the smaller the aperture value F), the depth of field is shallower, and when the aperture is smaller (the larger the aperture value F), the depth of field is deeper; when the lens focal length is longer, the depth of field is shallower, and when the lens focal length is shorter, the depth of field is deeper.

[0064] 2. Alpha mask (or simply mask)

[0065] The mask image can be an image generated by occluding the image (whole or part), and the mask image can be used to extract the area of ​​interest. Taking the data format of the mask image as 8bit as an example, the alpha value of each pixel in the mask image is illustrated. The alpha value of the pixel in the mask image is distributed between 0-255. The foreground area of ​​interest can be set to alpha=255, and the background area can be set to alpha=0.

[0066] Usually, a mask image can be used to solve the image matting problem. For example, the image matting problem can be modeled as follows:

[0067]

[0068] Where I is the complete image, Foreground for portraits, is the background, alpha is the portrait foreground The alpha value ranges from 0 to 255. The area with an alpha value of 0 can be the background area, the area with an alpha value of 255 can be the foreground area, and the area with an alpha value greater than 0 and less than 255 is the transition area in the mask image.

[0069] 3. Trimap

[0070] The ternary map can be an image containing three types of labels: foreground, background (background), and mixed foreground and background areas. The area of ​​each pixel in the ternary map can be one of 0, 128, and 255. The ternary map can achieve a rough division of a given image. For example, the area with trimap=0 can be the background area, the area with trimap=255 can be the foreground area, and the area with trimap=128 can be the transition area.

[0071] 4. Other terms

[0072] In the embodiments of the present application, words such as "first" and "second" are used to distinguish the same or similar items with substantially the same functions and effects. For example, the first chip and the second chip are only used to distinguish different chips, and their order is not limited. Those skilled in the art can understand that words such as "first" and "second" do not limit the quantity and execution order, and words such as "first" and "second" do not necessarily limit them to be different.

[0073] It should be noted that in the embodiments of the present application, words such as "exemplary" or "for example" are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "exemplary" or "for example" in the present application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "exemplary" or "for example" is intended to present related concepts in a specific way.

[0074] In the embodiments of the present application, "at least one" refers to one or more, and "more than one" refers to two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can represent: the existence of A alone, the existence of A and B at the same time, and the existence of B alone, where A and B can be singular or plural. The character " / " generally indicates that the previous and next associated objects are in an "or" relationship. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single or plural items. For example, at least one of a, b, or c can represent: a, b, c, ab, a--c, bc, or abc, where a, b, and c can be single or multiple.

[0075] 5. Electronic equipment

[0076] The electronic device of the embodiment of the present application may include a handheld device, a vehicle-mounted device, etc. with an image processing function. For example, some electronic devices are: mobile phones, tablet computers, PDAs, laptop computers, mobile internet devices (MIDs), wearable devices (e.g., smart watches, smart glasses, smart bracelets, or smart jewelry, etc.), virtual reality (VR) devices, augmented reality (AR) devices, wireless terminals in industrial control, wireless terminals in self driving, wireless terminals in remote medical surgery, wireless terminals in smart grids, wireless terminals in transportation safety, wireless terminals in smart cities, wireless terminals in smart homes, cellular phones, cordless phones, session initiation protocol (SIP) phones, wireless local loop (WLL) stations, personal digital assistants (PDAs), handheld devices with wireless communication capabilities, computing devices or other processing devices connected to wireless modems, vehicle-mounted devices, Internet of Things (IoT), and the like. The present invention relates to terminal devices in an Internet of Things (IoT) system, terminal devices in a 5G network, or terminal devices in a future evolved public land mobile network (PLMN), etc., and the embodiments of the present application are not limited to this.

[0077] The electronic device in the embodiments of the present application may also be referred to as: terminal equipment, user equipment (UE), mobile station (MS), mobile terminal (MT), access terminal, user unit, user station, mobile station, mobile station, remote station, remote terminal, mobile device, user terminal, terminal, wireless communication equipment, user agent or user device, etc.

[0078] In an embodiment of the present application, the electronic device or each network device includes a hardware layer, an operating system layer running on the hardware layer, and an application layer running on the operating system layer. The hardware layer includes hardware such as a central processing unit (CPU), a memory management unit (MMU), and a memory (also called main memory). The operating system can be any one or more computer operating systems that implement business processing through processes, such as a Linux operating system, a Unix operating system, an Android operating system, an iOS operating system, or a Windows operating system. The application layer includes applications such as browsers, address books, word processing software, and instant messaging software.

[0079] In daily photography, preserving the clarity of the foreground and blurring the background is a common photography method. For the camera function of mobile phones, due to the limitation of hardware modules, it is difficult for the images directly captured by electronic devices to achieve a sufficient degree of background blur, which can also be understood as insufficient blurring capability at equivalent focal length. Electronic devices need algorithms to extract the foreground from the entire image and then blur the background.

[0080] However, due to the complex edge distribution and rich details of the foreground, the algorithm may not be able to accurately distinguish between the foreground and the background, and cannot accurately segment the foreground and the background, and the blurred image may have blurred edges.

[0081] The following example is described by taking a mobile phone as the electronic device and a hair as the edge of the foreground, but the example does not constitute a limitation on the embodiments of the present application.

[0082] For example, Figure 1A A schematic diagram of a scenario provided for an embodiment of the present application. Figure 1B and Figure 1C for Figure 1A The schematic diagram of the interface of the electronic device corresponding to the scene shown in FIG. Figure 1A The scene shown may include a person in the foreground and the sun and sun rays in the background. At least one hair region may be displayed around the head of the person, such as hair region 101 and hair region 102.

[0083] In response to the camera opening operation, the electronic device may display Figure 1BThe interface shown may be a preview interface in the portrait photography function. The interface may include one or more of the following: a photography control 103, a preview screen, a control for switching cameras, or a control for indicating the use of the photography function. The camera application may also include: other modes besides the portrait photography mode, such as supporting aperture photography mode, night scene mode, video recording mode, short video mode, and HDR mode.

[0084] The preview image 104 displayed in the preview screen can be obtained by the electronic device identifying the foreground and background of the shooting screen and blurring the identified background. In a possible implementation, the preview screen may not display the preview image after blurring, which is not limited in the embodiments of the present application.

[0085] In response to the user's triggering operation on the photo control 103, the electronic device Figure 1A Take a photo of the scene shown in the figure and obtain the following Figure 1C The photographed image 105 in the interface shown. The photographed image 105 can be obtained by the electronic device identifying the foreground and background of the photographed image and blurring the identified background.

[0086] The photographed image 105 may include: a clear hair region 101', a blurred hair region 102', and a clear partial background region 106, etc. Figure 1A The scenes and Figure 1C In the photographed image shown, the hair region 101 may be part of the foreground region, so the hair region 101' may be a clear region; the hair region 102 may be part of the foreground, and due to the electronic device's misidentification of the foreground, the hair region 102 is mistaken for the background, so that the hair region 102' is blurred. In some embodiments, the sun and its rays may be the background, and due to the electronic device's misidentification of the background, part of the sun's rays are mistaken for the foreground, so that part of the background region 106 is not blurred.

[0087] It is understandable that, due to the limited ability of electronic devices to cut out the edges of the foreground, the image after blurring may have blurred edges, affecting the clarity of the image obtained by the photo.

[0088] In view of this, an embodiment of the present application provides an image processing method and a related device. The electronic device can add noise to the transition area in the image after background processing, and remove the noise under the constraint of the first condition through a diffusion model to obtain a processed image. Among them, the first condition is used to indicate the quality of the processed image. It can be understood that the transition area is the transition area between the foreground area and the background area, and can also be understood as the area where the foreground and the background are mixed. The transition area may include the edge of the foreground. Taking an image containing a portrait as an example, the transition area may include a hair area.

[0089] In this way, the transition area in the image is denoised according to the image quality indicated by the first condition, so that the resolution of the transition area in the image meets the quality requirements of the image, and thus the edge of the foreground in the transition area in the image is clear, which can reduce the edge blur and edge false blur caused by the segmentation of foreground and background in the image after background processing.

[0090] For example, FIG. 2A to FIG. 2F This is a schematic diagram of an interface of an electronic device in another scenario provided by an embodiment of the present application. Taking the images saved in the gallery application as an example, Figure 2A The interface shown is a gallery interface. The gallery interface may include at least one thumbnail. The at least one thumbnail may be a thumbnail of an image or a thumbnail of a video, which is not specifically limited here.

[0091] When the electronic device receives a trigger operation such as a click or a touch on the image thumbnail 201, the electronic device enters a state such as Figure 2B The image interface shown. The image interface includes: an image 202 corresponding to a thumbnail 201 and setting items. The setting items include but are not limited to: share, favorite, edit, delete, more 203 or other types of editing options.

[0092] When the electronic device receives a trigger operation (eg, a click operation) for more 203, the electronic device displays more editing items and enters Figure 2C The interface shown in FIG. The interface displays editing items such as blur 204, background change 205, and details.

[0093] In some embodiments, "blurring 204" and / or "changing background 205" can also be set in Figure 2B The interface shown in the figure. In this way, it is convenient for users to edit images, the operation is more convenient, and the user experience is improved.

[0094] When the electronic device receives a trigger operation such as a click or a touch for the virtualization 204, the electronic device enters Figure 2DThe interface shown in the figure includes: an adjustment bar 206, a slider 207 and an image 202. When the electronic device receives a sliding operation on the slider 207, the electronic device determines the background blur ratio in the image 202 according to the position of the slider 207 on the adjustment bar 206. The electronic device blurs the background in the image 202 through the image processing method provided in the embodiment of the present application, adds noise to the transition area in the blurred image, performs denoising through the diffusion model, and enters Figure 2E The interface shown in FIG.

[0095] In some embodiments, the electronic device may not display Figure 2D In the interface shown, when the electronic device receives a trigger operation such as a click or touch for blur 204, the electronic device blurs the background in image 202 through the image processing method provided in the embodiment of the present application, adds noise to the transition area in the blurred image, and performs denoising through a diffusion model to obtain and display the target image. In some embodiments, Figure 2C or Figure 2D The interface shown may also include a control for inputting text prompts, and the electronic device displays the text and other content input by the user in the control in response to the user's input operation. The electronic device can then perform image processing based on the content so that the processed image is consistent with the content.

[0096] When the electronic device receives a trigger operation such as a click or a touch for changing the background 205, the electronic device enters Figure 2F The interface shown in the figure has multiple background selection items and a target image 209. Multiple background selection items are used to provide different backgrounds. Multiple background selection items may include: no background, background 1, background 2, background 3, background 4 or more background selection items. No specific limitation is made here. The target image 209 is an image obtained by replacing the background in the image 202 by the electronic device through the image processing method provided by the embodiment of the present application, adding noise to the transition zone in the image after the background is replaced, and performing denoising processing through a diffusion model.

[0097] The image processing method provided in the embodiment of the present application can also be applied to other scenarios. For example, other scenarios may include: switching of backgrounds in images in applications such as wallpapers and desktops. For example, when an electronic device displays wallpaper, the background in a certain image can be switched randomly or in a preset order and the image after the background switching is updated. The application scenarios of the image processing method provided in the embodiment of the present application are not specifically limited.

[0098] In order to better understand the embodiments of the present application, the structure of the electronic device of the embodiments of the present application is introduced below. Figure 3A schematic diagram of the structure of an electronic device provided in an embodiment of the present application.

[0099] The electronic device may include: a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, an earphone interface 170D, a sensor module 180, a button 190, a motor 191, an indicator 192, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195, etc. The sensor module 180 may include a pressure sensor 180A, a gyroscope sensor 180B, an air pressure sensor 180C, a magnetic sensor 180D, an acceleration sensor 180E, a distance sensor 180F, a proximity light sensor 180G, a fingerprint sensor 180H, a temperature sensor 180J, a touch sensor 180K, an ambient light sensor 180L, a bone conduction sensor 180M, etc.

[0100] It is to be understood that the structure illustrated in the embodiment of the present invention does not constitute a specific limitation on the electronic device. In other embodiments of the present application, the electronic device may include more or fewer components than shown in the figure, or combine certain components, or split certain components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.

[0101] The processor 110 may include one or more processing units. Among them, different processing units may be independent devices or integrated into one or more processors. A memory may also be provided in the processor 110 for storing instructions and data. For example, the processor 110 is used to store instructions and data involved in the image processing method provided in the embodiment of the present application.

[0102] For example, in the embodiment of the present application, after the electronic device detects a trigger operation through the touch sensor 180K, the display screen of the electronic device can realize Figures 1A to 2F Switching display of the corresponding interface in. When the electronic device can detect the trigger operation for "blurring" through the touch sensor 180K, the processor can use the image processing method provided in the embodiment of the present application to achieve background blurring to obtain the target image. When the electronic device can detect the trigger operation for "changing background" through the touch sensor 180K, the processor 110 can use the image processing method provided in the embodiment of the present application to achieve background replacement to obtain the target image.

[0103] The software system of the electronic device can adopt a layered architecture, an event-driven architecture, a micro-kernel architecture, a microservice architecture, or a cloud architecture, etc., which will not be elaborated here.

[0104] For example, Figure 4 The software structure diagram of an electronic device provided in an embodiment of the present application is shown in FIG. Figure 4 As shown, the layered architecture divides the software into several layers, each with clear roles and division of labor. The layers communicate with each other through software interfaces. In some embodiments, the Android system is divided into four layers, from top to bottom, namely, the application layer, the application framework layer, the Android runtime (Android runtime) and the system library, and the kernel layer.

[0105] The application layer may include a series of application packages. In the embodiment of the present application, the application package may include: a camera, a gallery, etc. The camera may realize the shooting of images and the display of photographed images. The gallery may also be called an album, etc., and the gallery may realize the storage and access of photographed images.

[0106] In some embodiments, the application package includes: an image processing plug-in. The image processing plug-in may include operating codes and data for implementing the image processing method provided in the embodiments of the present application.

[0107] Exemplarily, the image processing plug-in may include: a depth calculation module, a hair cutout module, and a hair optimization module. The depth calculation module is used to obtain a blurred image with a clear foreground and a blurred background by blurring the original image; or to replace the background of the original image to obtain an image after the background is replaced; or to remove the background of the original image to obtain an image after the background is removed. The hair cutout module is used to obtain a mask image containing hair and a ternary image containing hair. The hair optimization module is used to use the blurred image, the diffusion model, and the ternary image to obtain a target image with clear hair; or the hair optimization module is used to use the image after the background is replaced, the diffusion model, and the ternary image to obtain a target image with clear hair; or the hair optimization module is used to use the image after the background is removed, the diffusion model, and the ternary image to obtain a target image with clear hair.

[0108] Among them, the implementation details of the depth calculation module can be found in the corresponding description below (for example, the description in S502), the implementation details of the hair cutout module can be found in the corresponding description below (for example, the description in S504), and the implementation details of the hair optimization module can be found in the corresponding description below (for example, the description in S506 to S508), which will not be repeated here.

[0109] The application framework layer provides an application programming interface (API) and a programming framework for the application of the application layer. The application framework layer includes some predefined functions. In an embodiment of the present application, the application framework layer may include a camera access interface, wherein the camera access interface may include camera management and camera devices. The camera access interface is used to provide an application programming interface and a programming framework for camera applications.

[0110] The hardware abstraction layer is an interface layer between the application framework layer and the driver layer, and provides a virtual hardware platform for the operating system. In the embodiment of the present application, the hardware abstraction layer may include a camera hardware abstraction layer and a camera algorithm library.

[0111] Among them, the camera hardware abstraction layer can provide virtual hardware of camera device 1, camera device 2 or more camera devices. The camera algorithm library may include running code and data for implementing the image processing method provided in the embodiment of the present application. Exemplarily, the camera algorithm library may include: a depth calculation module, a hair cutout module, and a hair optimization module. The depth calculation module, the hair cutout module, and the hair optimization module can refer to the above corresponding descriptions.

[0112] The depth calculation module is used to obtain a blurred image with a clear foreground and a blurred background by blurring the original image, or to replace the background of the original image to obtain an image after the background is replaced. The hair cutout module is used to obtain a mask image containing hair and a ternary image containing hair. The hair optimization module is used to obtain a target image with clear hair using the blurred image, the mask image and the ternary image; or the hair optimization module is used to obtain a target image with clear hair using the image after the background is replaced, the mask image and the ternary image.

[0113] The driver layer is a layer between hardware and software. The driver layer includes drivers for various hardware. The driver layer may include camera device drivers, digital signal processor drivers, and image processor drivers.

[0114] The camera device driver is used to drive the camera sensor to collect images and drive the image signal processor to pre-process the images. The digital signal processor driver is used to drive the digital signal processor to process images. The image processor driver is used to drive the graphics processor to process images.

[0115] The following is a detailed description of the image processing method in the embodiment of the present application in combination with the above system structure:

[0116] Taking the photo-taking process as an example, the electronic device responds to the user's operation of opening the camera application, such as clicking the camera application icon, and the camera application calls the camera access interface of the application framework layer to start the camera application, and then sends an instruction to start the camera by calling the camera device (camera device 1 and / or other camera devices) in the camera hardware abstraction layer. The camera hardware abstraction layer sends the instruction to the camera device driver of the kernel layer. The camera device driver can start the corresponding camera sensor and collect image light signals through the sensor. A camera device in the camera hardware abstraction layer corresponds to a camera sensor in the hardware layer.

[0117] Then, the camera sensor can transmit the collected image light signal to the image signal processor for preprocessing to obtain the image electrical signal (raw image), and transmit the above raw image to the camera hardware abstraction layer through the camera device driver.

[0118] The camera hardware abstraction layer can send the original image to the camera algorithm library. The camera algorithm library stores the program code for implementing the image processing method provided in the embodiment of the present application. Based on the digital signal processor and the image processor, the camera algorithm library executes the above code to implement the process of generating the target image in the image processing method described in the embodiment of the present application.

[0119] The camera algorithm library can send the raw image captured by the camera to the camera hardware abstraction layer, which can then display it.

[0120] Taking image editing in a gallery application as an example, in response to the user's operation on the "blur" control or the "change background" control, the electronic device calls the image processing plug-in in the gallery application, and implements the process of generating the target image in the image processing method described in the embodiment of the present application through the image processing plug-in.

[0121] It should be understood that the software architecture provided in the embodiments of the present application is merely an example and does not constitute a limitation on the embodiments of the present application.

[0122] For example, Figure 5 A schematic diagram of a flow chart of an image processing method provided in an embodiment of the present application. Figure 5 As shown, the process includes:

[0123] S501: Obtain an image to be processed.

[0124] The image to be processed may be any image, for example, an image saved in a gallery application, an image captured by a camera, an image saved in a wallpaper application, etc. No specific limitation is made here.

[0125] In some embodiments, in response to the photo-taking operation, the camera in the electronic device captures an image or pre-processes the captured image. The electronic device processes the image captured by the camera or the pre-processed image as the image to be processed and executes S502-S508. Figure 4 The software structure shown is taken as an example. In response to the photo-taking operation, the camera algorithm library of the electronic device can obtain the image taken by the camera from the camera hardware abstraction layer, or the image after the taken image is pre-processed. The camera algorithm library executes S502-S508.

[0126] Responding to a photo operation includes: responding to a user's triggering operation on a photo control in a portrait photo function, or responding to a user's triggering operation on a photo control in an HDR photo function, or responding to a user's triggering operation on a photo control in a large aperture photo function, or responding to a user's triggering operation on a recording control in a video recording function, etc. The applicable scenarios of the image processing method are not limited in the embodiments of the present application.

[0127] In some other embodiments, in response to a trigger operation on a "blur" control or a "change background" control, the electronic device processes the image displayed on the display screen as an image to be processed and executes S502-S508. Figure 2C Taking the interface shown as an example, when the electronic device detects a trigger operation for “blurring 204 ” or “changing background 205 ”, it processes the image 202 as an image to be processed and executes S502 - S508 .

[0128] For example, Figure 4 Taking the software structure shown as an example, the image processing plug-in in the application layer obtains the image to be processed. In response to the "blur" control or the "change background" control, the image processing plug-in of the electronic device processes the image displayed on the display screen and executes S502-S508. When the electronic device detects that it is in an idle scene, the image processing plug-in can obtain the image to be processed from the gallery application for image processing and execute S502-S508. The embodiment of the present application does not specifically limit the triggering method of image processing.

[0129] S502 : Perform depth calculation on the image to be processed to obtain a depth image.

[0130] The depth image may include the depth information of any pixel, which may indicate the distance from each point in the scene to the camera plane and may reflect the geometric shape of the visible surface in the scene. The depth information of any pixel in the depth image may be determined based on monocular depth estimation, binocular depth estimation, or depth estimation based on deep learning, which is not limited in the embodiments of the present application.

[0131] In some embodiments, the electronic device can distinguish the foreground and the background based on the focus point and the depth image. For example, the depth at the focus point can be a first depth, the depth of field at the focus point can be set to 0, and the depth of field at other positions in the depth of field image can be the difference between the depth at the other positions and the first depth, to obtain the foreground and the background. The foreground can include a clear range before the focus point, and the background can include a clear range after the focus point.

[0132] The focus point may be determined by the electronic device based on the portrait or object captured in the image to be processed. For example, in the portrait shooting function of the camera, the electronic device may detect the captured object. For example, when it is detected that the captured object includes a portrait, the position where the portrait is located is set as the focus point to complete the focus on the portrait, so that the portrait is clearly visible in the picture.

[0133] S503: Obtain a mask image A corresponding to the image to be processed and a ternary image A corresponding to the image to be processed.

[0134] In the embodiment of the present application, there is a corresponding relationship between the pixel points in the mask image A and the pixel points in the image to be processed, and there is also a corresponding relationship between the pixel points in the ternary image A and the pixel points in the image to be processed.

[0135] The electronic device may obtain the mask image A and the ternary image A through a neural network or in any other manner. Exemplarily, the electronic device (e.g., a camera algorithm library or an image plug-in module) may be pre-set with the neural network A. The electronic device (e.g., a camera algorithm library, an image plug-in module) may input the image to be processed into the neural network A, and the neural network A may output the mask image A and the ternary image A.

[0136] In some embodiments, neural network A may include one or more of the following modules: a shared editor, a semantic segmentation branch, a detail prediction branch, or a fusion branch. Specifically, the electronic device may input the image to be processed into neural network A, and the shared editor may obtain the image features corresponding to the image to be processed, and input the image features corresponding to the image to be processed into the semantic segmentation branch to obtain the portrait foreground. The camera algorithm library obtains the Laplace high-frequency map corresponding to the image to be processed, and inputs the Laplace high-frequency map corresponding to the image to be processed into the detail prediction molecule to obtain the hair details in the transition area. The camera algorithm library inputs the portrait foreground and the hair details in the transition area into the fusion branch to obtain the mask image A and the ternary image A. Among them, the mask image A may include the portrait foreground, the ternary image A may include the transition area, and the transition area may include the hair area. Among them, the training process of neural network A can be referred to. Figure 7 The corresponding embodiments will not be described in detail here.

[0137] S504 : Use the depth image and the mask image A to perform blurring processing on the image to be processed to obtain a blurred image A.

[0138] The depth image is used to indicate the degree of blur in the image to be processed. The degree of blur is related to the depth information of each pixel in the depth image. For example, the greater the depth corresponding to the pixel, the blurrier it is; the smaller the depth corresponding to the pixel, the clearer it is.

[0139] The blurred image A may include a clear foreground and a blurred background. Figure 1A As shown, after blurring the image to be processed, a blurred image with a clear foreground of the portrait and a blurred background can be obtained. The blurred image can be the image displayed in the interface shown in Figure C.

[0140] The blurring method may include: Gaussian blurring, or blurring based on a neural network, etc., which is not limited in the embodiments of the present application. For example, an example of constructing a blurred image based on Gaussian blurring is given: the camera algorithm library can perform Gaussian blurring with random blur intensity on the image to be processed to obtain a Gaussian blur map, and then extract a clear portrait foreground from the image to be processed through the position of the portrait foreground in the mask map A, and replace it with the above-mentioned Gaussian blur map to obtain image a. The image a can be a blurred image A. The degree of blur is related to the depth information of each pixel in the depth image. For example, the greater the depth corresponding to the pixel, the blurrier it is; the smaller the depth corresponding to the pixel, the clearer it is.

[0141] In some embodiments, the electronic device may optimize the hair region in the image a using the image to be processed, the mask image A, and the ternary image A to obtain a blurred image A. The mask image A may refer to the above corresponding description and will not be described again here.

[0142] Specifically, the electronic device can obtain the area A corresponding to the portrait foreground from the image to be processed based on the mask image A, and fuse the area A into the image a (or understand that the area A is covered into the image a) to obtain the blurred image A. It is necessary to ensure that the edge of the portrait foreground in the mask image A can be located in the transition area of ​​the ternary image A. Exemplarily, the electronic device can also obtain the blurred image A based on the generative network. The generative network can be a generative adversarial network (GAN), and the generative network includes at least one generator for generating hair, and at least one discriminator for judging whether the hair is real or fake. The embodiment of the present application does not specifically limit the implementation details in the generative network.

[0143] In some embodiments, the electronic device may use the depth image to perform blur processing on the image to be processed to obtain a blurred image A. Specifically, the electronic device may perform blur processing on the depth area behind the focus point to obtain a blurred image A. Alternatively, the electronic device may perform blur processing on the image to be processed to obtain a blurred image, and then extract a clear foreground from the image to be processed according to the position of the foreground in the depth image, and replace it with the blurred image to obtain a blurred image A.

[0144] S505 , performing cropping processing on the blurred image A and the ternary image A to obtain a cropped blurred image B and a cropped ternary image B.

[0145] The cropped blurred image A includes: a hair region. The cropped ternary image B includes: a hair region. In this way, cropping the blurred image A can reduce the amount of calculation for subsequent image processing and improve the image processing speed.

[0146] In some embodiments, the electronic device may not execute S505.

[0147] S506 , down-sampling the blurred image B and the ternary image B to obtain a blurred image C and a ternary image C.

[0148] The size of the blurred image C is smaller than the preset size. In this way, the subsequent processing of the blurred image C is facilitated, and the situation that the blurred image cannot be processed due to the large amount of calculation caused by the blurred image being too large in size is reduced.

[0149] The preset size may be 512 pixels*512 pixels or any value, and is not specifically limited here.

[0150] In some embodiments, when the size of the blurred image B is smaller than or equal to a preset size, the electronic device may not execute S506.

[0151] It is understandable that if the electronic device does not execute S505 , the blurred image B in S506 is the blurred image A.

[0152] S507 , inputting the ternary image B and the blurred image C into the neural network B, optimizing the hair region in the blurred image C, and obtaining the blurred image D.

[0153] Specifically, the neural network B can use the ternary map B to determine the hair region in the blurred image C, and add noise to the hair region in the blurred image C. The image after adding noise is input into the diffusion model in the neural network B to remove the noise in the image after adding noise under the constraint of the first condition, so as to obtain the blurred image D. The first condition is used to indicate the quality of the image after being processed by the diffusion model.

[0154] In this way, the neural network B can perform denoising on the transition area in the image according to the image quality indicated by the first condition, so that the resolution of the transition area in the image meets the quality requirements of the image, and thus the edge of the foreground in the transition area in the image is clear.

[0155] In some embodiments, the diffusion model in the neural network B is a model for processing specific objects, which is adaptive and the first condition remains unchanged. The specific objects may include: portraits, wool, etc., which are not specifically limited here. In this way, the quality of the processed image can meet the detail complexity requirements of the specific object, and the edge of the foreground in the transition area is clear.

[0156] In some other embodiments, the quality of the processed image indicated by the first condition is positively correlated with the detail complexity of the transition region in the first image. For example, the higher the detail complexity of the foreground edge, the higher the quality of the processed image. In this way, the first model can adjust the quality of the processed image according to the detail complexity of the foreground edge. The quality of the transition region in the processed image can meet the detail complexity requirement of the transition region, and thus the edge of the foreground in the transition region is clear.

[0157] The first condition in the diffusion model can be fixed. For example, when the diffusion model is used to process a model with high detail complexity such as hair, the quality of the processed image indicated by the first condition is higher; when the first model is used to process a model with low detail complexity such as apples, the quality of the processed image indicated by the first condition is lower.

[0158] The diffusion model may include a stable diffusion image generation algorithm, or other types of algorithms, which are not specifically limited here. The diffusion model may perform image-to-image region redrawing so that the redrawn region (the region where noise is added) in the image satisfies the constraint condition. Taking the constraint condition including the first condition as an example, the redrawn region in the image processed by the diffusion model satisfies the requirements indicated by the first condition, such as the resolution requirement of the image, the quality requirement of the image, etc.

[0159] The constraint condition can be derived from the image input to the diffusion model, or from the text feature input to the diffusion model, or any other form of constraint. No specific limitation is given here. The method for obtaining the text feature can refer to the following Figure 6 The corresponding description in is not repeated here.

[0160] It is understandable that the addition of noise can blur (or destroy) the features of the image. When the subsequent diffusion model performs image denoising, it can regenerate the area where the noise is added in the image based on the object generation ability learned during training (for example, when training with portrait images, the portrait generation ability is learned), so that the area meets the features of the object learned during training (portrait features). For example, if the transition area is the hair area of ​​the portrait, the hair in the transition area of ​​the image processed by the diffusion model is natural and realistic.

[0161] It can also be understood that when using portrait images for training, the trained diffusion model has the ability to generate images with real and natural portrait effects; or it can be understood as having the ability to remove noise from noise images of different degrees and generate images with real and natural portrait effects.

[0162] In some embodiments, the diffusion model can use the areas in the image where no noise is added (e.g., the foreground area and the background area in the image) as the strong constraint conditions of the whole image, and the areas where noise is added (e.g., the transition area) as the weak constraint conditions of the redrawing area, so that the image processed by the diffusion model has a real and natural effect. When noise is added to the transition area in the image, the transition area in the image generated by the diffusion model has a real and natural effect.

[0163] Exemplarily, the electronic device (e.g., camera algorithm library, image plug-in module) may be pre-set with a neural network B. The electronic device (e.g., camera algorithm library, image plug-in module) may input the blurred image C and the ternary map B into the neural network B, and the neural network B may output the blurred image D.

[0164] In some embodiments, the electronic device can compress the blurred image C from the pixel space to the latent space (also referred to as the latent space), add noise to the hair region in the blurred image C in the latent space, and remove the noise in the image after adding the noise under the constraint of the first condition. In this way, compared with the optimization in the pixel space, the latent space has a smaller amount of calculation and a faster operation speed. In addition, the latent space has a smaller amount of calculation, which can reduce the hardware requirements of the electronic device, so that the electronic device can implement the image optimization process provided in the embodiment of the present application.

[0165] In some embodiments, the first condition may include: inputting text features of the diffusion model. The method for obtaining the text features may refer to the following Figure 6 The corresponding description in is not repeated here.

[0166] For example, Figure 6 A schematic diagram of the structure of a neural network B provided in an embodiment of the present application is shown in FIG. Figure 6As shown, the neural network B may include one or more of the following modules: dVAE encoder, diffusion model, text encoder, dVAE decoder, etc.

[0167] Specifically, the electronic device can input the blurred image C into the neural network B, and the dVAE encoder can encode the blurred image C (which can also be understood as compressing the blurred image C into the latent space) to obtain a feature map A corresponding to the blurred image C.

[0168] The text encoder can encode the text prompt to obtain text features. The text features can be used as the first condition to constrain the denoising process of the subsequent diffusion model.

[0169] The text prompt may include: a positive prompt word. The positive prompt word is used to indicate that the processed image meets the requirements indicated by the positive prompt word and has what characteristics.

[0170] In the embodiment of the present application, the text prompt includes: a positive prompt word (prompt) for indicating the quality of the image. For example, best quality, master work, masterpiece, etc. Taking the best quality as an example, the processed image has the characteristics of high clarity, high contrast, accurate color reproduction, and rich details.

[0171] It is understandable that image quality is related to one or more of the following parameters: image resolution, authenticity, contrast, etc. The text prompt may include one or more of the following positive prompt words: a positive prompt word for indicating the resolution of the processed image, a positive prompt word for indicating the authenticity of the image, etc.

[0172] Positive prompt words used to describe image resolution may include: ultra-high resolution, ultra-detail, etc. Taking ultra-high resolution as an example, the processed image has a high resolution and can present subtle textures, colors and details.

[0173] Positive prompt words used to describe realism may include: realism (1.4), real texture, etc. Taking real texture as an example, the processed image has a high degree of similarity to reality. It can be understood that realism is used to describe the realism and simulation of an image, and can also be understood as the degree of similarity between an image and reality. The higher the realism, the higher the degree of similarity between the image and reality. The lighting, shadows, textures, perspective, colors, etc. in the image may affect the realism of the image. Realism can also be replaced by words such as real texture, and the embodiments of the present application do not specifically limit the prompt words that are close or similar.

[0174] In some embodiments, the text prompt may also include: positive prompt words for indicating the content of the image, for example: including: 1 girl, long-haired girl, etc. prompt words for describing objects in the image. The text feature is also used to indicate the content of the blurred image C. The first condition may also constrain the content of the processed image. In this way, the subsequent diffusion model can perform noise prediction on the image according to the expected content of the image and remove the predicted noise, so that the blurred image C is consistent with expectations.

[0175] In some embodiments, the text prompt may further include: a positive prompt word for indicating the image style. For example, it may include: a bright style, a dark style, etc. The text feature is also used to indicate the style of the blurred image C. The first condition may also constrain the style of the processed image. In this way, the subsequent diffusion model may perform noise prediction on the image according to the expected image style and remove the predicted noise, so that the blurred image C is consistent with the expected image style.

[0176] In some embodiments, the text prompt may also include: negative prompt words. Negative prompt words are used to indicate which images are not generated, and can also be understood as which requirements the processed images do not meet or which features they do not have. Negative prompt words may be "poor quality, low resolution, black and white", etc. In this way, constraining the images processed by the diffusion model from another perspective can reduce the situations where the processed images meet the negative prompt words, and further improve the image processing effect.

[0177] In the embodiment of the present application, the text prompt may be pre-set or input by the user, and is not specifically limited here.

[0178] For example, the pre-set text prompt may be "best quality, masterpiece, ultra-high resolution, authenticity (1.4), original photo" or "masterpiece, real texture, ultra-high resolution, ultra-detail", etc. No specific limitation is made here. In this way, the image processed by the subsequent diffusion model can meet the image quality requirements indicated by the text prompt, so that the edge of the foreground in the transition area of ​​the image is clear. In addition, no user input is required, which can reduce user operations.

[0179] Exemplarily, the text prompt is input by the user. Specifically, the interface of the electronic device may include: a control for inputting a text prompt, and the electronic device displays the text input by the user in response to the user's input operation. The electronic device may subsequently process the image based on the text as a text prompt, so that the processed image is consistent with the content described by the text. In this way, the text prompt is input by the user, and the image denoising process can be constrained according to the user's input, so that the image processed by the diffusion model meets the user's expectations.

[0180] The text encoder can be a text encoder based on self-supervised learning (contrastive language-image pretraining, CLIP) or any text encoder. The text encoder of CLIP can convert text into a digital representation containing the meaning of the text, and convert the digital representation into a feature representation (text feature) containing the semantic information of the text, so as to facilitate the subsequent diffusion model to denoise the image.

[0181] Neural network B can add noise to the features in feature map A corresponding to the transition area in blurred image C according to ternary map A, and obtain feature map B corresponding to blurred image C. It can be understood that the features corresponding to the transition area in feature map A are different from the features corresponding to the transition area in feature map B; the features corresponding to the area other than the transition area in feature map A are the same as the features corresponding to the area other than the transition area in feature map B.

[0182] The diffusion model can predict the noise of feature map B under the constraint of text features, and remove the predicted noise in feature map B to obtain the processed feature map C. The dVAE decoder decodes feature map C (which can also be understood as decompressing it to pixel space) to obtain the blurred image D.

[0183] It should be noted that the diffusion model is a generative model. The diffusion model can repair the artifacts caused by the foreground-background segmentation in the transition area, so that the foreground edge is clear. Artifacts refer to unnatural, abnormal traces, areas or flaws in the image that can be seen as artificial processing.

[0184] In the embodiment of the present application, since noise is added to the transition region in the feature map B, the diffusion model can denoise the transition region in the feature map B under the constraints of the features of the region other than the transition region in the feature map B and the text features (first condition), and regenerate the transition region so that the foreground edge is clear. It can also be understood that the diffusion model can redraw the transition region in the feature map B under the constraints of the features of the region other than the transition region in the feature map B and the text features (first condition) to repair the artifacts in the transition region so that the foreground edge is clear.

[0185] in, Figure 6 The training process of the diffusion model in the neural network B shown can be seen below Figure 8 The corresponding embodiments will not be described in detail here.

[0186] In some embodiments, the diffusion model is trained by adding Gaussian noise to the image, and adaptively, the added noise is Gaussian noise before removing the noise in the image through the diffusion model.

[0187] In the embodiment of the present application, the neural network B may include: dVAE decoders corresponding to multiple scales. The dVAE decoders corresponding to multiple scales decode the same feature map to obtain images of different sizes.

[0188] In an embodiment of the present application, the electronic device may confirm the decoder used for the decoding process based on the size of the blurred image B and the size of the blurred image C, so that the size of the blurred image D is the same as or similar to the size of the blurred image B. Exemplarily, taking the neural network B as an example, which may include: a 1X dVAE decoder, a 2X dVAE decoder and a 4X dVAE decoder, when the electronic device does not downsample the blurred image B to reduce the size, a 1X dVAE decoder is used for decoding; when the ratio of the size of the blurred image B to the size of the blurred image C is greater than 1, and the ratio is less than or equal to 2, a 2X dVAE decoder is used for decoding; when the ratio of the size of the blurred image B to the size of the blurred image C is greater than 2, a 4X dVAE decoder is used for decoding. The training process of the dVAE decoders corresponding to multiple scales can refer to the following Fig. 9 The embodiments shown are not described in detail here. Figure 6 The training process of the dVAE decoder in the neural network B shown can be seen below Fig. 9 The corresponding embodiments will not be described in detail here.

[0189] It is understandable that if the electronic device does not execute S506, the blurred image C in S507 is the blurred image B, and the ternary image C is the ternary image B. If the electronic device does not execute S505 and S506, the blurred image C in S507 is the blurred image A, and the ternary image C is the ternary image A.

[0190] S508: Fusing the blurred image D with the blurred image A to obtain a target image.

[0191] In some embodiments, if the electronic device does not execute S505 and the size of the blurred image D is the same as the size of the blurred image A, the electronic device may not execute S508, and the target image is the blurred image D.

[0192] In this way, the processed transition area is fused with the blurred image A, so that the target image has a clear foreground and a blurred background. The hair in the hair area of ​​the target image is clear, and the false blur of the hair is reduced.

[0193] Above Figure 5In the illustrated embodiment, the transition region in the image is confirmed by a ternary graph, and noise is added to the region, so that the diffusion model predicts (denoises) the noise in the transition region, so that the edge of the foreground in the region is clear. In other embodiments, noise can be added to the entire image of the defocused image (or the foreground region and the transition region of the defocused image) and the noise prediction (denoise) can be performed by a diffusion model to obtain a defocused image D. The defocused image D and the defocused image A are fused to make the transition region in the image clear. During the fusion process, since the entire image of the defocused image D is a generated image, there is a difference between the defocused image D and the defocused image A (for example, there is a difference between the foreground in the defocused image D and the foreground of the defocused image A). During the fusion process, the region corresponding to the transition region in the defocused image D and the region outside the transition region in the defocused image A may not be aligned, resulting in misalignment and the like. In this method, the fusion process requires the region corresponding to the transition region in the defocused image D to be aligned with the region outside the transition region in the defocused image A, and the processing method is relatively complicated.

[0194] Understandably, Figure 5 In the manner shown, the transition area in the blurred image D is different from the transition area in the blurred image A, and the difference between the areas other than the transition area in the image (for example, the foreground area in the image) is small or no, and the fusion process is relatively simple. For example, the blurred image D can be overlaid on the corresponding area in the blurred image A. The manner is simple and easy to implement.

[0195] Based on this, the electronic device obtains the ternary map corresponding to the image to be processed, and then determines the transition area including the edge of the foreground through the ternary map. The electronic device can denoise the transition area corresponding to the edge in the image after background processing through a diffusion model, and fuse the denoised image with the background processed image to obtain the target image.

[0196] It is understandable that the above Figure 5 In the corresponding embodiment, the transition area is illustrated by taking the hair area in a portrait as an example. In possible implementations, the transition area may also include: the wool area in a sweater, or the hair area of ​​an animal, or the branch and leaf area of ​​a plant, etc., which is not limited in the embodiments of the present application.

[0197] Combine the following Figures 7 to 9 right Figure 5 The training process of the neural network A, diffusion model, and dVAE decoder in the shown embodiment is explained.

[0198] Figure 7 This is a schematic diagram of the architecture of a neural network A provided in an embodiment of the present application. Figure 7As shown, the neural network A can be a neural network based on supervised learning. The neural network A includes: a shared editor, a semantic segmentation branch, a detail prediction branch and a fusion branch.

[0199] The shared editor is used to extract image features, such as obtaining image features of training image A, where training image A may be a clear image. For example, the shared editor may use the HRNet48 model pre-trained on ImageNet.

[0200] The semantic segmentation branch is used to obtain the approximate foreground of the portrait in the training image A. For example, the semantic segmentation branch may be composed of five blocks, each of which is composed of three ConvBNReLUs and a bilinear upsampling module. The three ConvBNReLUs may include: convolution Conv, batch normalization (BN), and ReLU.

[0201] The detail prediction branch is used to predict high-frequency information of more edges in the training image A, such as obtaining high-precision hair details in the training image A. For example, the detail prediction branch can be composed of 3 residual blocks and a convolutional layer.

[0202] Specifically, the training device can extract details from the edge area in the training image A by using the Laplace edge detection method to obtain a Laplace high-frequency map. The image features output by the shared editor and the Laplace high-frequency map are sent to the detail prediction branch, and the detail prediction branch can predict a lot of high-frequency information of the edge, such as predicting the hair details in the training image A.

[0203] The fusion branch is used to fuse the portrait foreground output by the semantic segmentation branch and the hair details output by the detail prediction branch, and output the predicted mask map and the predicted ternary map.

[0204] Exemplarily, the training data of neural network A may include: training image A, mask map true value and ternary map true value. After the training device inputs the training image A into neural network A, neural network A may adjust the parameters in neural network A based on the difference between the predicted mask map and the mask map true value, and the difference between the predicted ternary map and the ternary map true value. Neural network A may also be referred to as a source model.

[0205] For example, when the difference between the predicted mask image and the true value of the mask image satisfies the loss function (loss) loss1, and the difference between the predicted ternary image and the true value of the ternary image satisfies loss2, a trained fusion branch or a trained target neural network is obtained. Loss1 may be the absolute value loss between the true value of the mask image and the predicted mask image; loss2 may be the cross entropy loss between the true value of the ternary image and the predicted ternary image.

[0206] The predicted mask image and the preset ternary image can be jointly determined based on the semantic segmentation branch, the detail prediction branch and the fusion branch.

[0207] In a possible implementation, since the semantic segmentation branch is relatively rough in the semantic segmentation of the image and can only obtain an approximate foreground of the portrait, while the edge of the true value of the mask image is relatively accurate, in order to enable the semantic segmentation branch to output an image that is consistent with the foreground contour in the true value of the mask image, the training device can perform loss4 calculation on the true value of the mask image and the output result of the semantic segmentation branch. For example, the training device can perform Gaussian blur processing on the true value of the mask image to obtain a Gaussian blur image of the true value edge of the mask image (or simply a mask edge blur image), and adjust the parameters in the semantic segmentation branch based on the difference between the mask edge blur image and the output result of the semantic segmentation branch, and complete the training of the semantic segmentation branch when the mask edge blur image and the output result of the semantic segmentation branch meet loss4. Among them, loss4 can be the structural similarity loss between the mask edge blur image and the output result of the semantic segmentation branch.

[0208] In a possible implementation, in order to enable the detail prediction branch to predict more details in the transition area containing the foreground edge, the training device can perform loss3 calculation on the true value of the ternary image and the output result of the detail prediction branch. For example, the training device can obtain the mask image (or simply referred to as the transition area mask image, or the third image) in the transition area of ​​the ternary image based on the true value of the mask image and the true value of the ternary image, adjust the parameters in the detail prediction branch by the difference between the transition area mask image and the output result of the detail prediction branch, and complete the training of the detail prediction branch when the transition area mask image and the output result of the detail prediction branch meet loss3. Among them, loss3 can be the L1 loss that limits the output result of the transition area mask image and the detail prediction branch. L1 loss is also called mean absolute error.

[0209] It is understandable that the electronic device can train a better semantic segmentation branch based on loss4, and / or train a better detail prediction branch based on loss4.

[0210] The fusion branch can combine the foreground contour in the blurred mask edge map output by the semantic segmentation branch with the clear edge in the clear feature map output by the detail prediction branch, and a predicted mask map can be obtained, and the predicted mask map can have clear edges. In addition, the fusion branch can combine the foreground contour in the blurred mask edge map output by the semantic segmentation branch with the transition area containing the edge in the clear feature map output by the detail prediction branch, and a predicted ternary map can be obtained, and the predicted ternary map can be marked with a transition area, and the transition area can contain edges. Edges can include: hair in a portrait, wool in a sweater, or animal hair, or plant branches and leaves, etc., which are not limited in the embodiments of the present application.

[0211] Figure 7 The training device described in the embodiment may be the electronic device described in the embodiment of the present application, or may be other devices or servers that can establish a connection with the electronic device, etc., which is not limited in the embodiment of the present application.

[0212] For example, Figure 8 A training diagram of a diffusion model in a neural network B provided in an embodiment of the present application. Figure 8 As shown in Figure 2, the training process of the diffusion model is as follows:

[0213] S801. Input the text prompt corresponding to the training image B into the text encoder to obtain the text features of the training image B.

[0214] The text features of the training image B can be used as a second condition to constrain the denoising process of the diffusion model, so that the image processed by the diffusion model is the same as or similar to the training image B. The text prompt can describe the image quality (e.g., clarity), content (e.g., style, object, object type), etc. of the training image B. It can also be understood that the text features of the training image B can be used as a second condition to guide the following diffusion model to generate the training image B from the noisy image (corresponding to S803 below).

[0215] The text prompt corresponding to the training image B may include: positive prompt words for indicating the quality of the training image B. The positive prompt words may include: best quality, masterpiece, ultra-high resolution (realism 1.4) and original photo, etc. The positive prompt words may also include: positive prompt words for describing the image content and / or image style, for example, movie lighting, 1girl, dark style, daytime and city (lipstick 1.3), etc. The embodiment of the present application does not limit the specific content of the text prompt.

[0216] S802. Input the training image B into the dVAE encoder to obtain a feature map of the training image B.

[0217] The training image B may be an image containing the target object. For example, if the target object is a portrait, the training image B may be an image containing the portrait. In this way, the processing effect of the diffusion model on the portrait can be improved.

[0218] S803: forward diffusion is performed on the feature map of the training image B to generate a series of noise images interfered by noise. Forward diffusion can be understood as a process of adding noise of different intensities to the feature map of the training image B, or can be understood as a process of gradually adding noise to the feature map of the training image B.

[0219] S804: Input the text features and the noise image corresponding to the training image B into the diffusion model to be trained to obtain a predicted feature map.

[0220] Specifically, the diffusion model to be trained can predict the noise image and remove the predicted noise (denoising process) under the constraint of the text feature corresponding to the training image B, and obtain the predicted feature map. When the difference between the predicted noise and the added noise satisfies the loss function loss5, the trained diffusion model is obtained. Among them, loss5 can include mean-square error (MSE) loss.

[0221] The process of generating a predicted feature map from the noise image in S804 may be referred to as reverse derivation (also referred to as reverse diffusion).

[0222] It can be understood that the reverse derivation process is opposite to the forward diffusion process. If the forward diffusion process in S803 adds noise to the feature map of the training image B in N steps, then in S804, the noise image after the noise is added in the Nth step is predicted and removed in N steps.

[0223] It can be understood that the output of the Mth step in the forward diffusion process corresponds to the input of the Mth-to-last step in the backward derivation process, and the output of the Mth-to-last step in the forward diffusion process corresponds to the input of the Mth-to-last step in the backward derivation process. The noise added in the Mth step in the forward diffusion process corresponds to the predicted noise of the Mth-to-last step in the backward derivation process. The noise added in the Mth step in the forward diffusion process corresponds to the predicted noise of the Mth-to-last step in the backward derivation process. The corresponding predicted noise and added noise satisfy the loss function loss5.

[0224] Exemplarily, taking the addition of noise 50 times as an example, the noise added in the first step of the forward diffusion process corresponds to the predicted noise of the first to last step (which can also be understood as the 50th step) in the reverse derivation process; the noise added in the 50th step of the forward diffusion process corresponds to the predicted noise of the 50th to last step (which can also be understood as the first step) in the reverse derivation process.

[0225] In some embodiments, the noise image used in the first step of reverse derivation in S804 may be the noise image output after adding noise in the last step of S803, or may be a random noise image, which is not specifically limited here. It is understandable that the noise image output after adding noise in the last step of S803 has serious noise interference and is similar to a random noise image, so a random noise image may also be used for training.

[0226] It is understandable that the above Figure 8 In the training process shown, S801, S802 and S803 can be understood as input preparation before the diffusion model training, and the above S801, S802 and S803 may not be executed when the diffusion model is trained. Exemplarily, the text features and the noise image corresponding to the training image B are input into the diffusion model to be trained to obtain a predicted feature map. When the diffusion model obtains the predicted feature map from the noise image, the difference between the noise predicted in the Mth step and the noise added in the Mth to last step satisfies the loss function loss5, and loss5 may include MSE loss.

[0227] In some embodiments, the diffusion model is provided with a denoising strength parameter. The denoising strength parameter is used to indicate the number of steps for the diffusion model to predict and remove noise, which can also be understood as the number of denoising times. When the diffusion model is used, denoising can be performed according to the denoising strength parameter. Figure 5 In the embodiment shown, the processing of feature map B is taken as an example, and noise prediction and removal (denoising) of feature map B can be performed according to the denoising strength parameter. Taking the diffusion model from the noise image to the image including 50 steps of denoising as an example, when the denoising strength parameter is set to 1, denoising is performed on feature map B for 50 steps starting from step 1; when the denoising strength parameter is set to 0.5, denoising is performed on feature map B for 25 steps starting from step 26; when the denoising strength parameter is set to 0, denoising is not performed on feature map B.

[0228] In some embodiments, the feature map B may be denoised in a local area according to the ternary map. For example, the denoising intensity parameter of the foreground area and the background area is 0, and the denoising intensity parameter of the transition area is 0.5.

[0229] Fig. 9 A training diagram of a dVAE decoder in a neural network B provided in an embodiment of the present application. Fig. 9 As shown, for the dVAE decoder corresponding to any scale, the training process of the dVAE decoder is as follows:

[0230] S901 . Downsample the training image C to obtain a training image D.

[0231] The training image C may be the same as or different from the training image B. There is no specific limitation here.

[0232] S902: Input the training image D into the dVAE encoder for encoding to obtain a feature map of the training image D.

[0233] S903. Input the feature map of the training image D into the dVAE decoder to obtain a predicted image.

[0234] S904: When the difference between the predicted image and the training image C satisfies the loss function loss6, a trained dVAE decoder is obtained. Loss6 may be a cross entropy loss between the predicted image and the training image C.

[0235] Above Fig. 9 In the example shown, the training sample includes: training image C. The training image D is obtained by downsampling the training image C. In some embodiments, the training sample includes: training image C and training image D. When training the dVAE decoder, S901 may not be performed.

[0236] In some embodiments, when training the 1X dVAE decoder, the training device may not execute S901, and the training image D in S902 and S903 may be the training image C.

[0237] In some embodiments, before a decoder is trained, parameter settings of other decoders may be reused. In this way, reusing some parameters can reduce the amount of calculation of the decoder and shorten the training time of the decoder.

[0238] For example, the decoder includes: 1X dVAE decoder, 2X dVAE decoder and 4X dVAE decoder, such as Fig.10 As shown, the training device can first train the 1X dVAE decoder; initialize the 2X dVAE decoder with the parameters of the trained 1X dVAE decoder, and then train the 2X dVAE decoder; initialize the 4X dVAE decoder with the parameters of the trained 2X dVAE decoder, and then train the 4X dVAE decoder. In this way, reusing some parameters can reduce the training time of the 2X dVAE decoder and the 4X dVAE decoder.

[0239] Figures 8 to 10 The training device described in the embodiment may be the electronic device described in the embodiment of the present application, or may be other devices or servers that can establish a connection with the electronic device, etc., which is not limited in the embodiment of the present application.

[0240] On the basis of the above-mentioned embodiment, the embodiment of the present application further provides a method for generating a ternary graph, so that the transition area in the ternary graph can contain edges.

[0241] For example, Fig.11 A schematic diagram of the steps for generating a ternary graph provided in an embodiment of the present application. Fig.11 In the corresponding embodiment, the edge is taken as a hair for illustration, and this example does not constitute a limitation on the embodiments of the present application.

[0242] Understandably, Fig.11 The method for generating the ternary image described in the above can be used to generate the true value of the ternary image in the first neural network. For example, based on the training image and the true value of the mask image, the process of generating the true value of the ternary image can be referred to as follows Fig.11 Description in .

[0243] like Fig.11 As shown, the method for generating a ternary graph may include the following steps:

[0244] S1101. The electronic device obtains a first hair region in a training image based on a semantic segmentation method.

[0245] For example, the electronic device can perform Parsing-Net deep learning network calculation on the training image to obtain an approximate hair distribution area to obtain a first hair area. The first hair area can also be referred to as a second area.

[0246] Among them, Parsing-Net is a multi-classification deep learning method based on semantic segmentation, which can classify and annotate different areas of the human body, such as hair, face, hands, torso, and legs. The annotation results obtained by this method are usually of low accuracy. The embodiment of the present application does not specifically limit the method for obtaining the approximate hair distribution area.

[0247] S1102: The electronic device performs connected domain screening on the first hair region to remove noise, to obtain a second hair region.

[0248] Connected domain screening is used to find pixels with the same or similar color values ​​in an image and combine them into a continuous region.

[0249] For example, the electronic device may obtain at least one connected domain from the first hair region, and remove the connected domain whose area is smaller than the first value from the at least one connected domain to obtain the second hair region. The second hair region may also be referred to as the third region, and the first value may be 1 / 2500 of the area of ​​the training image, etc.

[0250] It is understandable that since the output results of the Parsing-Net method are of low accuracy and contain noise, the noise areas with an area smaller than 1 / 2500 of the training image area can be removed through connected domain screening to improve the accuracy of hair area recognition.

[0251] S1103: The electronic device expands the second hairline region to obtain a third hairline region.

[0252] The electronic device can perform 12 circular dilations with a core size of 6×6 on the second hairline region to obtain a third hairline region. The third hairline region can also be called a fourth region.

[0253] It is understandable that in order to improve the accuracy of hair region recognition and avoid the presence of unmarked hair regions in the output results of the Parsing-Net method, the electronic device can perform expansion processing so that the expanded hair region can cover all hair details. In this embodiment of the application, the number of expansions, the shape of the expansion, and the size of the expansion are not limited.

[0254] S1104: The electronic device dilates the transition region in the true value of the mask image to obtain a second mask image.

[0255] The true value of the mask image may be determined based on a target neural network, or the true value of the mask image may be determined based on one or more methods such as manual annotation, data synthesis, or computer graphics (CG) synthesis.

[0256] The electronic device may perform a circular expansion with a kernel size of 30×30 on the transition area in the true value of the mask image once to obtain a second mask area. The second mask area may also be referred to as an expanded mask image. In the embodiment of the present application, the number of expansions, the expanded shape, and the expanded size are not limited.

[0257] S1105: The electronic device takes an intersection of the second mask area and the third hair area to obtain a transition area in the ternary graph.

[0258] It is understandable that, since the transition area of ​​the mask image can include the whole body, it is necessary to intersect the second mask image with the third hair region, and perform a connected domain screening again to remove noise regions with an area smaller than 1 / 20 of the entire image area, so as to remove the transition area distributed in the non-hair region and obtain the transition area in the ternary image. The transition area in the ternary image can also be called the first transition area.

[0259] S1106. The electronic device sets the alpha=255 area in the true value of the mask image in the foreground of the ternary image to obtain the true value of the ternary image.

[0260] The electronic device can fuse the alpha=255 region and the third hair region in the mask image true value to obtain the ternary image true value

[0261] Based on this, the electronic device can obtain the true value of the ternary image with the transition area restricted to the hair position by fusing the mask area and the hair area.

[0262] In possible implementations, the embodiments of the present application may also be based on Fig.11 The ternary image generation method described in the article obtains the ternary image corresponding to the clear image. The specific process is the same as Fig.11 The same is true in , so I will not repeat it here.

[0263] For example, Fig.12 A schematic diagram of the structure of an image processing method provided in an embodiment of the present application. Fig.12 As shown, the method includes:

[0264] S1201. Acquire a first image; wherein the first image includes a foreground area and a transition area, the transition area is a transition area between the foreground area and the background area, and noise is added to the transition area in the first image.

[0265] The first image may be an image after adding noise to an image with unclear foreground and background segmentation, and the image with unclear foreground and background segmentation may be an image after background processing, for example, a blurred image, an image after background replacement, an image after background removal, etc. Exemplarily, the first image may correspond to the image after adding noise to the transition area in the blurred image A, and the image after adding noise to the transition area in the blurred image C. Or the first image may correspond to the feature map B mentioned above.

[0266] S1202. Input the first image into the first model to obtain a second image; wherein the first model is used to remove noise in the first image under the constraint of a first condition, wherein the first condition is used to indicate the quality of the image after being processed by the first model.

[0267] The first model may correspond to the diffusion model mentioned above. The first condition may be a textual prompt indicating the quality of the processed image, such as super detail, super resolution, a specific value of resolution, etc. The first condition may also be a textual feature processed by encoding the textual prompt indicating the quality of the processed image.

[0268] The first condition may correspond to the text prompt mentioned above or the text feature generated by the text prompt encoding process mentioned above. The second image may correspond to the blurred image D mentioned above, or the second image may correspond to the feature map C mentioned above.

[0269] In some embodiments, the first model may predict the noise in the first image according to the first image and the first condition, and remove the predicted noise.

[0270] In some embodiments, the first model is a model for processing a specific object, the first condition is fixed, and the specific object may include: hair, animal hair, wool, etc. In this way, the amount of calculation of the diffusion model can be reduced. In addition, the diffusion model can be trained for a specific object, and the processing effect on the specific object is better. Optionally, when the first image is identified as an image including a specific object, the image processing is performed using the first model.

[0271] In some other embodiments, the quality of the processed image indicated by the first condition is positively correlated with the detail complexity of the transition region in the first image. The first model can adjust the quality of the processed image according to the detail complexity of the foreground edge. For example, the higher the detail complexity of the foreground edge, the higher the quality of the processed image. In this way, the quality of the transition region in the processed image can meet the detail complexity requirement of the transition region, and then the edge of the foreground in the transition region is clear.

[0272] In this way, the transition area in the image is denoised according to the quality of the image indicated by the first condition, so that the transition area in the image meets the quality requirement of the image and the edge of the foreground in the transition area is clear.

[0273] In a possible implementation, the first condition includes a text feature, and the text feature is used to indicate one or more of the following parameters of the image after being processed by the first model: image quality, resolution, authenticity, style, and objects in the image. In this way, the image processed by the diffusion model can be made consistent with expected image parameters.

[0274] In some embodiments, the text feature can be obtained by encoding the text prompt. The text prompt may include positive prompt words and / or negative prompt words. Both the positive prompt words and the negative prompt words can be used to describe one or more of the following parameters: resolution, authenticity, style, and objects in the image. Both the positive prompt words and the negative prompt words can refer to the above corresponding descriptions, which will not be repeated here.

[0275] In some embodiments, the image quality indicated by the text feature may be fixed. For example, the text feature may correspond to positive prompt words indicating high quality, such as "super resolution, super detail, master work", etc.

[0276] In some embodiments, the image quality indicated by the text feature may be positively correlated with the detail complexity of the object in the image. When the object in the image is an object with high detail complexity such as a portrait or wool, the text feature may correspond to positive prompt words indicating high quality such as "super resolution, super detail, master work"; when the object in the image is an object with low detail complexity such as an apple, the text feature may correspond to positive prompt words indicating high quality such as "higher resolution, detail".

[0277] In one possible implementation, obtaining a first image includes: obtaining a third image; when the size of the third image is larger than a preset size, downsampling the third image to obtain a fourth image, wherein the size of the fourth image is smaller than or equal to the preset size; inputting the fourth image into an encoder for encoding to obtain an encoded fourth image; adding noise to a transition region in the encoded fourth image to obtain the first image; after obtaining the second image, the method further includes: inputting the second image into a first decoder for decoding to obtain a fifth image, wherein the size of the fifth image is larger than the preset size.

[0278] The third image may be an image after background processing. Taking the background processing as blur processing as an example, the third image may correspond to the blurred image B mentioned above. The fourth image may correspond to the blurred image C mentioned above. The encoded fourth image may correspond to the feature map A mentioned above, the first image may correspond to the feature map B mentioned above, the second image may correspond to the feature map C mentioned above, and the fifth image may correspond to the blurred image D mentioned above. The size of the image decoded by the first decoder is larger than the size of the image encoded by the encoder, and may correspond to the 2XdVAE decoder and the 4XdVAE decoder mentioned above.

[0279] The preset size may be 512 pixels*512 pixels or any value, and is not specifically limited here.

[0280] Before the image is processed by the first model, the image can be downsampled to reduce the computational complexity of the subsequent denoising process of the first model. The downsampled image can meet the size requirements of the first model for image denoising, reducing the situation where the electronic device is stuck or not supported due to excessive computational complexity.

[0281] The downsampled image is compressed into the latent space by the encoder, and the first model can perform denoising in the latent space, further reducing the computational complexity of image processing. After the first model is processed, the processed image can also be decoded by the encoder, and the decoded image size is larger than the preset size. In this way, the image processed by the first model can be restored to the size before the downsampling process.

[0282] In one possible implementation, obtaining a first image includes: obtaining a third image; when the size of the third image is less than or equal to a preset size, inputting the third image into an encoder for encoding processing to obtain an encoded third image; adding noise to a transition area in the encoded third image to obtain the first image; after obtaining the second image, the method further includes: inputting the second image into a second decoder for decoding processing to obtain a fifth image, and the size of the fifth image is equal to the size of the third image.

[0283] The size of the image obtained by the second decoder is the same as the size of the image encoded by the encoder, which can correspond to the 1XdVAE decoder mentioned above.

[0284] When the size of the third image is less than or equal to the preset size, no downsampling process may be performed. Adaptively, the image processed by the first model is decoded by the second decoder.

[0285] In one possible implementation, obtaining a third image includes: obtaining a sixth image, the sixth image including: a foreground area, a transition area, and a background area; cropping the sixth image to obtain a third image, the third image including: a foreground area, a transition area, and a portion of the background area; the method also includes: fusing the fifth image into the sixth image to obtain a target image.

[0286] The sixth image may correspond to the blurred image A mentioned above, the third image may correspond to the blurred image B mentioned above, and the target image may correspond to the target image mentioned above.

[0287] In this way, before the image is processed by the first model, the image can be cropped to reduce the amount of calculation for the subsequent denoising process of the first model.

[0288] In a possible implementation, adding noise to a transition region in the encoded fourth image to obtain a first image includes: using a ternary map to determine a position of the transition region in the encoded fourth image; and adding noise to the transition region in the fourth image to obtain the first image.

[0289] The position of the transition area in the image can be determined by the ternary map, and noise can be accurately added to the transition area in the image to reduce interference to areas other than the transition area in the image.

[0290] In a possible implementation manner, the method further includes: obtaining an eighth image in response to a photographing operation; and performing background processing on the eighth image to obtain the third image or the sixth image.

[0291] The eighth image may be an image obtained by shooting. The method may be applied in a photo shooting scene. For example, it may be applied in a photo shooting scene in a portrait mode to make the hair in the portrait clear.

[0292] In a possible implementation, the method further includes: displaying a first interface, the first interface displaying a ninth image and a first control; and in response to a trigger operation on the first control, performing background processing on the ninth image to obtain a third image or a sixth image.

[0293] The ninth image may correspond to the image in the album mentioned above. The ninth image may also be an image set as a wallpaper, etc., which is not specifically limited here. The first interface may correspond to the image in the album mentioned above. Figure 2D or Figure 2F The interface shown. The trigger operation may be a click operation, a slide operation, or any type of operation, which is not specifically limited here. The first control may be a control for indicating background processing, for example, "blur", "change background", etc. mentioned above. This is not specifically limited here.

[0294] In one possible implementation, the training process of the first model includes: adding noise to a first training image to generate a noise image; predicting the noise in the noise image under the constraint of a second condition corresponding to the first training image, and removing the predicted noise from the noise image; the second condition is used to indicate the quality of the first training image, and the second condition is also used to indicate the content in the first training image; when the difference between the predicted noise and the added noise contained in the noise image satisfies the first loss function, the first model is obtained.

[0295] The first training image may correspond to the training image B mentioned above. The second condition may be Figure 8 The text prompt or text feature shown. The first loss function may correspond to the loss function loss5 mentioned above. The training process of the first model may correspond to the above Figure 8 The process shown.

[0296] In some embodiments, the training process of the first model includes: gradually adding noise to the first training image to generate a noise image containing noise of different degrees; predicting the noise in the noise image of different degrees of noise under the constraint of the second condition corresponding to the first training image, and removing the predicted noise from the noise image of different degrees of noise; the second condition is used to indicate the quality of the first training image; when the difference between the noise predicted by the to-be-trained model for the noise image of different degrees of noise and the most recently added noise contained in the noise image satisfies the first loss function, the first model is obtained. The training process of the first model may correspond to the above Figure 8 In this way, denoising is performed step by step to improve the accuracy of noise prediction.

[0297] In one possible implementation, the training process of the first decoder includes: downsampling the third training image to obtain a fourth training image; inputting the fourth training image into the encoder for encoding to obtain a fifth training image; inputting the fifth training image into the first decoder to be trained for decoding to obtain a second predicted image; when the difference between the second predicted image and the fifth training image satisfies the second loss function, obtaining the first decoder.

[0298] The third training image may correspond to the training image C mentioned above, the fourth training image may correspond to the training image D mentioned above, and the fifth training image may correspond to the feature map of the training image D mentioned above. The second loss function may correspond to the loss function loss6 mentioned above. The training process of the first decoder may correspond to the above Fig. 9 The process shown.

[0299] In one possible implementation, before training the first decoder, the method also includes: training the second decoder to be trained to obtain the second decoder; setting the parameters of the first decoder to be trained to be the same as the parameters of the second decoder; wherein the training process of the second decoder includes: inputting the fourth training image into the encoder for encoding processing to obtain the fifth training image; inputting the fifth training image into the second decoder to be trained for decoding processing to obtain the third predicted image; when the difference between the third predicted image and the fourth training image satisfies the third loss function, obtaining the second decoder.

[0300] In this way, some parameters can be reused, which can reduce the computational complexity of the decoder and shorten the training time of the decoder.

[0301] In a possible implementation, the first image includes: a portrait with hair, which can be applied to hair processing of the portrait.

[0302] It should be noted that the module names involved in the embodiments of the present application can be defined as other names as long as the functions of each module can be achieved, and there is no specific restriction on the names of the modules.

[0303] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in the embodiments of the present application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of the relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.

[0304] The image processing method of the embodiment of the present application has been described above, and the device for executing the above method provided by the embodiment of the present application is described below. Those skilled in the art can understand that the method and the device can be combined and referenced with each other, and the relevant device provided by the embodiment of the present application can execute the steps in the above method.

[0305] like Fig.13 As shown, Fig.13 A schematic diagram of the structure of an image processing device provided in an embodiment of the present application. The image processing device may be an electronic device in an embodiment of the present application, or a chip or chip system in an electronic device.

[0306] like Fig.13 As shown, the image processing device can be used in a communication device, a circuit, a hardware component or a chip, and the image processing device includes: a display unit 1301 and a processing unit 1302. The display unit 1301 is used to support the display step performed by the display method; the processing unit 1302 is used to support the image processing device to perform the information processing step.

[0307] In a possible implementation, the image processing apparatus may further include a communication unit 1303, and the communication unit 1303 is used to support the image processing apparatus to perform steps such as receiving or sending a message.

[0308] The image processing devices described in the embodiments of the present application may include Fig.13 The units described in the corresponding embodiments.

[0309] Specifically, the processing unit 1302 and the display unit 1301 may be integrated together, and the processing unit 1302 and the display unit 1301 may communicate with each other.

[0310] In a possible implementation, the image processing apparatus may further include: a storage unit 1304. The storage unit 1304 may include one or more memories, and the memory may be a device in one or more devices or circuits for storing programs or data.

[0311] The storage unit 1304 may exist independently and be connected to the processing unit 1302 via a communication bus. The storage unit 1304 may also be integrated with the processing unit 1302.

[0312] Taking the image processing device as an example, which may be a chip or chip system of an electronic device in an embodiment of the present application, the storage unit 1304 may store computer-executable instructions of the method of the electronic device, so that the processing unit 1302 executes the method of the electronic device in the above embodiment. The storage unit 1304 may be a register, a cache, or a random access memory (RAM), etc. The storage unit 1304 may be integrated with the processing unit 1302. The storage unit 1304 may be a read-only memory (ROM) or other types of static storage devices that can store static information and instructions, and the storage unit 1304 may be independent of the processing unit 1302.

[0313] In a possible implementation, the image processing device may further include: a communication unit 1303. The communication unit 1303 is used to support the image processing device to interact with other devices. Exemplarily, when the image processing device is an electronic device, the communication unit 1303 may be a communication interface or an interface circuit. When the image processing device is a chip or a chip system in an electronic device, the communication unit 1303 may be a communication interface. For example, the communication interface may be an input / output interface, a pin or a circuit.

[0314] The device of this embodiment can be used to execute the steps executed in the above method embodiment. Its implementation principle and technical effects are similar and will not be repeated here.

[0315] The image processing method provided in the embodiment of the present application can be applied to electronic devices with image processing functions. The electronic devices include terminal devices, and the specific device form of the terminal devices can refer to the above related descriptions, which will not be repeated here.

[0316] An embodiment of the present application provides an electronic device, which includes: one or more processors and a memory; the memory is coupled to the one or more processors, the memory is used to store computer program code, the computer program code includes computer instructions, and the one or more processors call the computer instructions to enable the electronic device to execute the above method.

[0317] The embodiment of the present application provides a chip or chip system. The chip or chip system includes one or more processors, and the one or more processors are used to call computer instructions to enable the electronic device to execute the technical solution in the above embodiment. Its implementation principle and technical effect are similar to those of the above-mentioned related embodiments, and will not be repeated here.

[0318] The embodiment of the present application also provides a computer-readable storage medium. The computer-readable storage medium includes computer instructions, and when the computer instructions are run on an electronic device, the electronic device executes the above method. The method described in the above embodiment can be implemented in whole or in part by software, hardware, firmware, or any combination thereof. If implemented in software, the function can be stored as one or more instructions or codes on a computer-readable medium or transmitted on a computer-readable medium. Computer-readable media may include computer storage media and communication media, and may also include any medium that can transfer a computer program from one place to another. The storage medium can be any target medium that can be accessed by a computer.

[0319] In one possible implementation, a computer-readable medium may include RAM, ROM, compact disc read-only memory (CD-ROM) or other optical disk storage, magnetic disk storage or other magnetic storage devices, or any other medium that is intended to carry or store the required program code in the form of instructions or data structures and can be accessed by a computer. Moreover, any connection is appropriately referred to as a computer-readable medium. For example, if the software is transmitted from a website, server or other remote source using a coaxial cable, fiber optic cable, twisted pair, digital subscriber line (DSL) or wireless technology (such as infrared, radio and microwave), the coaxial cable, fiber optic cable, twisted pair, DSL or wireless technology such as infrared, radio and microwave are included in the definition of medium. Disks and optical disks as used herein include optical disks, laser disks, optical disks, digital versatile disks (DVD), floppy disks and Blu-ray disks, where disks usually reproduce data magnetically, while optical disks reproduce data optically using lasers. Combinations of the above should also be included in the scope of computer-readable media.

[0320] An embodiment of the present application provides a computer program product, which includes a computer program code. When the computer program code is executed, the computer executes the above method.

[0321] The embodiments of the present application are described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processing unit of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable device to generate a machine, so that the instructions executed by the processing unit of the computer or other programmable data processing device generate a device for implementing the functions specified in one process or multiple processes in the flowchart and / or one box or multiple boxes in the block diagram.

[0322] The above specific implementation methods further illustrate the purpose, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above are only specific implementation methods of the present invention and are not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc. made on the basis of the technical solutions of the present invention should be included in the scope of protection of the present invention.

Claims

1. An image processing method, characterized in that: include: Acquire a first image; wherein the first image includes a foreground area and a transition area, and the transition area is a transition area between the foreground area and the background area; the first image is an image obtained by performing background processing on the image to be processed, performing encoding processing, and adding noise to the transition area in the encoded image; the size of the encoded image is less than or equal to a preset size; The first image is input into a first model to obtain a second image; wherein the first model is used to remove noise in the first image under the constraint of a first condition, wherein the first condition is used to indicate the quality of the image processed by the first model.

2. The method according to claim 1, characterized in that: The first condition includes a text feature, and the text feature is used to indicate one or more of the following parameters of the image after being processed by the first model: resolution, authenticity, style, and objects in the image.

3. The method according to claim 1, characterized in that The acquiring of the first image comprises: acquiring a third image; When the size of the third image is larger than the preset size, down-sampling the third image to obtain a fourth image, wherein the size of the fourth image is smaller than or equal to the preset size; Inputting the fourth image into an encoder for encoding processing to obtain the encoded fourth image; adding noise to the transition region in the encoded fourth image to obtain the first image; After obtaining the second image, the method further includes: The second image is input into the first decoder for decoding to obtain a fifth image, and the size of the fifth image is larger than the preset size.

4. The method according to claim 1, characterized in that: The acquiring of the first image comprises: acquiring a third image; When the size of the third image is less than or equal to a preset size, inputting the third image into an encoder for encoding to obtain the encoded third image; adding noise to the transition area in the encoded third image to obtain the first image; After obtaining the second image, the method further includes: The second image is input into a second decoder for decoding to obtain a fifth image, and the size of the fifth image is equal to the size of the third image.

5. The method according to claim 3, characterized in that: The adding noise to the transition area in the encoded fourth image to obtain the first image comprises: Determining the position of the transition region in the encoded fourth image using a ternary map; Noise is added to the transition area in the fourth image to obtain the first image.

6. The method according to any one of claims 3 to 5, characterized in that: The acquiring of the third image comprises: Acquire a sixth image, wherein the sixth image includes: the foreground area, the transition area, and the background area; Performing cropping processing on the sixth image to obtain the third image, wherein the third image includes: the foreground area, the transition area, and a portion of the background area; The method further comprises: The fifth image is fused into the sixth image to obtain a target image.

7. The method according to any one of claims 3 to 5, characterized in that: The method further comprises: In response to the photographing operation, an eighth image is obtained; Background processing is performed on the eighth image to obtain the third image.

8. The method according to any one of claims 3 to 5, characterized in that: The method further comprises: Displaying a first interface, wherein the first interface displays a ninth image and a first control; In response to a triggering operation on the first control, background processing is performed on the ninth image to obtain the third image.

9. The method according to any one of claims 1 to 5, characterized in that: The training process of the first model includes: adding noise to the first training image to generate a noise image; predicting the noise in the noise image under the constraint of a second condition corresponding to the first training image, and removing the predicted noise from the noise image; the second condition is used to indicate the quality of the first training image, and the second condition is also used to indicate the content in the first training image; The first model is obtained when the difference between the predicted noise and the added noise contained in the noisy image satisfies a first loss function.

10. The method according to any one of claims 1 to 5, characterized in that: The training process of the first decoder includes: Downsampling the third training image to obtain a fourth training image; Inputting the fourth training image into an encoder for encoding to obtain a fifth training image; Inputting the fifth training image into a first decoder to be trained for decoding to obtain a second predicted image; When the difference between the second predicted image and the fifth training image satisfies a second loss function, the first decoder is obtained.

11. The method according to claim 10, characterized in that Before training the first decoder, the method further includes: training a second decoder to be trained to obtain a second decoder; Setting the parameters of the first decoder to be trained to be the same as the parameters of the second decoder; The training process of the second decoder includes: Inputting the fourth training image into the encoder for encoding to obtain the fifth training image; Inputting the fifth training image into the second decoder to be trained for decoding to obtain a third predicted image; When the difference between the third predicted image and the fourth training image satisfies a third loss function, the second decoder is obtained.

12. The method according to any one of claims 1 to 5, characterized in that: The first image includes: a portrait with hair.

13. An electronic device, characterized in that: The electronic device comprises: one or more processors and a memory; The memory is coupled to the one or more processors, and the memory is used to store computer program codes, wherein the computer program codes include computer instructions, and the one or more processors call the computer instructions to enable the electronic device to perform the method according to any one of claims 1 to 12.

14. A chip system, characterized in that: The chip system is applied to an electronic device, and the chip system includes one or more processors, and the one or more processors are used to call computer instructions so that the electronic device executes the method as described in any one of claims 1 to 12.

15. A computer-readable storage medium, characterized in that: The computer-readable storage medium comprises computer instructions, and when the computer instructions are executed on an electronic device, the electronic device is caused to perform the method according to any one of claims 1 to 12.

16. A computer program product, characterized in that The computer program product comprises a computer program code, and when the computer program code is run on an electronic device, the electronic device is caused to perform the method according to any one of claims 1 to 12.

Citation Information

Patent Citations

  • An image processing method, a mobile terminal

    CN108989678A

  • Image processing method, terminal and computer storage medium

    CN110855876A

  • Image processing method and device, electronic equipment and storage medium

    CN111915505A

  • Image processing method and device, electronic equipment and computer readable storage medium

    CN113888437A

  • Background replacement method and device, equipment and storage medium

    CN115690406A