Image processing method and device, storage medium and electronic equipment
Through an automated image processing method, discretely enclosed graphics using color replacement technology in masked images, the problems of low efficiency and high cost in the prior art are solved, and automatic image cutting and efficient image processing are realized.
Patent Information
- Application Number
- CN202411537716.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-30
- Publication Date
- 2025-02-07
AI Technical Summary
In the prior art, the cutout process requires manual operation, resulting in high cost and low efficiency.
By obtaining the mask image of the image to be processed, traversing the pixels in the mask image, color replacement is performed according to the colors of the foreground and background, and the graphics are discretely closed, thereby automatically completing the cutout.
Automatic cutout is realized, which improves cutout efficiency, reduces labor costs, and has strong versatility, and can automatically process any closed graphics in the image.
Smart Images

Figure CN119399238A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of image processing technology, and in particular to an image processing method, an image processing device, a computer-readable storage medium, a computer program product, and an electronic device. Background Art
[0002] Cutting out an image means extracting the part of interest from an image. Taking game development as an example, in game development, it is usually necessary to separate the art assets corresponding to the game elements from the image.
[0003] In the related art, it is necessary to manually perform image cutting in an image processing tool. The manual operation of image cutting increases the labor cost of image cutting and has low efficiency.
[0004] It should be noted that the information disclosed in the above background technology section is only used to enhance the understanding of the background of the present disclosure, and therefore may include information that does not constitute the prior art known to ordinary technicians in the field. Summary of the invention
[0005] The purpose of the present disclosure is to provide an image processing method and device, a computer-readable storage medium, a computer program product and an electronic device, thereby improving the efficiency of image cutting at least to a certain extent.
[0006] Other features and advantages of the present disclosure will become apparent from the following detailed description, or may be learned in part by the practice of the present disclosure.
[0007] According to a first aspect of the present disclosure, there is provided an image processing method, comprising: acquiring an image to be processed, determining a mask image of the image to be processed according to a foreground and a background of the image to be processed, wherein the foreground is a first color in the mask image, and the background is a second color in the mask image; traversing the first pixel in the mask image, and when the color of the first pixel currently traversed is the first color, updating a first quantity, and performing a first color replacement on the mask image based on the first pixel currently traversed and a current replacement color to discretize closed figures in the mask image, wherein the current replacement color is determined according to the first quantity currently updated, and the first quantity is used to count the number of closed figures in the mask image; and obtaining a cutout image of the image to be processed based on the discrete result of the closed figures in the mask image.
[0008] Optionally, the step of performing a first color replacement on the mask image based on the currently traversed first pixel and the current replacement color to discretize the closed figures in the mask image comprises: a current replacement color determination step: determining the current replacement color according to the currently updated first quantity; a first replacement starting point determination step: taking the currently traversed first pixel as the current replacement starting point, and when the third color of the current replacement starting point is the same as the current replacement color, obtaining the adjacent pixel points of the current replacement starting point; and when the third color of the current replacement starting point is different from the current replacement color, modifying the third color of the current replacement starting point to the current replacement color and obtaining the current replacement color. The adjacent pixel points of the replacement starting point; the first to-be-replaced set updating step: when the color of the adjacent pixel points is the same as the color of the current replacement starting point, the adjacent pixel points are added to the first to-be-replaced set; the first replacement step: traversing the elements in the first to-be-replaced set, changing the color of the currently traversed element to the current replacement color, and obtaining the adjacent pixel points of the currently traversed element; repeating the first to-be-replaced set updating step and the first replacement step until the last element in the first to-be-replaced set is traversed, and the color of the last element is changed to the current replacement color, and determining that a first color replacement is completed to discretize a closed figure.
[0009] Optionally, performing a first color replacement on the mask image to discretize a closed figure in the mask image includes: after completing a first color replacement, continuing to traverse the next first pixel in the mask image, when the color of the next first pixel is the first color, updating the first number, and performing the first color replacement on the mask image based on the next first pixel, when the color of the next pixel is the second color, continuing to traverse the next pixel in the mask image, repeating the process until the last pixel in the mask image is traversed, determining that the first color replacement is completed, to complete the discretization of the closed figure in the mask image.
[0010] Optionally, the current replacement color is determined by: determining the current replacement color according to a difference between the first color and the first number of current updates; or determining the current replacement color according to a sum of the first number of current updates and a first preset value.
[0011] Optionally, obtaining the cutout image of the image to be processed based on the discrete results of the closed figures in the mask image includes: obtaining a target image based on the discrete results of the closed figures in the mask image; determining an index value range based on a second quantity, wherein the second quantity is determined based on the value of the first quantity obtained after the first color replacement is completed; for any index value in the index value range, determining a current color threshold based on the index value; performing a first processing on a second pixel in the target image based on the current color threshold, and obtaining a cutout image of the closed figure corresponding to the index value based on the first processing result.
[0012] Optionally, the first processing of the second pixel in the target image according to the current color threshold includes: calculating a first difference between the color value of each second pixel in the target image and the current color threshold; when the first difference corresponding to the second pixel in the target image is greater than a second preset value, eliminating the second pixel; when the first difference corresponding to the second pixel is less than or equal to the second preset value, retaining the second pixel.
[0013] Optionally, obtaining the cutout image of the closed figure corresponding to the index value based on the first processing result includes: obtaining a target candidate image according to the first processing result; merging the target candidate image and the image to be processed, and obtaining the cutout image of the closed figure corresponding to the index value according to the merging result.
[0014] Optionally, performing a first processing on the second pixel in the target image according to the current color threshold, and obtaining a cutout image of the closed figure corresponding to the index value based on the first processing result includes: calculating a first difference between the color value of each second pixel in the target image and the current color threshold; when the first difference corresponding to the second pixel in the target image is greater than a second preset value, modifying the color of the second pixel to a fourth color, and when the first difference corresponding to the second pixel is less than or equal to the second preset value, modifying the color of the second pixel to a fifth color, to obtain a first candidate image; performing a transparent processing on the third pixel whose color is the fourth color in the first candidate image, to obtain a second candidate image; merging the second candidate image and the image to be processed, to obtain a third candidate image according to the merging result; obtaining a bounding box in the third candidate image, cropping the third candidate image according to the bounding box, and obtaining a cutout image of the closed figure corresponding to the index value according to the cropping result.
[0015] Optionally, the method further includes: when the color of the first pixel currently traversed is the second color, continuing to traverse the next pixel in the mask image.
[0016] According to a second aspect of the present disclosure, an image processing device is provided, comprising: a mask image determination module, configured to obtain an image to be processed, and determine a mask image of the image to be processed according to the foreground and background of the image to be processed, wherein the foreground is a first color in the mask image, and the background is a second color in the mask image; a first color replacement module, configured to traverse the first pixel in the mask image, and when the color of the currently traversed first pixel is the first color, update a first quantity, and perform a first color replacement on the mask image based on the currently traversed first pixel and the current replacement color to discretize closed figures in the mask image, wherein the current replacement color is determined according to the currently updated first quantity, and the first quantity is used to count the number of closed figures in the mask image; a cutout module, configured to obtain a cutout image of the image to be processed based on the discretization result of the closed figures in the mask image.
[0017] According to a third aspect of the present disclosure, a computer program product comprising instructions is provided, which, when executed on a computer, enables the computer to execute the steps of the image processing method as described in the first aspect.
[0018] According to a fourth aspect of the present disclosure, a computer-readable storage medium is provided, on which a computer program is stored. When the program is executed by a processor, the image processing method as described in the first aspect of the above embodiment is implemented.
[0019] According to a fifth aspect of an embodiment of the present disclosure, an electronic device is provided, comprising: a processor; and a storage device for storing one or more programs, which, when executed by the one or more processors, enables the one or more processors to implement the image processing method as described in the first aspect of the above embodiment.
[0020] It can be seen from the above technical solutions that the image processing method, image processing device, and computer-readable storage medium, computer program product, and electronic device for implementing the image processing method in the exemplary embodiments of the present disclosure have at least the following advantages and positive effects:
[0021] In the technical solutions provided by some embodiments of the present disclosure, the current replacement color of the color replacement can be determined by counting the closed figures, so that the closed figures in the image can be discretized based on the current replacement color, and then the cutout image of the image to be processed can be obtained according to the discretization result of the closed figures. Compared with the related art, on the one hand, the present disclosure can automatically cutout the image to be processed, thereby improving the efficiency of cutout; on the other hand, the present disclosure can automatically cutout any closed figure in the image to be processed by discretizing the closed figures in the mask image, thereby meeting the different needs of users and having strong versatility.
[0022] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] The accompanying drawings herein are incorporated into the specification and constitute a part of the specification, illustrate embodiments consistent with the present disclosure, and together with the specification are used to explain the principles of the present disclosure. Obviously, the accompanying drawings described below are only some embodiments of the present disclosure, and for ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without creative work.
[0024] Figure 1 A schematic diagram showing an exemplary system architecture to which embodiments of the present disclosure may be applied;
[0025] Figure 2 A schematic flow chart showing an image processing method according to an exemplary embodiment of the present disclosure;
[0026] Figure 3 A schematic diagram showing an image to be processed in an exemplary embodiment of the present disclosure;
[0027] Figure 4 A schematic diagram showing a mask image in an exemplary embodiment of the present disclosure;
[0028] Figure 5 A schematic flow chart showing a method for first color replacement in an exemplary embodiment of the present disclosure;
[0029] Figure 6 A schematic diagram showing a replacement image obtained after a first color replacement is completed in an exemplary embodiment of the present disclosure;
[0030] Figure 7 A schematic flow chart showing a method for obtaining a cutout image according to a discrete result in an exemplary embodiment of the present disclosure;
[0031] Figure 8 A schematic flow chart showing a method for obtaining a cutout image of a closed figure corresponding to an index value in an exemplary embodiment of the present disclosure;
[0032] Fig. 9 A schematic diagram showing the composition of an image processing device in an exemplary embodiment of the present disclosure;
[0033] Fig.10 A schematic structural diagram of an electronic device in an exemplary embodiment of the present disclosure is shown. DETAILED DESCRIPTION
[0034] Example embodiments will now be described more fully with reference to the accompanying drawings. However, example embodiments can be implemented in a variety of forms and should not be construed as being limited to the examples set forth herein; on the contrary, these embodiments are provided so that the present disclosure will be more comprehensive and complete, and the concepts of the example embodiments are fully conveyed to those skilled in the art. The described features, structures, or characteristics may be combined in one or more embodiments in any suitable manner. In the following description, many specific details are provided to provide a full understanding of the embodiments of the present disclosure. However, those skilled in the art will appreciate that the technical solutions of the present disclosure may be practiced while omitting one or more of the specific details, or other methods, components, devices, steps, etc. may be adopted. In other cases, known technical solutions are not shown or described in detail to avoid obscuring various aspects of the present disclosure.
[0035] The terms "a", "an", "the" and "said" are used in this specification to indicate the presence of one or more elements / components / etc.; the terms "including" and "having" are used to express an open-ended inclusion and mean that additional elements / components / etc. may exist in addition to the listed elements / components / etc.; the terms "first" and "second" etc. are used only as labels and are not intended to limit the quantity of their objects.
[0036] In addition, the accompanying drawings are only schematic illustrations of the present disclosure and are not necessarily drawn to scale. The same reference numerals in the figures represent the same or similar parts, and their repeated description will be omitted. Some of the block diagrams shown in the accompanying drawings are functional entities and do not necessarily correspond to physically or logically independent entities. These functional entities can be implemented in software form, or implemented in one or more hardware modules or integrated circuits, or implemented in different networks and / or processor devices and / or microcontroller devices.
[0037] Cutting can extract objects of interest from images. For example, in the process of developing games based on art assets, individual art assets can be separated from the atlas.
[0038] In the related art, to complete this operation, one needs to manually cut out the image in an image processing tool, complete cropping and splitting, and finally output and save it locally. The process is very tedious and complicated, and inefficient.
[0039] In order to solve the above problems, the present invention provides an image processing method and device, which can be applied to Figure 1 In the system architecture of the exemplary application environment shown.
[0040] like Figure 1As shown, the system architecture 100 may include a terminal device 110 and a server 120. The terminal device 110 may be a terminal device such as a smart phone, a tablet computer, a desktop computer, a laptop computer, a smart wearable device, etc. The server 120 generally refers to a background system that provides related services of the image processing method in this exemplary embodiment, and may be a server or a cluster formed by multiple servers. The terminal 110 and the server 120 may be connected via a wired or wireless communication link to exchange data.
[0041] In an exemplary embodiment, the above-mentioned image processing method can be performed by the terminal device 110. Accordingly, the image processing device can be set in the terminal 110 to implement the corresponding module functions. For example, the user uses the terminal device 110 to obtain the image to be processed, and the terminal device 110 determines the mask image of the image to be processed according to the foreground and background of the image to be processed, traverses the first pixel in the mask image, and when the color of the first pixel currently traversed is the first color corresponding to the foreground, updates the first quantity, and replaces the first color of the mask image based on the first pixel currently traversed and the current replacement color, so as to discretize the closed figure in the mask image, and then outputs the cutout image of the image to be processed based on the discretization result of the closed figure in the mask image.
[0042] In an exemplary embodiment, the above-mentioned image processing method can be performed by the server 120. Accordingly, the image processing device can be set in the server 120 to implement the corresponding module functions. For example, the server 120 obtains the image to be processed, determines the mask image of the image to be processed according to the foreground and background of the image to be processed, traverses the first pixel in the mask image, and when the color of the first pixel currently traversed is the first color corresponding to the foreground, updates the first quantity, and replaces the first color of the mask image based on the first pixel currently traversed and the current replacement color, so as to discretize the closed figure in the mask image, and then outputs the cutout image of the image to be processed based on the discretization result of the closed figure in the mask image.
[0043] In an exemplary embodiment, the above-mentioned image processing method can also be performed jointly by the terminal device 110 and the server 120. Accordingly, some modules of the image processing device can be set in the terminal device 110, and some modules can be set in the server 120 to realize the corresponding module functions. For example, the terminal device 110 shoots the image to be processed, determines the mask image of the shot image to be processed, and sends the image to be processed and the mask image to the server 120. After receiving the image to be processed and the mask image, the server 120 traverses the first pixel in the mask image, and when the color of the first pixel currently traversed is the first color corresponding to the foreground, the first number is updated, and the mask image is replaced with the first color based on the first pixel currently traversed and the current replacement color, so as to discretize the closed figure in the mask image, and then outputs the cutout image of the image to be processed based on the discretization result of the closed figure in the mask image and the received image to be processed, and sends the cutout image to the terminal device 110.
[0044] It should be understood that Figure 1 The number of terminal devices and servers in the example is only illustrative. Any number of terminal devices and servers may be provided as required. For example, the server 120 may be an independent physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms.
[0045] Figure 2 A schematic diagram showing a flow chart of an image processing method in an exemplary embodiment of the present disclosure is shown. Figure 2 , the method comprising:
[0046] Step S210, obtaining an image to be processed, and determining a mask image of the image to be processed according to a foreground and a background of the image to be processed, wherein the foreground is a first color in the mask image, and the background is a second color in the mask image;
[0047] Step S220, traversing the first pixel in the mask image, when the color of the currently traversed first pixel is the first color, updating the first number, and performing the first color replacement on the mask image based on the currently traversed first pixel and the current replacement color, so as to discretize the closed figures in the mask image, wherein the current replacement color is determined according to the currently updated first number, and the first number is used to count the number of closed figures in the mask image;
[0048] Step S230: obtaining a matting image of the image to be processed based on the discrete results of the closed figures in the mask image.
[0049] exist Figure 2 In the technical solution provided by the illustrated embodiment, the current replacement color of the color replacement can be determined by counting the closed figures, so that the closed figures in the image can be discretized based on the current replacement color, and then the cutout image of the image to be processed can be obtained according to the discretization result of the closed figures. Compared with the related art, on the one hand, the present disclosure can automatically cutout the image to be processed, thereby improving the efficiency of cutout; on the other hand, the present disclosure can automatically cutout any closed figure in the image to be processed by discretizing the closed figures in the mask image, thereby meeting the different needs of users and having strong versatility.
[0050] Next, the specific implementation of "step S210, obtaining the image to be processed, and determining the mask image of the image to be processed according to the foreground and background of the image to be processed" will be described in detail.
[0051] In an exemplary embodiment, the foreground is a first color in the mask image, and the background is a second color in the mask image. The first color and the second color can be customized according to user needs, as long as the first color and the second color are different, such as the first color is white and the second color is black.
[0052] For example, a machine learning model can be pre-trained based on training samples and sample labels, so that the machine learning model learns the foreground features and background features in the image. After the training is completed, the image to be processed can be processed by the trained machine learning model to determine the foreground and background in the image to be processed. For example, the image to be processed can be used as the input of the machine learning obtained by training, and the foreground and background in the image to be processed can be determined by the output of the model. Then, the foreground area is assigned a first color, and the background area is assigned a second color to obtain a mask image of the image to be processed. Among them, the machine learning model can be customized according to needs, such as a convolutional neural network model, SegmentAnything (any segmentation model, which can segment any object in any image), etc., and this exemplary embodiment does not specifically limit this.
[0053] For example, the image to be processed can be Figure 3 The image shows a collection of in-game art assets. Figure 3 The mask image of the image to be processed can be as shown Figure 4 As shown. For example, if the first color is white and the second color is black, Figure 3 The image to be processed is input into the trained machine learning model, and the machine learning model recognizes Figure 3 The foreground area and background area of the image to be processed are shown in FIG. 1 , and then the identified foreground area is assigned white, and the identified background area is assigned black, so as to obtain the following Figure 4 The mask image shown.
[0054] In an exemplary embodiment, the image to be processed can be customized according to user needs. For example, the image to be processed can be a collection of art assets in the game, a face image, etc. The image to be processed can be an image taken or produced in advance, or the image to be processed can be an image taken in real time by the terminal device 110. This exemplary embodiment does not make any special limitations on this.
[0055] Below, the specific implementation method of "step S220, traversing the first pixel in the mask image, when the color of the currently traversed first pixel is the first color, updating the first quantity, and replacing the first color of the mask image based on the currently traversed first pixel and the current replacement color to discretize the closed figures in the mask image" is described in detail.
[0056] In an exemplary implementation, the initial value of the first quantity may be 0, and updating the first quantity may include increasing the value of the first quantity by one quantity unit.
[0057] For example, there may be multiple foreground areas of interest in the mask image obtained in step S210, that is, multiple images need to be cut out from one image to be processed. In other words, when the mask image includes multiple foreground areas, the multiple foreground areas need to be discretized to distinguish different foreground areas, so that each foreground area is cut out separately in the subsequent step S230. Figure 4 As shown, each foreground area is a closed figure, which refers to a figure of any shape with all edges connected and without gaps or openings.
[0058] In step S220, when the color of the first pixel in the traversed mask image is the first color, the first quantity can be updated first, and then the current replacement color is determined based on the currently updated first quantity, and then the colors of the first pixel in the mask image and the pixels in the area connected to the first pixel (that is, the closed figure to which the first pixel belongs) are all replaced with the current replacement color.
[0059] In the present disclosure, the color of closed figures in the mask image can be replaced by a depth-first search color replacement method, so that the closed figures corresponding to each foreground area can be discretely distinguished by different replacement colors. For example, in the present disclosure, for different closed figures, the replacement colors of different closed figures can be determined according to the index value of each closed figure in the image, so that the foreground area colors of different closed figures are replaced with different colors to discretize different closed figures. The index value of the closed figure in the image can be understood as the number of closed figures in the mask image that are currently being replaced. For example, if the first closed figure is currently being replaced, then the index value of the closed figure is 1, and if the second closed figure is currently being replaced, then the index value of the closed figure is 2.
[0060] Exemplarily, the index value can be determined based on the currently updated first quantity, that is, the index value of each closed figure is the value of the most recently updated first quantity when the color of the closed figure is replaced. In other words, the current replacement color can be determined based on the currently updated first quantity. After the first color replacement is completed, the value of the first quantity is the total number of closed figures in the mask image, that is, the first quantity can be used to count the number of closed figures in the mask image, and during the first color replacement process, the currently updated first quantity can be used to indicate the index value of the currently replaced closed figure in all closed figures in the mask image.
[0061] In the process of traversing the first pixel in the mask image, when the color of the first pixel currently traversed is the first color, it means that the boundary point of the foreground area is currently traversed. Since each time a first pixel with the color of the first color is traversed in the mask image, the first number is first increased by 1 and then a color replacement is performed. The color replacement process has changed the color of the closed figure to which the first pixel belongs from the first color to its corresponding current replacement color. Therefore, after a color replacement is completed, in the process of sequentially traversing other first pixels in the mask image, if the first pixel with the color of the first color is traversed again, the first pixel must belong to a new closed figure that has not been color replaced. Therefore, the first number can be increased by 1 to indicate that a new closed figure has appeared in the mask image. At the same time, the value after the first number is increased by 1 is the index value of the newly appeared closed figure in all closed figures in the mask image. In this way, the closed figures in the mask image are counted in real time by color replacement, and the current count value is applied to the color replacement of the closed figures, so that different closed figures can be discretized.
[0062] For example, Figure 5 A schematic flow chart of a method for first color replacement in an exemplary embodiment of the present disclosure is shown. Figure 5, the method may include steps S510 to S550.
[0063] In step S510, a current replacement color is determined according to the currently updated first quantity.
[0064] Among them, step S510 can be understood as a current replacement color determination step, that is, step S510 is mainly used to determine the current replacement color according to the currently updated first quantity.
[0065] In an exemplary manner, the current replacement color may be determined by: determining the current replacement color according to a difference between the first color and the first number currently updated.
[0066] Take the first color being white as an example, that is, the values of the three RGB color channels of the first color are all 255, then the color value of each channel in the three color channels of the first color is subtracted from the first quantity to obtain the current replacement color. That is, the current replacement color is the color corresponding to (255,255,255)-(current first quantity, current first quantity, current first quantity). Taking the current first quantity being 1 as an example, the RGB value of the current replacement color is (254,254,254), where RGB refers to the three color channels of red, green, and blue.
[0067] In another exemplary embodiment, the method for determining the current replacement color may include: determining the current replacement color according to the sum of the currently updated first quantity and the first preset value.
[0068] The first preset value can be customized according to experience or needs, and the first preset value can be 5, 10, etc., but the first preset value should not be too large. If the first preset value is too large, the value of the replacement color will be greater than the maximum color value 255. Taking the first preset value of 5 as an example, when the current first number is 1, the RGB value of the current replacement color is (6,6,6), and when the current first number is 2, the RGB value of the current replacement color is (7,7,7).
[0069] Of course, the current replacement color can also be determined according to the current first quantity in other ways, such as determining the first quantity as the channel value of the RGB three color channels of the current replacement color. Taking the current first quantity as 1 as an example, the RGB value of the current replacement color can be (1,1,1). The current replacement color can also be determined according to the first quantity and a replacement color mapping table, such as pre-determining the replacement colors corresponding to different first quantities, and finding the replacement color corresponding to the current first quantity in the mapping table according to the current first quantity, and using it as the current replacement color. This exemplary embodiment does not specifically limit this.
[0070] In another exemplary embodiment, the current replacement color can be determined based on an arithmetic progression greater than 2 and the current first number. For example, the difference of the arithmetic progression is 3, that is, the arithmetic progression is 0, 3, 6, 9, 12, ..., 3n, n is an integer. When the current first number is 1, the RGB value of the current replacement color is (1, 1, 1). When the current first number is 2, the RGB value of the current replacement color is the sum of the current first number and the second value in the arithmetic progression, that is, (5, 5, 5). When the current first number is 3, the RGB value of the current replacement color is the sum of the current first number and the third value in the arithmetic progression, that is, (9, 9), and so on. In the process of color replacement, for a certain pixel, the color value error of its final replacement color and the current replacement color in the RGB three channels may be within 2 color values respectively, so determining the current replacement color based on an arithmetic progression with a difference greater than 2 can ensure that the color value difference between the replacement colors of different closed figures is greater than 2. In this way, the influence of color error during color replacement on the distinction between different closed figures can be avoided, the accuracy of closed figure discretization can be improved, and the accuracy of subsequent cutouts can be improved.
[0071] In another exemplary embodiment, before performing the first color replacement, a second color replacement may be performed on the mask image, and the total number of closed figures in the mask image may be determined through the second color replacement. Then, during the first color replacement process, the current replacement color may be determined based on the total number and the currently updated first number.
[0072] The specific implementation of the second color replacement refers to the specific implementation process of the first color replacement. The only difference between the specific implementation of the second color replacement and the specific implementation process of the first color replacement is that during the second color replacement process, the replacement color is the same each time, rather than being determined based on the currently updated first quantity. For example, the replacement color of each second color replacement is red until the last first pixel in the mask image is traversed, and it is determined that the second color replacement is completed. After the second color replacement is completed, the total number of closed figures in the mask image can be obtained based on the value of the first quantity after the second color replacement is completed.
[0073] After the total number of closed figures in the mask image is obtained according to the second color replacement, in the first color replacement process, the current replacement color of the current first color replacement can be determined in the following manner: determine the first quotient of the first color value of the first color and the total number, determine the first product of the first quotient and the current first number; determine the current replacement color according to the second difference between the first color and the first product. Taking the first color as white as an example, the first quotient of 255 divided by the total number can be calculated, and then the first product of the first quotient and the current first number can be calculated, and the RGB color value of the current replacement color can be (255-first product, 255-first product, 255-first product). In this way, the distinction of replacement colors of different closed figures can be increased, the influence of color error during color replacement on the distinction of different closed figures can be avoided, the accuracy of closed figure discreteness can be improved, and then the accuracy of subsequent cutouts can be improved. At the same time, it can also avoid the current replacement color being greater than the maximum color value (255) or less than the minimum color value (0), resulting in the situation that the replacement cannot be made or the replacement is wrong.
[0074] In step S520, the first pixel currently traversed is taken as the current replacement starting point. When the third color of the current replacement starting point is the same as the current replacement color, the adjacent pixel points of the current replacement starting point are obtained. When the third color of the current replacement starting point is different from the current replacement color, the third color of the current replacement starting point is modified to the current replacement color and the adjacent pixel points of the current replacement starting point are obtained.
[0075] Among them, step S520 can be understood as a first replacement starting point determination step, that is, the purpose of step S520 is to determine the first replacement starting point and obtain the adjacent pixel points of the first replacement starting point to determine the elements in the first set to be replaced.
[0076] For example, the first pixel currently traversed can be used as the current replacement starting point. When the third color of the current replacement starting point is the same as the current replacement color, the adjacent pixels of the current replacement starting point can be directly obtained, that is, the adjacent pixels of the first pixel currently traversed can be obtained; when the third color of the current replacement starting point is different from the current replacement color, the color of the current replacement starting point can be modified to the current replacement color, and the adjacent pixels of the current replacement starting point can be obtained.
[0077] In an exemplary embodiment, when the absolute value of the color value difference between the color of the current replacement starting point and the current replacement color is less than a third preset value, it is determined that the two colors are the same; otherwise, it is determined that the two colors are different.
[0078] In step S530, when the color of the adjacent pixel point is the same as the color of the current replacement starting point, the adjacent pixel point is added to the first to-be-replaced set.
[0079] Among them, step S530 can be understood as a first to-be-replaced set updating step, that is, it is necessary to update the elements in the first to-be-replaced set according to whether the color of the acquired adjacent pixel point is the same as the color of the current replacement starting point.
[0080] For example, when the color of the acquired adjacent pixel point is the same as the color of the current replacement starting point before color modification, that is, when the color of the acquired vector pixel point is also the first color, the adjacent pixel point is added to the first set to be replaced to update the elements in the first set to be replaced. When the color of the acquired adjacent pixel point is not the same as the color of the current replacement starting point before color modification, no processing is performed.
[0081] In an exemplary embodiment, adjacent pixel points can be determined by means of four neighborhoods or eight neighborhoods, that is, obtaining four adjacent upper, lower, left, and right pixel points or obtaining eight adjacent pixel points. This exemplary embodiment does not make any special limitation on this.
[0082] In step S540, the elements in the first to-be-replaced set are traversed, the color of the currently traversed element is modified to the current replacement color, and the adjacent pixel points of the currently traversed element are obtained, and the process goes to step S530.
[0083] Wherein, step S540 can be understood as the first replacement step, that is, through step S540, the color replacement of adjacent pixels is achieved.
[0084] For example, the elements in the first set to be replaced can be traversed, and the elements in the first set to be replaced can be taken out from the first set to be replaced in turn, and the color of the pixel indicated by the taken out element can be modified to the current replacement color, and then the adjacent pixel points of the pixel indicated by the element can be obtained, and then the process can be transferred to step S530, that is, according to whether the color of the adjacent pixel points of the traversed element is the same as the color of the current replacement starting point before the color modification, that is, the first color, then the obtained adjacent pixel points can be added to the first set to be replaced, and if they are not the same, no processing is performed. Then, the process of step S540 is continued, and after execution, the process can be transferred to step S530, and this is repeated until the last element in the first set to be replaced is traversed, that is, until the first set to be replaced is empty, and then the process can be transferred to step S550.
[0085] For example, the first set to be replaced is continuously updated, and each time the elements in the first set to be replaced are traversed, that is, each time an element is taken out from the first set to be replaced, the element is taken from the most recently updated first set to be replaced.
[0086] In step S550, after traversing to the last element in the first to-be-replaced set and changing the color of the last element to the current replacement color, it is determined that a first color replacement is completed to discretize a closed figure.
[0087] For example, in step S540, after obtaining the adjacent pixel points of the element currently traversed in the first set to be replaced, steps S530 and S540 can be repeated until the last element in the first set to be replaced is traversed, and the color of the last element is modified to the current replacement color, to determine that a first color replacement is completed to discretize a closed figure.
[0088] In an exemplary embodiment, after completing a first color replacement, continue to traverse the next first pixel in the mask image, when the color of the next first pixel is the first color, update the first number, and perform the first color replacement on the mask image based on the next first pixel, when the color of the next pixel is not the first color, continue to traverse the next pixel in the mask image, and repeat the process until the last pixel in the mask image is traversed, and it is determined that the first color replacement is completed to complete the discretization of the closed figure in the mask image.
[0089] Taking a quantity unit of 1 as an example, traverse the first pixel in the mask image, and when the color of the currently traversed first pixel is the second color, continue to traverse the next first pixel. When the color of the currently traversed first pixel is the first color, add 1 to the value of the first quantity, and perform a first color replacement on the mask image. The current replacement color of the current first color replacement is determined according to the first quantity obtained after the current update. That is to say, the replacement color of each first color replacement is different. When all the elements in the first to-be-replaced set updated according to the current replacement starting point are taken out, it is determined that the first color replacement is completed. Each time the first color replacement is completed, the next first pixel in the mask image is traversed, and then whether to continue traversing the next first pixel or to perform a first color replacement based on the currently traversed first pixel is determined according to whether the color of the next first pixel is the first color or the second color. At the same time, if the color of the next first pixel is the first color, the first quantity needs to be updated again, that is, the value of the first quantity is increased by 1 based on the previous update to obtain the currently updated first quantity, and the replacement color of the current color replacement is determined according to the currently updated first quantity. This process is repeated until the last first pixel in the mask image is traversed. If the color of the last first pixel is the second color, it is determined to directly end the first color replacement. If the color of the last first pixel is the first color, the last first color replacement is performed based on the last first pixel. After the last first color replacement is completed, the first color replacement is ended.
[0090] After the first color replacement is completed, the closed figures in the mask image can be discretized. That is, in the process of the first color replacement, each closed figure is replaced with a different color, so different closed figures can be discretized and distinguished by different colors.
[0091] For example, the first color is white, and the current replacement color is determined according to the difference between the first color and the first quantity currently updated. Figure 6 FIG. 1 is a schematic diagram showing a replacement image obtained after a first color replacement is completed in an exemplary embodiment of the present disclosure. Figure 6 , each closed shape in the mask image is replaced with a different color.
[0092] In an exemplary embodiment, when generating a mask image of an image to be processed, it is necessary to replace all foreground areas with the first color and all background areas with the second color. However, due to errors in the color replacement process, for example, if the first color is white and the second color is black, there are non-black and non-white pixels in the foreground area. Therefore, before traversing the first pixel of the mask image, the colors of all non-black pixels in the mask image can be set to white. That is, the colors of pixels in the mask image that are not the first color or the second color are set to the first color. In this way, it is ensured that all foreground areas are the first color, and the accuracy of color replacement is ensured, thereby improving the accuracy of cutout.
[0093] In an exemplary implementation, when traversing the first pixel in the mask image, if the color of the currently traversed first pixel is the second color, continue to traverse the next pixel in the mask image.
[0094] For example, when the color of the first pixel currently traversed is the second color, it means that the first pixel currently traversed belongs to the background area, and then no processing is performed, and the next first pixel is directly traversed.
[0095] In the present disclosure, by counting the closed figures with color replacement and applying the counted value to the mask value of each closed figure, a phase difference can be achieved, thereby discretizing each closed figure in the mask image, and then realizing automatic cutout, thereby improving the efficiency and accuracy of cutout.
[0096] The specific implementation of "step S230, obtaining a cutout image of the image to be processed based on the discrete results of the closed figures in the mask image" is described below.
[0097] For example, Figure 7 A schematic diagram of a method for obtaining a cutout image according to a discrete result in an exemplary embodiment of the present disclosure is shown. Figure 7 The method may include steps S710 to S740. Wherein:
[0098] In step S710, a target image is obtained based on the discretization result of the closed figures in the mask image.
[0099] For example, after the first color replacement is performed on the mask image, the image obtained after the first color replacement is determined as the target image, that is, Figure 6 The image shown may be an example of a target image.
[0100] In step S720, an index value range is determined according to the second quantity.
[0101] In an exemplary embodiment, the second number is determined according to the value of the first number obtained after the first color replacement is completed. That is, the second number is the total number of closed figures in the mask image. The index value range can be obtained according to the range from 1 to the second number. If the second number is 15, the index value range is the interval [1, 15].
[0102] In step S730, for any index value in the index value range, a current color threshold is determined according to the index value.
[0103] For example, the method of determining the current color threshold according to the index value corresponds to the method of determining the current replacement color, that is, the current color threshold can be understood as the current replacement color of the closed figure corresponding to the current index value.
[0104] For example, in the aforementioned solution, when performing the first color replacement, when the color value corresponding to the currently updated first quantity is directly determined as the current replacement color, the current color threshold can be directly determined according to the index value.
[0105] For another example, when the aforementioned scheme performs the first color replacement, if the method for determining the current replacement color of the current first color replacement is "determining the first quotient of the first color value and the total number, determining the first product of the first quotient and the current first quantity; determining the current replacement color based on the second difference between the first color and the first product", then after replacing other current first quantities with the current index value, the current color threshold corresponding to the current index value can be obtained in the same way.
[0106] Taking the second number of 15 as an example, the index values can be taken from 1 to 15, and then the cutout images of 15 closed figures can be automatically obtained, that is, the cutout images of all foreground objects in the image to be processed can be automatically obtained. Of course, one or more index values can also be specified from 1 to 15 to cut out the closed figures corresponding to the specified index values, and the cutout images of the closed figures corresponding to the specified index values can be automatically obtained.
[0107] In step S740, a first process is performed on the second pixel in the target image according to the current color threshold, and a cutout image of the closed figure corresponding to the index value is obtained based on the first process result.
[0108] Exemplarily, an implementation of performing a first processing on a second pixel in a target image may include: calculating a first difference between a color value of each second pixel in the target image and the current color threshold; when the first difference corresponding to a second pixel in the target image is greater than a second preset value, eliminating the second pixel; and when the first difference corresponding to the second pixel is less than or equal to the second preset value, retaining the second pixel.
[0109] Among them, the first preset value can be customized according to needs. The first preset value is used to measure the similarity between the color of the second pixel and the current color threshold, so the first preset value should not be set too large, such as 0, 1, 2, 3, etc.
[0110] For example, when the difference between the second color value of the second pixel and the current color threshold is greater than the second preset value, it indicates that the current second pixel does not belong to the pixel point in the closed figure corresponding to the current index value, so the current second pixel can be directly eliminated; otherwise, it indicates that the current second pixel belongs to the pixel point in the closed figure corresponding to the current index value, so the second pixel can be retained. After the processing is completed, an image containing only the closed figure corresponding to the current index value is obtained, which is the target candidate image described below.
[0111] Exemplarily, obtaining the cutout image of the closed figure corresponding to the index value based on the first processing result in step S740 includes: obtaining a target candidate image according to the first processing result; merging the target candidate image and the image to be processed, and obtaining the cutout image of the closed figure corresponding to the index value according to the merging result.
[0112] For example, the target candidate image is obtained based on the target image (i.e., the image after the first color replacement), so the target image and the image to be processed need to be merged before the closed figure in the image to be processed can be cut out, and the cutout image of the closed figure corresponding to the index value in step S730 can be obtained from the image to be processed. For example, the coordinate position of the second pixel in the retained target image in the target image can be recorded, and the image to be processed can be cropped based on the coordinate position, and the area corresponding to the coordinate position is retained in the image to be processed, thereby obtaining the corresponding cutout image.
[0113] Because the size information of the target image and the image to be processed is exactly the same, the coordinate position of the second pixel in the target image and the coordinate position of the closed figure indicated by the second pixel in the image to be processed are exactly the same. Therefore, the image to be processed can be cropped based on the set of coordinate positions of the second pixel point in the target candidate image in the target image to obtain a cutout image.
[0114] For example, Figure 8A schematic flow chart of a method for obtaining a cutout image of a closed figure corresponding to an index value in an exemplary embodiment of the present disclosure is shown. Figure 8 , the method may include steps S810 to S850. Wherein:
[0115] In step S810, a first difference between the color value of each second pixel in the target image and the current color threshold is calculated.
[0116] In step S820, when the first difference corresponding to the second pixel in the target image is greater than the second preset value, the color of the second pixel is changed to the fourth color; when the first difference corresponding to the second pixel is less than or equal to the second preset value, the color of the second pixel is changed to the fifth color to obtain a first candidate image.
[0117] In an exemplary embodiment, the fourth color and the fifth color can be customized according to the requirements, and the fourth color and the fifth color can be different. For example, the fourth color can be the first color mentioned above, and the fifth color can be the second color mentioned above.
[0118] In step S830, a third pixel whose color is the fourth color in the first candidate image is made transparent to obtain a second candidate image.
[0119] For example, pixels with the fourth color in the first candidate image are not pixels in the closed figure corresponding to the current index value, so they can be made transparent to obtain the second candidate image. That is to say, in the second candidate image, other areas outside the closed figure corresponding to the current index value are made transparent.
[0120] In step S840, the second candidate image and the image to be processed are merged, and a third candidate image is obtained according to the merging result.
[0121] For example, the second candidate image and the image to be processed have the same size, so the second candidate image and the image to be processed can be merged based on the same coordinate position, that is, the intersection of the second candidate image and the image to be processed is taken. In this way, since the other areas outside the closed figure corresponding to the current index value in the second candidate image are transparently processed, only the area corresponding to the closed figure will be displayed in the image to be processed.
[0122] In step S850, a bounding box in the third candidate image is obtained, the third candidate image is cropped according to the bounding box, and a cutout image of the closed figure corresponding to the index value is obtained according to the cropping result.
[0123] For example, the bounding box in the third candidate image can be extracted by any bounding box extraction method. Since only the area corresponding to the closed figure is displayed in the third candidate image, the bounding box in the third candidate image is the dividing line between the area corresponding to the closed figure and other areas. Therefore, in the third candidate image, the area inside the bounding box is retained and the area outside the bounding box is cropped, so that the cutout image of the closed figure corresponding to the index value in step S730 can be obtained.
[0124] Through the image processing method disclosed in the present invention, the foreground area in the image to be processed can be automatically cut out without any manual operation, thereby reducing labor costs and improving cutout efficiency.
[0125] In addition, it should be noted that the above-mentioned figures are only schematic illustrations of the processes included in the method according to an exemplary embodiment of the present invention, and are not intended to be limiting. It is easy to understand that the processes shown in the above-mentioned figures do not indicate or limit the time sequence of these processes. In addition, it is also easy to understand that these processes can be performed synchronously or asynchronously, for example, in multiple modules.
[0126] Furthermore, an exemplary embodiment of the present disclosure also provides an image processing device. Fig. 9 As shown, the image processing device 900 includes the following program modules: a mask image determination module 910, configured to obtain an image to be processed, and determine a mask image of the image to be processed according to the foreground and background of the image to be processed, wherein the foreground is a first color in the mask image, and the background is a second color in the mask image; a first color replacement module 920, configured to traverse the first pixel in the mask image, and when the color of the first pixel currently traversed is the first color, update the first number, and perform a first color replacement on the mask image based on the first pixel currently traversed and the current replacement color to discretize the closed figures in the mask image, the current replacement color is determined according to the first number currently updated, and the first number is used to count the number of closed figures in the mask image; a cutout module 930, configured to obtain a cutout image of the image to be processed based on the discrete result of the closed figures in the mask image.
[0127] In an exemplary implementation, based on the aforementioned embodiment, the mask image is subjected to a first color replacement based on the currently traversed first pixel and the current replacement color to discretize the closed figure in the mask image, including: a current replacement color determination step: determining the current replacement color according to the currently updated first quantity; a first replacement starting point determination step: taking the currently traversed first pixel as the current replacement starting point, and when the third color of the current replacement starting point is the same as the current replacement color, obtaining the adjacent pixel points of the current replacement starting point, and when the third color of the current replacement starting point is different from the current replacement color, modifying the third color of the current replacement starting point to the current replacement color and Obtain the adjacent pixel points of the current replacement starting point; first to-be-replaced set updating step: when the color of the adjacent pixel points is the same as the color of the current replacement starting point, add the adjacent pixel points to the first to-be-replaced set; first replacement step: traverse the elements in the first to-be-replaced set, modify the color of the currently traversed element to the current replacement color, and obtain the adjacent pixel points of the currently traversed element; repeat the first to-be-replaced set updating step and the first replacement step until the last element in the first to-be-replaced set is traversed, and the color of the last element is modified to the current replacement color, and determine that a first color replacement is completed to discretize a closed figure.
[0128] In an exemplary embodiment, based on the aforementioned embodiment, the first color replacement of the mask image to discretize the closed figures in the mask image includes: after completing the first color replacement once, continuing to traverse the next first pixel in the mask image, when the color of the next first pixel is the first color, updating the first number, and performing the first color replacement of the mask image based on the next first pixel, when the color of the next pixel is the second color, continuing to traverse the next pixel in the mask image, repeating the process until the last pixel in the mask image is traversed, determining that the first color replacement is completed, to complete the discretization of the closed figures in the mask image.
[0129] In an exemplary embodiment, based on the aforementioned embodiment, the method for determining the current replacement color includes: determining the current replacement color based on the difference between the first color and the first quantity of the current update; or determining the current replacement color based on the sum of the first quantity of the current update and a first preset value.
[0130] In an exemplary embodiment, based on the aforementioned embodiments, the cutout module 930 may be specifically configured as follows: obtaining a target image based on discrete results of closed figures in the mask image; determining an index value range based on a second quantity, wherein the second quantity is determined based on the value of the first quantity obtained after the first color replacement is completed; for any index value in the index value range, determining a current color threshold based on the index value; performing a first processing on a second pixel in the target image based on the current color threshold, and obtaining a cutout image of the closed figure corresponding to the index value based on the first processing result.
[0131] In an exemplary embodiment, based on the aforementioned embodiment, the first processing of the second pixel in the target image according to the current color threshold includes: calculating the first difference between the color value of each second pixel in the target image and the current color threshold; when the first difference corresponding to the second pixel in the target image is greater than a second preset value, eliminating the second pixel; when the first difference corresponding to the second pixel is less than or equal to the second preset value, retaining the second pixel.
[0132] In an exemplary embodiment, based on the aforementioned embodiment, obtaining the cutout image of the closed figure corresponding to the index value based on the first processing result includes: obtaining the target candidate image according to the first processing result; merging the target candidate image and the image to be processed, and obtaining the cutout image of the closed figure corresponding to the index value according to the merging result.
[0133] In an exemplary embodiment, based on the aforementioned embodiment, the first processing is performed on the second pixel in the target image according to the current color threshold, and the cutout image of the closed figure corresponding to the index value is obtained based on the first processing result, including: calculating the first difference between the color value of each second pixel in the target image and the current color threshold; when the first difference corresponding to the second pixel in the target image is greater than a second preset value, the color of the second pixel is modified to a fourth color, and when the first difference corresponding to the second pixel is less than or equal to the second preset value, the color of the second pixel is modified to a fifth color to obtain a first candidate image; the third pixel in the first candidate image whose color is the fourth color is transparent to obtain a second candidate image; the second candidate image and the image to be processed are merged to obtain a third candidate image according to the merged result; the bounding box in the third candidate image is obtained, the third candidate image is cropped according to the bounding box, and the cutout image of the closed figure corresponding to the index value is obtained according to the cropping result.
[0134] In an exemplary implementation, based on the above-mentioned embodiment, the first color replacement module 920 may also be configured to: when the color of the first pixel currently traversed is the second color, continue to traverse the next pixel in the mask image.
[0135] The specific details of each part of the above-mentioned device have been described in detail in the implementation method of the method part. The undisclosed details can be found in the implementation method of the method part, so they will not be repeated here.
[0136] It should be noted that, although several modules or units of the device for action execution are mentioned in the above detailed description, this division is not mandatory. In fact, according to the exemplary embodiments of the present disclosure, the features and functions of two or more modules or units described above can be embodied in one module or unit. Conversely, the features and functions of one module or unit described above can be further divided into multiple modules or units to be embodied.
[0137] In addition, although the steps of the method in the present disclosure are described in a specific order in the drawings, this does not require or imply that the steps must be performed in this specific order, or that all the steps shown must be performed to achieve the desired results. Additionally or alternatively, some steps may be omitted, multiple steps may be combined into one step, and / or one step may be decomposed into multiple steps, etc.
[0138] The exemplary embodiments of the present disclosure also provide a computer program product, which includes a computer program, and when the computer program is executed by a processor, the image processing method is implemented.
[0139] In one embodiment, the computer program product may be a tangible product containing a computer program, such as a computer-readable storage medium storing a computer program. The readable storage medium may be a storage medium based on electrical, magnetic, optical, electromagnetic, infrared, or other signals, including but not limited to: random access memory (RAM), read-only memory (ROM), magnetic tape, floppy disk, flash memory (Flash), mechanical hard disk (HDD), solid-state drive (SSD), and the like. Exemplarily, the computer program product may be implemented as a non-volatile storage medium storing a computer program, such as a read-only memory, a NAND flash memory, and the like.
[0140] In one embodiment, the computer program product may be an intangible product including a computer program. Exemplarily, the computer program product may be implemented as a virtual digital product, such as a digital file storing an executable file, an installation package, etc. of the computer program.
[0141] The code of the computer program can be written in one or more programming languages. Programming languages such as C language, Java, C++, Python, etc. The program code can be executed entirely on the user computing device, or partially on the user computing device, or as a separate software package, or partially on the user computing device and partially on a remote computing device, or entirely on a remote computing device or server. In the case of a remote computing device, the remote computing device can be connected to the user computing device through any type of network, such as a local area network (LAN), a wide area network (WAN), etc., or can be connected to an external computing device (e.g., an Internet connection provided by an operator).
[0142] The computer program can be carried or transmitted by electrical, magnetic, optical, electromagnetic, infrared and other signals. The electronic device can convert the signal carrying the computer program into a digital signal, and then run the computer program. When the computer program runs on the electronic device, its code is used to make the electronic device execute (more specifically, the processor of the electronic device can execute) the method steps of various exemplary embodiments of the present disclosure, such as the above-mentioned image processing method, which includes the following steps: obtaining the image to be processed, determining the mask image of the image to be processed according to the foreground and background of the image to be processed, wherein the foreground is the first color in the mask image, and the background is the second color in the mask image; traversing the first pixel in the mask image, when the color of the first pixel currently traversed is the first color, updating the first number, and performing the first color replacement on the mask image based on the first pixel currently traversed and the current replacement color, so as to discretize the closed figures in the mask image, the current replacement color is determined according to the first number currently updated, and the first number is used to count the number of closed figures in the mask image; based on the discrete result of the closed figures in the mask image, obtaining the cutout image of the image to be processed.
[0143] By executing the above method steps through a computer program, on the one hand, the image to be processed can be automatically cut out, thereby improving the efficiency of cutout; on the other hand, by discretizing the closed figures in the mask image, any closed figures in the image to be processed can be automatically cut out, thereby meeting the different needs of users and having strong versatility.
[0144] The exemplary embodiments of the present disclosure also provide an electronic device, such as the terminal device 110 or the server 120 described above. The electronic device may include a processor and a memory. The memory stores executable instructions of the processor, such as a computer program. The processor executes the method steps of various exemplary embodiments of the present disclosure by executing the executable instructions. In addition, the electronic device may also include a display for displaying a graphical user interface.
[0145] Reference below Fig.10 , the electronic device is exemplarily described in the form of a general-purpose computing device. It should be understood that Fig.10 The electronic device 1000 shown is only an example and should not limit the functions and scope of use of the embodiments of the present disclosure.
[0146] like Fig.10 As shown, the electronic device 1000 may include: a processor 1010 , a memory 1020 , a bus 1030 , an I / O (input / output) interface 1040 , a network adapter 1050 , and a display 1060 .
[0147] The memory 1020 may include a volatile memory, such as a RAM 1021, a cache unit 1022, and may also include a non-volatile memory, such as a ROM 1023. The memory 1020 may also include one or more program modules 1024, such program modules 1024 include but are not limited to: an operating system, one or more application programs, other program modules, and program data, each of which or a combination thereof may include the implementation of a network environment. For example, the program module 1024 may include each module in the above-mentioned device.
[0148] The processor 1010 may include one or more processing units. For example, the processor 1010 may include an AP (Application Processor), a modem processor, a GPU (Graphics Processing Unit), an ISP (Image Signal Processor), a controller, an encoder, a decoder, a DSP (Digital Signal Processor), a baseband processor and / or an NPU (Neural-Network Processing Unit), etc.
[0149] The processor 1010 can be used to execute executable instructions stored in the memory 1020, such as the above-mentioned image processing method, which includes the following steps: obtaining an image to be processed, and determining a mask image of the image to be processed according to the foreground and background of the image to be processed, wherein the foreground is a first color in the mask image, and the background is a second color in the mask image; traversing the first pixel in the mask image, and when the color of the first pixel currently traversed is the first color, updating the first quantity, and performing a first color replacement on the mask image based on the first pixel currently traversed and the current replacement color to discretize the closed figures in the mask image, the current replacement color is determined according to the first quantity currently updated, and the first quantity is used to count the number of closed figures in the mask image; based on the discrete results of the closed figures in the mask image, obtaining a cutout image of the image to be processed.
[0150] By implementing the above method through a computer program, on the one hand, the image to be processed can be automatically cut out, thereby improving the efficiency of cutout; on the other hand, by discretizing the closed figures in the mask image, any closed figures in the image to be processed can be automatically cut out, thereby meeting the different needs of users and having strong versatility.
[0151] The bus 1030 is used to realize the connection between different components of the electronic device 1000, and may include a data bus, an address bus, and a control bus.
[0152] The electronic device 1000 can communicate with one or more external devices 1200 (eg, a keyboard, a mouse, an external controller, etc.) through the I / O interface 1040 .
[0153] The electronic device 1000 can communicate with one or more networks through the network adapter 1050. For example, the network adapter 1050 can provide mobile communication solutions such as 3G / 4G / 5G, or provide wireless communication solutions such as wireless LAN, Bluetooth, near field communication, etc. The network adapter 1050 can communicate with other modules of the electronic device 1000 through the bus 1030.
[0154] The electronic device 1000 may display a graphical user interface, such as a cutout image, through the display 1060 .
[0155] although Fig.10 Not shown, other hardware and / or software modules may also be provided in the electronic device 1000, including but not limited to: microcode, device drivers, redundant processors, external disk drive arrays, RAID systems, tape drives, and data backup storage systems.
[0156] In addition, the above-mentioned figures are only schematic illustrations of the processes included in the method according to the exemplary embodiments of the present disclosure, and are not intended to be limiting. It is easy to understand that the processes shown in the above-mentioned figures do not indicate or limit the time sequence of these processes. In addition, it is also easy to understand that these processes can be performed synchronously or asynchronously, for example, in multiple modules.
[0157] As can be seen from the above, the technical solution of the present disclosure can be implemented as a method, an apparatus, a system, a computer program product, a storage medium, an electronic device, etc. Those skilled in the art can understand that various aspects of the present disclosure can be specifically implemented in the following forms, namely: a complete hardware implementation, a complete software implementation (including firmware, microcode, etc.), or an implementation combining hardware and software, such as being respectively referred to as a "circuit", "module" or "system".
[0158] It should be understood that the present disclosure is not limited to the specific method steps or structures described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from the scope thereof. Those skilled in the art will easily think of other embodiments based on the specific embodiments provided by the present disclosure. Therefore, the specific embodiments provided by the present disclosure are only exemplary, and the scope and spirit of the present disclosure are indicated by the claims, and any variations, uses or adaptive changes of the present disclosure should be covered, which follow the general principles of the present disclosure and include common knowledge or customary technical means in the technical field that are not disclosed in the present disclosure.
Claims
1. An image processing method, characterized in that: include: Acquire an image to be processed, and determine a mask image of the image to be processed according to a foreground and a background of the image to be processed, wherein the foreground is a first color in the mask image, and the background is a second color in the mask image; Traversing the first pixel in the mask image, when the color of the currently traversed first pixel is the first color, updating the first quantity, and performing the first color replacement on the mask image based on the currently traversed first pixel and the current replacement color, so as to discretize the closed figures in the mask image, wherein the current replacement color is determined according to the currently updated first quantity, and the first quantity is used to count the number of closed figures in the mask image; Based on the discrete results of the closed figures in the mask image, a matting image of the image to be processed is obtained.
2. The method according to claim 1, characterized in that The step of performing a first color replacement on the mask image based on the currently traversed first pixel and the current replacement color to discretize the closed figure in the mask image includes: Current replacement color determination step: determining a current replacement color according to the first quantity currently updated; The first replacement starting point determination step: taking the first pixel currently traversed as the current replacement starting point, and when the third color of the current replacement starting point is the same as the current replacement color, obtaining the adjacent pixel points of the current replacement starting point; when the third color of the current replacement starting point is different from the current replacement color, changing the third color of the current replacement starting point to the current replacement color and obtaining the adjacent pixel points of the current replacement starting point; A first to-be-replaced set updating step: when the color of the adjacent pixel point is the same as the color of the current replacement starting point, the adjacent pixel point is added to the first to-be-replaced set; First replacement step: traversing the elements in the first to-be-replaced set, changing the color of the currently traversed element to the current replacement color, and obtaining adjacent pixel points of the currently traversed element; Repeat the first to-be-replaced set updating step and the first replacement step until the last element in the first to-be-replaced set is traversed, and the color of the last element is modified to the current replacement color, to determine that a first color replacement is completed to discretize a closed figure.
3. The image processing method according to claim 2, characterized in that: The performing a first color replacement on the mask image to discretize closed figures in the mask image includes: After completing a first color replacement, continue to traverse the next first pixel in the mask image, when the color of the next first pixel is the first color, update the first number, and perform the first color replacement on the mask image based on the next first pixel, when the color of the next pixel is the second color, continue to traverse the next pixel in the mask image, and repeat this process until the last pixel in the mask image is traversed, and it is determined that the first color replacement is completed, so as to complete the discretization of the closed figure in the mask image.
4. The image processing method according to claim 1 or 2, characterized in that: The current replacement color is determined by: Determine a current replacement color according to a difference between the first color and the first number of current updates; or The current replacement color is determined according to the sum of the first number currently updated and the first preset value.
5. The image processing method according to claim 1, characterized in that: The step of obtaining a cutout image of the image to be processed based on the discrete result of the closed figure in the mask image comprises: Obtaining a target image based on a discrete result of the closed figures in the mask image; determining an index value range according to a second number, where the second number is determined according to a value of the first number obtained after the first color replacement is completed; For any index value in the index value range, determining a current color threshold according to the index value; A first process is performed on a second pixel in the target image according to the current color threshold, and a cutout image of the closed figure corresponding to the index value is obtained based on the first process result.
6. The image processing method according to claim 5, characterized in that: The performing a first processing on the second pixel in the target image according to the current color threshold comprises: Calculating a first difference between a color value of each second pixel in the target image and the current color threshold; When the first difference corresponding to the second pixel in the target image is greater than a second preset value, the second pixel is eliminated; when the first difference corresponding to the second pixel is less than or equal to the second preset value, the second pixel is retained.
7. The image processing method according to claim 5, characterized in that: The step of obtaining a cutout image of a closed figure corresponding to the index value based on the first processing result includes: Obtaining a target candidate image according to the first processing result; The target candidate image and the image to be processed are merged, and a cutout image of the closed figure corresponding to the index value is obtained according to the merging result.
8. The image processing method according to claim 5, characterized in that: The performing a first processing on the second pixel in the target image according to the current color threshold, and obtaining a cutout image of the closed figure corresponding to the index value based on the first processing result comprises: Calculating a first difference between a color value of each second pixel in the target image and the current color threshold; When the first difference value corresponding to a second pixel in the target image is greater than a second preset value, the color of the second pixel is changed to a fourth color; when the first difference value corresponding to the second pixel is less than or equal to the second preset value, the color of the second pixel is changed to a fifth color, to obtain a first candidate image; Performing transparency processing on the third pixel of the first candidate image whose color is the fourth color to obtain a second candidate image; Merging the second candidate image and the image to be processed, and obtaining a third candidate image according to the merging result; A bounding box in the third candidate image is obtained, the third candidate image is cropped according to the bounding box, and a cutout image of the closed figure corresponding to the index value is obtained according to the cropping result.
9. The image processing method according to claim 1, characterized in that: The method further comprises: When the color of the first pixel currently traversed is the second color, continue to traverse the next pixel in the mask image.
10. An image processing device, characterized in that: include: A mask image determination module is configured to obtain an image to be processed, and determine a mask image of the image to be processed according to a foreground and a background of the image to be processed, wherein the foreground is a first color in the mask image, and the background is a second color in the mask image; a first color replacement module configured to traverse a first pixel in the mask image, and when the color of the currently traversed first pixel is the first color, update a first quantity, and perform a first color replacement on the mask image based on the currently traversed first pixel and a current replacement color to discretize closed figures in the mask image, wherein the current replacement color is determined according to the currently updated first quantity, and the first quantity is used to count the number of closed figures in the mask image; The cutout module is configured to obtain a cutout image of the image to be processed based on the discrete results of the closed figures in the mask image.
11. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 9 is implemented.
12. An electronic device, characterized in that: include: one or more processors; A storage device for storing one or more programs, which, when executed by the one or more processors, enables the one or more processors to implement the method according to any one of claims 1 to 9.