Portrait cutout method and device
By inputting the original image into the three-part map generation model and the cutout model, the mask is generated and the portrait image is intercepted, which solves the problems of poor portrait cutout effect and high model coupling in the existing technology, and achieves efficient portrait separation. and model maintenance.
Patent Information
- Application Number
- CN201910912853.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2019-09-25
- Publication Date
- 2024-09-24
- Estimated Expiration
- 2039-09-25
AI Technical Summary
The existing technology is difficult to effectively separate the portrait and the background, resulting in poor portrait cutout effects, and the model is highly coupled and difficult to independently modify or enhance.
By obtaining the original image and inputting it into the pre-trained three-quarter map generation model, a three-quarter map is generated, and then the mask is determined based on the three-quarter map and the cutout model, thereby intercepting the portrait image and ensuring the three-quarter map generation model and cutout model. No coupling, convenient for function modification or enhancement.
It achieves efficient separation of portraits and backgrounds, improves the accuracy of cutout results, and simplifies the model maintenance and upgrade process.
Smart Images

Figure CN111784726B
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of computer technology, and specifically to a method and device for cutting out a portrait. Background Art
[0002] With the rapid development of smartphones and Internet technology, taking photos with mobile phones and editing and sharing them has become an important part of people's lives. Portrait photos, as a form of photos, play an important role in daily entertainment, social interaction and sharing. Portrait photos generally refer to photos in the form of portraits and backgrounds. For such pictures, a common requirement is to separate the portrait from the background, which is what we often call portrait segmentation or portrait cutout. Summary of the invention
[0003] The embodiments of the present application provide a method and device for cutting out a portrait.
[0004] In a first aspect, an embodiment of the present application provides a portrait cutout method, comprising: obtaining an original image presenting a portrait; inputting the original image into a pre-trained tripartite map generation model to obtain a tripartite map of the original image; determining a mask of the original image based on the original image, the tripartite map of the original image and the pre-trained cutout model; and using the mask of the original image to cut out a portrait image from the original image.
[0005] In some embodiments, based on the original image, the triplicate map of the original image and a pre-trained cutout model, determining the mask of the original image includes: correcting the triplicate map of the original image; inputting the original image and the corrected triplicate map into the pre-trained cutout model to obtain the mask of the original image.
[0006] In some embodiments, the tripartite map of the original image is corrected, including: determining at least one connected area of the foreground area in the tripartite map of the original image; determining a connected area whose area in the at least one connected area of the foreground area is smaller than a preset first area threshold as a first target connected area; and changing the first target connected area from a foreground area to an unknown area to obtain a corrected tripartite map.
[0007] In some embodiments, the tripartite map of the original image is corrected, including: determining at least one connected area of the background area in the tripartite map of the original image; determining a connected area whose area in at least one connected area of the background area is smaller than a preset second area threshold as a second target connected area; and changing the second target connected area from a background area to an unknown area to obtain a corrected tripartite map.
[0008] In some embodiments, the tripartite map generation model is trained in the following manner: obtaining a first training sample set, wherein the first training sample includes a first sample image and a first sample tripartite map, and the first sample tripartite map is generated by performing a morphological transformation on a mask corresponding to the first sample image; using the first sample image and the first sample tripartite map in the first training sample in the first training sample set as the input and expected output of a first initial model, respectively, and using a machine learning method to train the first initial model to obtain a tripartite map generation model.
[0009] In some embodiments, the cutout model is trained in the following manner: obtaining a second training sample set, wherein the second training sample includes a second sample image, a second sample triplicate map and a sample mask, wherein a portrait is presented in the second sample image; using the second sample image and the second sample triplicate map in the second training sample in the second training sample set as inputs to the second initial model, using the sample mask corresponding to the input second sample image and the second sample triplicate map as the expected output of the second initial model, and using a machine learning method to train the second initial model to obtain the cutout model.
[0010] In the second aspect, an embodiment of the present application provides a portrait cutout device, comprising: an acquisition unit, configured to acquire an original image presenting a portrait; an input unit, configured to input the original image into a pre-trained tripartite map generation model to obtain a tripartite map of the original image; a determination unit, configured to determine a mask of the original image based on the original image, the tripartite map of the original image and the pre-trained cutout model; and a capture unit, configured to use the mask of the original image to capture a portrait image from the original image.
[0011] In some embodiments, the determination unit is further configured to determine the mask of the original image based on the original image, the triplicate map of the original image and a pre-trained cutout model in the following manner: correcting the triplicate map of the original image; inputting the original image and the corrected triplicate map into the pre-trained cutout model to obtain the mask of the original image.
[0012] In a third aspect, an embodiment of the present application provides an electronic device, comprising: one or more processors; a storage device on which one or more programs are stored, and when the one or more programs are executed by the one or more processors, the one or more processors implement the method described in any implementation method in the first aspect.
[0013] In a fourth aspect, an embodiment of the present application provides a computer-readable medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the method described in any implementation manner in the first aspect.
[0014] The portrait cutout method and device provided by the above embodiment of the present application first obtains an original image showing a portrait; then, the original image is input into a pre-trained tripartite map generation model to obtain a tripartite map of the original image; then, based on the original image, the tripartite map of the original image and the pre-trained cutout model, a mask of the original image is determined; finally, the portrait image is cut out from the original image using the mask of the original image. In this way, the tripartite map generation model and the cutout model are not coupled, which facilitates modification or enhancement of any model. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] Other features, objects and advantages of the present application will become more apparent by reading the detailed description of non-limiting embodiments made with reference to the following drawings:
[0016] Figure 1 is an exemplary system architecture diagram to which various embodiments of the present application may be applied;
[0017] Figure 2 is a flow chart of an embodiment of a portrait cutout method according to the present application;
[0018] Figure 3 is a flowchart of another embodiment of the portrait cutout method according to the present application;
[0019] Figure 4 is a schematic diagram of an application scenario of the portrait cutout method according to the present application;
[0020] Figure 5 is a structural schematic diagram of an embodiment of a portrait cutout device according to the present application;
[0021] Figure 6 It is a structural diagram of a computer system suitable for implementing an electronic device of an embodiment of the present application. DETAILED DESCRIPTION
[0022] The present application will be further described in detail below in conjunction with the accompanying drawings and embodiments. It is to be understood that the specific embodiments described herein are only used to explain the relevant invention, rather than to limit the invention. It should also be noted that, for ease of description, only the parts related to the relevant invention are shown in the accompanying drawings.
[0023] It should be noted that, in the absence of conflict, the embodiments and features in the embodiments of the present application can be combined with each other. The present application will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.
[0024] Figure 1 An exemplary system architecture 100 to which the portrait cutout method or portrait cutout device of the present application can be applied is shown.
[0025] like Figure 1 As shown, the system architecture 100 may include terminal devices 101, 102, 103, a network 104 and a server 105. The network 104 is used to provide a medium for communication links between the terminal devices 101, 102, 103 and the server 105. The network 104 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.
[0026] The user can use the terminal devices 101, 102, 103 to interact with the server 105 through the network 104 to receive or send messages (for example, the terminal devices 101, 102, 103 can send the original image showing the portrait to the server 105; the server 105 can also send the pre-trained three-picture generation model and the pre-trained cutout model to the terminal devices 101, 102, 103). Various communication client applications can be installed on the terminal devices 101, 102, 103, such as image processing applications, camera applications, face recognition applications, etc.
[0027] The terminal devices 101, 102, and 103 can first obtain an original image showing a portrait; then, the original image can be input into a pre-trained tripartite map generation model to obtain a tripartite map of the original image; then, the mask of the original image can be determined based on the original image, the tripartite map of the original image, and the pre-trained cutout model; finally, the mask of the original image can be used to extract a portrait image from the original image.
[0028] Terminal devices 101, 102, 103 can be hardware or software. When terminal devices 101, 102, 103 are hardware, they can be various electronic devices with cameras and supporting information interaction, including but not limited to smart phones, tablet computers, laptops, etc. When terminal devices 101, 102, 103 are software, they can be installed in the electronic devices listed above. It can be implemented as multiple software or software modules (for example, multiple software or software modules for providing distributed services), or it can be implemented as a single software or software module. No specific limitation is made here.
[0029] The server 105 may be a server that provides various services. For example, an original image showing a portrait may be analyzed to extract a portrait image from the original image. The server 105 may first obtain an original image showing a portrait; then, the original image may be input into a pre-trained tripartite graph generation model to obtain a tripartite graph of the original image; then, a mask of the original image may be determined based on the original image, the tripartite graph of the original image, and a pre-trained cutout model; finally, the mask of the original image may be used to extract a portrait image from the original image.
[0030] It should be noted that the server 105 can be hardware or software. When the server 105 is hardware, it can be implemented as a distributed server cluster consisting of multiple servers, or it can be implemented as a single server. When the server 105 is software, it can be implemented as multiple software or software modules (for example, software or software modules for providing distributed services), or it can be implemented as a single software or software module. No specific limitation is made here.
[0031] It should be noted that the portrait cutout method provided in the embodiment of the present application can be executed by the terminal devices 101 , 102 , 103 , or by the server 105 .
[0032] It should also be noted that the terminal devices 101, 102, and 103 may store a pre-trained tripartite map generation model and a pre-trained cutout model locally, and the terminal devices 101, 102, and 103 may determine the tripartite map of the original image and the mask of the original image, thereby extracting a portrait image from the original image. In this case, the exemplary system architecture 100 may not have the network 104 and the server 105.
[0033] It should also be noted that the server 105 may also store an original image showing a human portrait locally, and the server 105 may obtain the original image showing a human portrait locally. In this case, the exemplary system architecture 100 may not have the terminal devices 101, 102, 103 and the network 104.
[0034] It should be understood that Figure 1 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.
[0035] Continue to refer Figure 2 , shows a process 200 of an embodiment of a portrait cutout method according to the present application. The portrait cutout method comprises the following steps:
[0036] Step 201, obtaining an original image showing a human portrait.
[0037] In this embodiment, the execution subject of the portrait cutout method (for example Figure 1 The server shown in the figure can obtain the original image showing a portrait. A portrait is a two-dimensional or three-dimensional description of a person as a whole, usually including the face, eyes, nose, mouth, ears, eyebrows, etc. Sometimes it is also a description of the whole body of a person, referring to a rough outline of a person.
[0038] Step 202: input the original image into a pre-trained tripartite map generation model to obtain a tripartite map of the original image.
[0039] In this embodiment, the execution subject may input the original image acquired in step 201 into a pre-trained trimap generation model to obtain a trimap of the original image. The trimap may reflect the foreground area, the background area (background area), and the unknown area (uncertain area) of the original image. Here, the trimap generation model may be used to characterize the correspondence between the original image and the trimap of the original image, that is, the trimap generation model may generate a trimap of the original image based on the original image. Here, a trimap generation model that can characterize the correspondence between the original image and the trimap of the original image may be trained in a variety of ways.
[0040] As an example, a correspondence table storing the correspondence between multiple sample images and the tripartite diagrams corresponding to the sample images may be generated by a technician based on statistics of a large number of sample images and the tripartite diagrams corresponding to the sample images (for example, the tripartite diagrams may be tripartite diagrams drawn by the technician based on masks corresponding to the sample images), and the correspondence table may be used as a tripartite diagram generation model.
[0041] Step 203, determining a mask of the original image based on the original image, the tripartite map of the original image and the pre-trained cutout model.
[0042] In this embodiment, the above-mentioned execution entity can determine the mask of the above-mentioned original image based on the original image obtained in step 201, the tripartite map of the original image obtained in step 202, and the pre-trained cutout model. It should be noted that the mask refers to the outside of the selection box. The mask here usually refers to a mask with an alpha channel. The alpha channel refers to the transparency and translucency of a picture. Specifically, the above-mentioned execution entity can input the above-mentioned original image and the tripartite map of the above-mentioned original image into the above-mentioned pre-trained cutout model to obtain the mask of the above-mentioned original image.
[0043] Here, the above-mentioned cutout model can be used to characterize the correspondence between the original image and the tripartite diagram of the original image and the mask of the original image, that is, the cutout model can generate the mask of the original image based on the original image and the tripartite diagram of the original image. Here, a cutout model that can characterize the correspondence between the original image and the tripartite diagram of the original image and the mask of the original image can be trained in a variety of ways.
[0044] As an example, a technician may generate a correspondence table storing the correspondence between multiple sample images, triplicated images of sample images and masks of sample images based on statistics of a large number of sample images, triplicated images of sample images and masks of sample images, and use the correspondence table as a cutout model.
[0045] Step 204: Using the mask of the original image, extract the portrait image from the original image.
[0046] In this embodiment, the execution subject may extract the portrait image from the original image using the mask of the original image determined in step 203. Since the mask refers to the outside of the selection box, the execution subject may determine the inner area of the selection box as the portrait image, thereby extracting the portrait image from the original image.
[0047] In some optional implementations of this embodiment, the above-mentioned three-part image generation model may be obtained by training the above-mentioned execution subject or other execution subjects used to train the above-mentioned three-part image generation model in the following manner:
[0048] In step S1, a first training sample set may be obtained.
[0049] Here, the first training sample in the above-mentioned first training sample set may include a first sample image and a first sample triplicate map. It should be noted that the first sample triplicate map may be generated by performing a morphological transformation on a mask corresponding to the first sample image. The mask refers to the outside of the selection box (the inside of the selection box is the selection area). The mask here usually refers to a mask with an alpha (alpha) channel. The alpha channel refers to the transparency and translucency of an image.
[0050] The core problem of image cutout technology is to solve the following formula (1):
[0051] I p =α p F p +(1-α p )B p (1)
[0052] Among them, I p is the pixel value of the pth pixel in the image, which is a known quantity; αp is the transparency of the p-th pixel in the image, F p is the foreground pixel value of the p-th pixel in the image, B p is the background pixel value of the pth pixel in the image, and these three variables are unknown. To understand this formula, the original image can be regarded as the foreground and background according to a certain weight (α p For pixels that are completely certain to be foreground, α=1; for pixels that are completely certain to be background, α=0; for pixels that are uncertain to be foreground or background (unknown area), α is between 0 and 1.
[0053] Here, the mask corresponding to the image can be morphologically transformed in the following way to obtain the tripartite map of the image: the mask corresponding to the image can be dilated and eroded to obtain the tripartite map of the image. It should be noted that dilation and erosion usually target the foreground area (white part, highlight part). Here, dilation is to dilate the foreground area in the image, and the area expanded by the dilation operation is regarded as the unknown area. It is easy to understand that after the dilation operation is performed on the image, the background area (black part) will naturally become smaller; erosion is to erode the foreground area in the image, and the area reduced by the erosion operation is regarded as the unknown area. It is easy to understand that after the erosion operation is performed on the image, the foreground area (white part) will naturally become smaller.
[0054] It should be noted that, in the process of morphological transformation, if different parameters (dilation parameters, erosion parameters) are used for morphological transformation, the resulting tripartite graphs are different. In order to improve the effect of the tripartite graphs generated by the trained tripartite graph generation model during application, each first sample tripartite graph here is usually generated by morphologically transforming the mask corresponding to the first sample image using different parameters.
[0055] In step S2, the first sample image and the first sample tripartite map in the first training sample in the first training sample set may be used as the input and expected output of the first initial model respectively, and the first initial model may be trained using a machine learning method to obtain a tripartite map generation model.
[0056] Here, the first sample image in the first training sample in the first training sample set can be input into the first initial model to obtain a tripartite graph of the first sample image, and the first sample tripartite graph in the first training sample is used as the expected output of the first initial model, and the first initial model is trained using a machine learning method. Specifically, the difference between the obtained tripartite graph and the first sample tripartite graph in the first training sample can be first calculated using a preset loss function, and the difference between the obtained tripartite graph and the first sample tripartite graph in the first training sample can be calculated using a standard cross entropy loss function as a loss function. Then, the network parameters of the first initial model can be adjusted based on the calculated difference, and the training can be terminated when the preset training termination condition is met. For example, the preset training termination condition here can include but is not limited to at least one of the following: the training time exceeds the preset duration; the number of training times exceeds the preset number of times; the calculated difference is less than the preset difference threshold. The first initial model can be a convolutional neural network, a deep neural network, and the like.
[0057] Here, the trained tripartite map generation model can be a structure based on Deeplab V3+ and Resnet50, which can include an encoder that downsamples the input image by 16 times and upsamples it by 4 times. Deeplab V3+ is a tool that can adjust the filter field of view and control the resolution of feature responses calculated by convolutional neural networks.
[0058] In some optional implementations of this embodiment, the above-mentioned cutout model may be obtained by training the above-mentioned execution subject or other execution subjects used to train the above-mentioned cutout model in the following manner:
[0059] In step S1, a second training sample set may be obtained.
[0060] Here, the second training sample in the second training sample set may include a second sample image, a second sample triplicate map and a sample mask. The second sample image usually presents a human figure. It should be noted that the second sample triplicate map may be generated by performing a morphological transformation on the mask corresponding to the second sample image. Here, when performing a morphological transformation on the mask corresponding to the second sample image, the parameters applied during the morphological transformation are usually set to increase the range of the morphological transformation.
[0061] In step S2, the second sample image and the second sample tri-map in the second training sample in the above-mentioned second training sample set can be used as inputs of the second initial model, and the sample mask corresponding to the input second sample image and the second sample tri-map can be used as the expected output of the second initial model. The above-mentioned second initial model can be trained using a machine learning method to obtain a cutout model.
[0062] Here, the second sample image and the second sample tripartite graph in the second training sample in the second training sample set can be input into the second initial model to obtain the mask of the second sample image, and the sample mask in the second training sample is used as the expected output of the second initial model, and the second initial model is trained using a machine learning method. Specifically, the difference between the obtained mask and the sample mask in the second training sample can be first calculated using a preset loss function, and the difference between the obtained mask and the sample mask in the second training sample can be calculated using a regression loss function as a loss function. Then, the network parameters of the second initial model can be adjusted based on the calculated difference, and the training can be terminated when the preset training termination condition is met. For example, the preset training termination condition here can include but is not limited to at least one of the following: the training time exceeds the preset duration; the number of training times exceeds the preset number of times; the calculated difference is less than the preset difference threshold. The second initial model can be a convolutional neural network, a deep neural network, and the like.
[0063] The method provided by the above embodiment of the present application obtains a tripartite map of the original image by inputting the original image into a pre-trained tripartite map generation model, and then determines the mask of the original image based on the original image, the tripartite map of the original image, and the pre-trained cutout model. This method can make the tripartite map generation model and the cutout model uncoupled, making it convenient to modify or enhance the function of either model.
[0064] Further references Figure 3 , which shows a process 300 of another embodiment of a portrait cutout method. The process 300 of the portrait cutout method includes the following steps:
[0065] Step 301, obtaining an original image showing a human portrait.
[0066] Step 302: input the original image into a pre-trained tripartite map generation model to obtain a tripartite map of the original image.
[0067] In this embodiment, steps 301 - 302 may be performed in a manner similar to steps 201 - 202 , and will not be described in detail herein.
[0068] Step 303, correcting the trisection image of the original image.
[0069] In this embodiment, the execution subject may correct the tripartite diagram of the original image obtained in step 302. As an example, if the execution subject is a terminal device, the execution subject may present the original image and the tripartite diagram of the original image, and the user may correct the original image and the tripartite diagram of the original image presented by the terminal device. If the execution subject is a server, the execution subject may send the original image and the tripartite diagram of the original image to the target terminal device (the terminal device of the person who corrects the tripartite diagram); then, the target terminal device may present the original image and the tripartite diagram of the original image, and the user may correct the original image and the tripartite diagram of the original image presented by the target terminal device; finally, the target terminal device may send the corrected tripartite diagram of the original image to the execution subject.
[0070] Step 304 , input the original image and the corrected trisection image into a pre-trained cutout model to obtain a mask of the original image.
[0071] In this embodiment, the execution subject may input the original image acquired in step 301 and the modified trisection image obtained in step 303 into a pre-trained cutout model to obtain a mask of the original image.
[0072] Here, the above-mentioned cutout model can be used to characterize the correspondence between the original image and the tripartite diagram of the original image and the mask of the original image, that is, the cutout model can generate the mask of the original image based on the original image and the tripartite diagram of the original image. Here, a cutout model that can characterize the correspondence between the original image and the tripartite diagram of the original image and the mask of the original image can be trained in a variety of ways.
[0073] As an example, a technician may generate a correspondence table storing the correspondence between multiple sample images, triplicated images of sample images and masks of sample images based on statistics of a large number of sample images, triplicated images of sample images and masks of sample images, and use the correspondence table as a cutout model.
[0074] Step 305: Using the mask of the original image, extract the portrait image from the original image.
[0075] In this embodiment, step 305 may be performed in a manner similar to step 204, and will not be described in detail herein.
[0076] In some optional implementations of this embodiment, the execution subject may correct the tripartite graph of the original image in the following manner: the execution subject may first determine at least one connected region of the foreground region in the tripartite graph of the original image. In an image, the smallest unit is a pixel, and each pixel is surrounded by 8 adjacent pixels. There are two common adjacency relationships: 4-adjacency and 8-adjacency. 4-adjacency refers to the adjacency of 4 points in the upper, lower, left, and right directions. 8-adjacency refers to the adjacency of 8 points in the upper, lower, left, and right directions as well as the diagonal positions. If pixel points A and B are adjacent, we say that A and B are connected, so we have the following conclusion without proof: if A is connected to B, and B is connected to C, then A and C are connected. Visually, connected points form an area, while disconnected points form different areas. Such a set of connected points is called a connected area. Here, the adjacency relationship of the pixel points in the connected area is usually 8-adjacency. Afterwards, for each connected region in at least one connected region of the foreground region, the execution entity may determine the area of the connected region; and may determine the connected region whose area in the at least one connected region is less than a preset first area threshold as a first target connected region. Finally, the execution entity may change the area attribute of the first target connected region in the tripartite map from a foreground region to an unknown region, thereby obtaining a modified tripartite map.
[0077] In some optional implementations of the present embodiment, the execution subject may correct the tripartite graph of the original image in the following manner: the execution subject may first determine at least one connected region of the background region in the tripartite graph of the original image. Here, the adjacency relationship of the pixels in the connected region is usually 8-adjacent. Afterwards, for each connected region in at least one connected region of the background region, the execution subject may determine the area of the connected region; and the connected region in at least one connected region of the background region whose area is less than a preset second area threshold may be determined as the second target connected region. Finally, the execution subject may change the area attribute of the second target connected region in the tripartite graph from the background region to the unknown region to obtain a corrected tripartite graph.
[0078] It should be noted that the above-mentioned execution entity can change the first target connected area in the tripartite map of the original image from the foreground area to the unknown area, and change the second target connected area in the tripartite map of the original image from the background area to the unknown area, so as to obtain a modified tripartite map.
[0079] from Figure 3 It can be seen that Figure 2Compared with the corresponding embodiment, the process 300 of the portrait cutout method in this embodiment embodies step 303 of correcting the three-division map of the original image, and step 304 of inputting the original image and the corrected three-division map into the pre-trained cutout model to obtain the mask of the original image. Therefore, the solution described in this embodiment can correct the output three-division map, thereby improving the accuracy of the cutout result.
[0080] Continue to see Figure 4 , Figure 4 FIG. 1 is a schematic diagram of an application scenario of the portrait cutout method according to this embodiment. Figure 4 In the application scenario of the present invention, the execution subject (for example, a server or a terminal device) of the portrait cutout method may first obtain an original image 401 showing a portrait. Afterwards, the execution subject may input the original image 401 into a pre-trained tripartite map generation model 402 to obtain a tripartite map 403 of the original image 401. Then, the execution subject may correct the tripartite map 403 of the original image to obtain a corrected tripartite map 404. Then, the execution subject may input the original image 401 and the corrected tripartite map 404 into a pre-trained cutout model 405 to obtain a mask 406 of the original image 401. Finally, the execution subject may use the mask 406 of the original image to cut out a portrait image 407 from the original image 401.
[0081] Further references Figure 5 As an implementation of the methods shown in the above figures, the present application provides an embodiment of a portrait cutout device, which is similar to Figure 2 Corresponding to the method embodiment shown, the device can be specifically applied to various electronic devices.
[0082] like Figure 5 As shown, the portrait cutout device 500 of this embodiment includes: an acquisition unit 501, an input unit 502, a determination unit 503 and a clipping unit 504. The acquisition unit 501 is configured to acquire an original image showing a portrait; the input unit 502 is configured to input the original image into a pre-trained tripartite graph generation model to obtain a tripartite graph of the original image; the determination unit 503 is configured to determine a mask of the original image based on the original image, the tripartite graph of the original image and the pre-trained cutout model; the clipping unit 504 is configured to clip a portrait image from the original image using the mask of the original image.
[0083] In this embodiment, the specific processing of the acquisition unit 501, the input unit 502, the determination unit 503 and the interception unit 504 of the portrait cutout device 500 can be referred to. Figure 2 This corresponds to step 201, step 202, step 203 and step 204 in the embodiment.
[0084] In some optional implementations of this embodiment, the determination unit 503 may correct the tripartite map of the original image. As an example, if the execution subject is a terminal device, the determination unit 503 may present the original image and the tripartite map of the original image, and the user may correct the original image and the tripartite map of the original image presented by the terminal device. If the execution subject is a server, the determination unit 503 may send the original image and the tripartite map of the original image to the target terminal device (the terminal device of the person who corrects the tripartite map); then, the target terminal device may present the original image and the tripartite map of the original image, and the user may correct the original image and the tripartite map of the original image presented by the target terminal device; finally, the target terminal device may send the corrected tripartite map of the original image to the execution subject. The determination unit 503 may input the original image and the corrected tripartite map into a pre-trained cutout model to obtain a mask of the original image. Here, the above-mentioned cutout model can be used to characterize the correspondence between the original image and the tripartite diagram of the original image and the mask of the original image, that is, the cutout model can generate the mask of the original image based on the original image and the tripartite diagram of the original image. Here, a cutout model that can characterize the correspondence between the original image and the tripartite diagram of the original image and the mask of the original image can be trained in a variety of ways. As an example, a correspondence table storing the correspondence between multiple sample images and the tripartite diagram of the sample image and the mask of the sample image can be generated by a technician based on statistics of a large number of sample images, the tripartite diagram of the sample images, and the mask of the sample image, and the correspondence table is used as the cutout model.
[0085] In some optional implementations of this embodiment, the determination unit 503 may correct the tripartite graph of the original image in the following manner: the determination unit 503 may first determine at least one connected region of the foreground region in the tripartite graph of the original image. In an image, the smallest unit is a pixel, and each pixel is surrounded by 8 adjacent pixels. There are two common adjacency relationships: 4-adjacency and 8-adjacency. 4-adjacency refers to the adjacency of 4 points in the upper, lower, left, and right directions. 8-adjacency refers to the adjacency of 8 points in the upper, lower, left, right, and diagonal positions. If pixel points A and B are adjacent, we say that A and B are connected, so we have the following conclusion without proof: if A is connected to B, and B is connected to C, then A and C are connected. Visually, connected points form an area, while disconnected points form different areas. Such a set of connected points is called a connected area. Here, the adjacency relationship of the pixel points in the connected area is usually 8-adjacency. Afterwards, for each connected region in the at least one connected region of the foreground region, the determination unit 503 may determine the area of the connected region; and may determine the connected region whose area in the at least one connected region is less than a preset first area threshold as the first target connected region. Finally, the determination unit 503 may change the area attribute of the first target connected region in the tripartite map from the foreground region to the unknown region, and obtain a modified tripartite map.
[0086] In some optional implementations of the present embodiment, the determination unit 503 may correct the tripartite graph of the original image in the following manner: the determination unit 503 may first determine at least one connected region of the background region in the tripartite graph of the original image. Here, the adjacency relationship of the pixels in the connected region is usually 8-adjacent. Afterwards, for each connected region in at least one connected region in the background region, the determination unit 503 may determine the area of the connected region; and may determine the connected region in at least one connected region in the background region whose area is less than a preset second area threshold as the second target connected region. Finally, the determination unit 503 may change the area attribute of the second target connected region in the tripartite graph from the background region to the unknown region to obtain a corrected tripartite graph.
[0087] In some optional implementations of this embodiment, the above-mentioned three-part image generation model may be obtained by training the above-mentioned execution subject or other execution subjects used to train the above-mentioned three-part image generation model in the following manner:
[0088] In step S1, a first training sample set may be obtained.
[0089] Here, the first training sample in the above-mentioned first training sample set may include a first sample image and a first sample triplicate map. It should be noted that the first sample triplicate map may be generated by performing a morphological transformation on a mask corresponding to the first sample image. The mask refers to the outside of the selection box (the inside of the selection box is the selection area). The mask here usually refers to a mask with an alpha (alpha) channel. The alpha channel refers to the transparency and translucency of an image.
[0090] The core problem of image cutout technology is to solve the following formula (1):
[0091] I p =α p F p +(1-α p )B p (1)
[0092] Among them, I p is the pixel value of the pth pixel in the image, which is a known quantity; α p is the transparency of the p-th pixel in the image, F p is the foreground pixel value of the p-th pixel in the image, B p is the background pixel value of the pth pixel in the image, and these three variables are unknown. To understand this formula, the original image can be regarded as the foreground and background according to a certain weight (α p For pixels that are completely certain to be foreground, α=1; for pixels that are completely certain to be background, α=0; for pixels that are uncertain to be foreground or background (unknown area), α is between 0 and 1.
[0093] Here, the mask corresponding to the image can be morphologically transformed in the following way to obtain the tripartite map of the image: the mask corresponding to the image can be dilated and eroded to obtain the tripartite map of the image. It should be noted that dilation and erosion usually target the foreground area. Here, dilation is to dilate the foreground area in the image, and the area expanded by the dilation operation is regarded as the unknown area. It is easy to understand that after the dilation operation is performed on the image, the background area will naturally become smaller; erosion is to erode the foreground area in the image, and the area reduced by the erosion operation is regarded as the unknown area. It is easy to understand that after the erosion operation is performed on the image, the foreground area will naturally become smaller.
[0094] It should be noted that, in the process of morphological transformation, if different parameters are used for morphological transformation, the obtained tripartite graphs are different. In order to improve the effect of the tripartite graph generated by the trained tripartite graph generation model during application, each first sample tripartite graph here is usually generated by morphologically transforming the mask corresponding to the first sample image using different parameters.
[0095] In step S2, the first sample image and the first sample tripartite map in the first training sample in the first training sample set may be used as the input and expected output of the first initial model respectively, and the first initial model may be trained using a machine learning method to obtain a tripartite map generation model.
[0096] Here, the first sample image in the first training sample in the first training sample set can be input into the first initial model to obtain a tripartite graph of the first sample image, and the first sample tripartite graph in the first training sample is used as the expected output of the first initial model, and the first initial model is trained using a machine learning method. Specifically, the difference between the obtained tripartite graph and the first sample tripartite graph in the first training sample can be first calculated using a preset loss function, and the difference between the obtained tripartite graph and the first sample tripartite graph in the first training sample can be calculated using a standard cross entropy loss function as a loss function. Then, the network parameters of the first initial model can be adjusted based on the calculated difference, and the training can be terminated when the preset training termination condition is met. For example, the preset training termination condition here can include but is not limited to at least one of the following: the training time exceeds the preset duration; the number of training times exceeds the preset number of times; the calculated difference is less than the preset difference threshold. The first initial model can be a convolutional neural network, a deep neural network, and the like.
[0097] Here, the trained tripartite map generation model can be a structure based on Deeplab V3+ and Resnet50, which can include an encoder that downsamples the input image by 16 times and upsamples it by 4 times. Deeplab V3+ is a tool that can adjust the filter field of view and control the resolution of feature responses calculated by convolutional neural networks.
[0098] In some optional implementations of this embodiment, the above-mentioned cutout model may be obtained by training the above-mentioned execution subject or other execution subjects used to train the above-mentioned cutout model in the following manner:
[0099] In step S1, a second training sample set may be obtained.
[0100] Here, the second training sample in the second training sample set may include a second sample image, a second sample triplicate map and a sample mask. The second sample image usually presents a human figure. It should be noted that the second sample triplicate map may be generated by performing a morphological transformation on the mask corresponding to the second sample image. Here, when performing a morphological transformation on the mask corresponding to the second sample image, the parameters applied during the morphological transformation are usually set to increase the range of the morphological transformation.
[0101] In step S2, the second sample image and the second sample tri-map in the second training sample in the above-mentioned second training sample set can be used as inputs of the second initial model, and the sample mask corresponding to the input second sample image and the second sample tri-map can be used as the expected output of the second initial model. The above-mentioned second initial model can be trained using a machine learning method to obtain a cutout model.
[0102] Here, the second sample image and the second sample tripartite graph in the second training sample in the second training sample set can be input into the second initial model to obtain the mask of the second sample image, and the sample mask in the second training sample is used as the expected output of the second initial model, and the second initial model is trained using a machine learning method. Specifically, the difference between the obtained mask and the sample mask in the second training sample can be first calculated using a preset loss function, and the difference between the obtained mask and the sample mask in the second training sample can be calculated using a regression loss function as a loss function. Then, the network parameters of the second initial model can be adjusted based on the calculated difference, and the training can be terminated when the preset training termination condition is met. For example, the preset training termination condition here can include but is not limited to at least one of the following: the training time exceeds the preset duration; the number of training times exceeds the preset number of times; the calculated difference is less than the preset difference threshold. The second initial model can be a convolutional neural network, a deep neural network, and the like.
[0103] Reference below Figure 6 , which shows an electronic device (eg, Figure 1 The terminal device in the embodiment of the present disclosure may include but is not limited to mobile terminals such as mobile phones, notebook computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 6 The electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.
[0104] like Figure 6As shown, the electronic device 600 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage device 608 into a random access memory (RAM) 603. In the RAM 603, various programs and data required for the operation of the electronic device 600 are also stored. The processing device 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0105] Typically, the following devices may be connected to the I / O interface 605: an input device 606 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 607 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; and a communication device 609. The communication device 609 may allow the electronic device 600 to communicate with other devices wirelessly or by wire to exchange data. Figure 6 The electronic device 600 is shown with various devices, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead. Figure 6 Each block shown in the figure may represent one device, or may represent multiple devices as required.
[0106] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through a communication device 609, or installed from a storage device 608, or installed from a ROM 602. When the computer program is executed by the processing device 601, the above functions defined in the method of the embodiment of the present disclosure are executed. It should be noted that the computer-readable medium described in the embodiment of the present disclosure can be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium can be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to, an electrical connection with one or more conductors, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In an embodiment of the present disclosure, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, an apparatus, or a device. In an embodiment of the present disclosure, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, in which a computer-readable program code is carried. Such propagated data signals may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. A computer-readable signal medium may also be any computer-readable medium other than a computer-readable storage medium, which may send, propagate, or transmit a program for use by or in combination with an instruction execution system, an apparatus, or a device. The program code embodied on the computer readable medium may be transmitted using any appropriate medium, including but not limited to: wires, optical cables, RF (radio frequency), etc., or any suitable combination of the foregoing.
[0107] The computer-readable medium may be included in the electronic device; or it may exist independently without being installed in the electronic device. The computer-readable medium carries one or more programs. When the one or more programs are executed by the electronic device, the electronic device: obtains an original image showing a portrait; inputs the original image into a pre-trained tripartite map generation model to obtain a tripartite map of the original image; determines a mask of the original image based on the original image, the tripartite map of the original image, and the pre-trained cutout model; and uses the mask of the original image to cut out a portrait image from the original image.
[0108] Computer program code for performing the operations of embodiments of the present disclosure may be written in one or more programming languages or a combination thereof, including object-oriented programming languages, such as Java, Smalltalk, C++, and conventional procedural programming languages, such as "C" or similar programming languages. The program code may be executed entirely on a user's computer, partially on a user's computer, as a separate software package, partially on a user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving a remote computer, the remote computer may be connected to the user's computer via any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0109] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present disclosure. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some implementations as replacements, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0110] The units involved in the embodiments described in the present disclosure may be implemented by software or hardware. The units described may also be set in a processor, for example, it may be described as: a processor includes an acquisition unit, an input unit, a determination unit, and an interception unit. The names of these units do not constitute limitations on the units themselves in some cases, for example, the acquisition unit may also be described as a "unit for acquiring an original image showing a portrait".
[0111] The above description is only a preferred embodiment of the present disclosure and an explanation of the technical principles used. Those skilled in the art should understand that the scope of the invention involved in the embodiments of the present disclosure is not limited to the technical solutions formed by a specific combination of the above-mentioned technical features, but should also cover other technical solutions formed by any combination of the above-mentioned technical features or their equivalent features without departing from the above-mentioned inventive concept. For example, the above-mentioned features are replaced with the technical features with similar functions disclosed in the embodiments of the present disclosure (but not limited to) to form a technical solution.
Claims
1. A portrait cutout method, include: Obtaining an original image showing a human figure; The original image is input into a pre-trained tripartite graph generation model to obtain a tripartite graph of the original image, wherein the tripartite graph generation model is trained in the following manner: a first training sample set is obtained, wherein the first training sample includes a first sample image and a first sample tripartite graph, and the first sample tripartite graph is generated by performing a morphological transformation on a mask corresponding to the first sample image, and the mask includes a channel with transparency and semi-transparency of the image; the first sample image and the first sample tripartite graph in the first training sample in the first training sample set are used as input and expected output of a first initial model, respectively, and the first initial model is trained using a machine learning method to obtain a tripartite graph generation model; Determining a mask of the original image based on the original image, a tripartite diagram of the original image, and a pre-trained matting model; The portrait image is cut out from the original image using the mask of the original image.
2. The method according to claim 1, in, The determining of the mask of the original image based on the original image, the tripartite map of the original image and the pre-trained cutout model comprises: Correcting the tripartite map of the original image; The original image and the corrected tripartite map are input into a pre-trained cutout model to obtain a mask of the original image.
3. The method according to claim 2, in, The correcting the tripartite map of the original image includes: Determine at least one connected region of the foreground region in the tripartite map of the original image; Determine a connected region whose area in at least one connected region of the foreground region is smaller than a preset first area threshold as a first target connected region; The first target connected area is changed from a foreground area to an unknown area to obtain a modified tripartite map.
4. The method according to claim 2, in, The correcting the tripartite map of the original image includes: Determine at least one connected region of the background region in the trigraph of the original image; Determine a connected region whose area in at least one connected region of the background region is smaller than a preset second area threshold as a second target connected region; The second target connected area is changed from a background area to an unknown area to obtain a modified tripartite map.
5. The method according to claim 1, in, The cutout model is trained in the following way: Acquire a second training sample set, wherein the second training sample includes a second sample image, a second sample triplicate image, and a sample mask, wherein a human portrait is presented in the second sample image; The second sample image and the second sample tri-map in the second training sample in the second training sample set are used as inputs of the second initial model, and the sample mask corresponding to the input second sample image and the second sample tri-map is used as the expected output of the second initial model. The second initial model is trained using a machine learning method to obtain a cutout model.
6. A portrait cutout device, include: An acquisition unit, configured to acquire an original image showing a human portrait; The input unit is configured to input the original image into a pre-trained tripartite graph generation model to obtain a tripartite graph of the original image, wherein the tripartite graph generation model is trained in the following manner: obtaining a first training sample set, wherein the first training sample includes a first sample image and a first sample tripartite graph, and the first sample tripartite graph is generated by performing a morphological transformation on a mask corresponding to the first sample image, and the mask includes a channel with transparency and semi-transparency of the image; using the first sample image and the first sample tripartite graph in the first training sample in the first training sample set as input and expected output of a first initial model, respectively, and training the first initial model using a machine learning method to obtain a tripartite graph generation model; a determining unit configured to determine a mask of the original image based on the original image, a tripartite map of the original image, and a pre-trained matting model; The interception unit is configured to intercept a portrait image from the original image using the mask of the original image.
7. The device according to claim 6, in, The determining unit is further configured to determine a mask of the original image based on the original image, a tripartite map of the original image, and a pre-trained matting model in the following manner: Correcting the tripartite map of the original image; The original image and the corrected tripartite map are input into a pre-trained cutout model to obtain a mask of the original image.
8. An electronic device, include: one or more processors; a storage device having one or more programs stored thereon, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 5.
9. A computer readable medium having a computer program stored thereon, in, When the program is executed by a processor, the method according to any one of claims 1 to 5 is implemented.
Citation Information
Patent Citations
CAD (computer-aided design) people counting method based on FAST (features from accelerated segment test)
CN103577875A
Method and device for detecting whether face in image is sheltered
CN103996203A
Full-automatic three-dimensional scene construction method based on single image
CN104134234A
Automatic matting algorithm and device
CN107452010A
Image processing method and device, and mobile terminal
CN108961279A
