Method and system for automatically removing picture background
1. A method for automatically removing a picture background is characterized by comprising the following steps:
s1: acquiring a background picture to be removed, and wiping unnecessary internal details containing excessive noise points in the background picture to be removed;
s2: extracting object lines in the background picture to be removed by adopting any one method including a thick line extraction method and a thin line extraction method;
s3: acquiring the external contour of the object line, and drawing the external contour;
s4: filling the outer contour inner color as white RGB (255, 255, 255), filling the outer contour outer color as black RGB (0, 0, 0);
s5: when the background picture to be removed is a picture which does not comprise an alpha layer and comprises jpg and jpeg, a new alpha layer is created, when the background picture to be removed is a picture which comprises a png and comprises an alpha layer, the numerical value of the alpha layer is rewritten, the alpha layer is assigned to the numerical value of any other layer, wherein, because 255 in the alpha layer represents opaque and 0 represents transparent, the image inside the outer contour is displayed after setting, the image outside the outer contour is not displayed, namely the picture background is successfully removed.
2. The method according to claim 1, wherein in step S1, the unnecessary internal details including excessive noise in the background picture to be removed are erased, specifically: and wiping out the internal details in the background picture to be removed by adopting Gaussian blur.
3. The method according to claim 1, wherein in step S2, any method including the thick line extraction method and the thin line extraction method is adopted to extract object lines in the background picture to be removed, specifically:
the thick line extraction method adopts an edge detection algorithm canny and is applied to a scene which is expected to remove background lines and is smooth;
the fine line extraction method adopts an edge detection CNN model Dexined, and is applied to a scene which is expected to remove background lines and is fine.
4. The method for automatically removing the background of a picture according to claim 1, wherein in step S3, an outer contour of the object line is obtained and is depicted, specifically:
and acquiring the external contour of the object line by using a findContours function in Python, and drawing the external contour by using a drawContours function in Python.
5. A system for automatically removing background from a picture, comprising:
the internal detail removing module is used for acquiring a background picture to be removed and wiping unnecessary internal details containing excessive noise points in the background picture to be removed;
the object line extraction module is used for extracting the object lines in the background picture to be removed by adopting any one method including a thick line extraction method and a thin line extraction method;
the external contour drawing module is used for acquiring the external contour of the object line and drawing the external contour;
a color fill module to fill the outer contour interior color as white RGB (255, 255, 255) and the outer contour exterior color as black RGB (0, 0, 0)
and the alpha layer assignment module is used for creating a new alpha layer when the background picture to be removed is a picture which does not comprise the alpha layer and comprises jpg and jpeg, rewriting the numerical value of the alpha layer when the background picture to be removed is a picture which comprises png and comprises the alpha layer, and assigning the alpha layer to be the numerical value of any other layer, wherein 255 in the alpha layer represents opaque and 0 represents transparent, so that the image inside the outer contour is displayed after setting, and the image outside the outer contour is not displayed, namely, the picture background is successfully removed.
6. The system for automatically removing the background of the picture according to claim 5, wherein the internal detail removal module further comprises: and wiping out the internal details in the background picture to be removed by adopting Gaussian blur.
7. The system for automatically removing the background of the picture according to claim 5, wherein in the object line extraction module, the system further comprises: the thick line extraction method adopts an edge detection algorithm canny and is applied to a scene which is expected to remove background lines and is smooth; the fine line extraction method adopts an edge detection CNN model Dexined, and is applied to a scene which is expected to remove background lines and is fine.
8. The system for automatically removing background from pictures according to claim 5, wherein said external contouring module further comprises: and acquiring the external contour of the object line by using a findContours function in Python, and drawing the external contour by using a drawContours function in Python.
9. An electronic device comprising a processor and a memory, the memory having stored therein at least one instruction, at least one program, a set of codes, or a set of instructions, the at least one instruction, the at least one program, the set of codes, or the set of instructions being loaded and executed by the processor to implement the method of any one of claims 1-4.
10. A computer readable storage medium storing computer code which, when executed, performs the method of any of claims 1 to 4.
Background
Background removal of pictures is often required in the fields of movie taking, photo processing, fashion element design, and the like. However, no method which is fully automatic and can completely remove the background is available in the prior art.
Existing software typically removes the background from the picture by:
(1) deleting the manually selected area;
(2) manually selecting a point, and removing adjacent pixel points similar to the selected point;
(3) and manually selecting the area, and removing adjacent pixel points similar to the selected area.
However, the above methods all require manual operation, and the more complicated the picture edge is, the less effective the background removal is.
Disclosure of Invention
In view of the above problems, an object of the present invention is to provide a method and a system for automatically removing a background of a picture, which have the advantages of being able to automatically remove the background of the picture without manual operation, and having a good background removal effect for the picture with a complex edge.
The above object of the present invention is achieved by the following technical solutions:
a method for automatically removing a picture background comprises the following steps:
s1: acquiring a background picture to be removed, and wiping unnecessary internal details containing excessive noise points in the background picture to be removed;
s2: extracting object lines in the background picture to be removed by adopting any one method including a thick line extraction method and a thin line extraction method;
s3: acquiring the external contour of the object line, and drawing the external contour;
s4: filling the outer contour inner color as white RGB (255, 255, 255), filling the outer contour outer color as black RGB (0, 0, 0);
s5: when the background picture to be removed is a picture which does not comprise an alpha layer and comprises jpg and jpeg, a new alpha layer is created, when the background picture to be removed is a picture which comprises a png and comprises an alpha layer, the numerical value of the alpha layer is rewritten, the alpha layer is assigned to the numerical value of any other layer, wherein, because 255 in the alpha layer represents opaque and 0 represents transparent, the image inside the outer contour is displayed after setting, the image outside the outer contour is not displayed, namely the picture background is successfully removed.
Further, in step S1, the unnecessary internal details including excessive noise in the background picture to be removed are erased, specifically: and wiping out the internal details in the background picture to be removed by adopting Gaussian blur.
Further, in step S2, any method including the thick line extraction method and the thin line extraction method is adopted to extract the object lines in the background picture to be removed, specifically:
the thick line extraction method adopts an edge detection algorithm canny and is applied to a scene which is expected to remove background lines and is smooth;
the fine line extraction method adopts an edge detection CNN model Dexined, and is applied to a scene which is expected to remove background lines and is fine.
Further, in step S3, an outer contour of the object line is obtained, and the outer contour is drawn, specifically:
and acquiring the external contour of the object line by using a findContours function in Python, and drawing the external contour by using a drawContours function in Python.
A system for automatically removing background from a picture, comprising:
the internal detail removing module is used for acquiring a background picture to be removed and wiping unnecessary internal details containing excessive noise points in the background picture to be removed;
the object line extraction module is used for extracting the object lines in the background picture to be removed by adopting any one method including a thick line extraction method and a thin line extraction method;
the external contour drawing module is used for acquiring the external contour of the object line and drawing the external contour;
a color fill module to fill the outer contour interior color as white RGB (255, 255, 255) and the outer contour exterior color as black RGB (0, 0, 0)
and the alpha layer assignment module is used for creating a new alpha layer when the background picture to be removed is a picture which does not comprise the alpha layer and comprises jpg and jpeg, rewriting the numerical value of the alpha layer when the background picture to be removed is a picture which comprises png and comprises the alpha layer, and assigning the alpha layer to be the numerical value of any other layer, wherein 255 in the alpha layer represents opaque and 0 represents transparent, so that the image inside the outer contour is displayed after setting, and the image outside the outer contour is not displayed, namely, the picture background is successfully removed.
Further, in the internal detail removal module, the method further includes: and wiping out the internal details in the background picture to be removed by adopting Gaussian blur.
Further, in the object line extraction module, the method further includes: the thick line extraction method adopts an edge detection algorithm canny and is applied to a scene which is expected to remove background lines and is smooth; the fine line extraction method adopts an edge detection CNN model Dexined, and is applied to a scene which is expected to remove background lines and is fine.
Further, in the external contouring module, further comprising: and acquiring the external contour of the object line by using a findContours function in Python, and drawing the external contour by using a drawContours function in Python.
An electronic device comprising a processor and a memory, wherein at least one instruction, at least one program, a set of codes, or a set of instructions is stored in the memory, and wherein the at least one instruction, the at least one program, the set of codes, or the set of instructions is loaded and executed by the processor to implement the method as described above.
A computer readable storage medium storing computer code which, when executed, performs a method as described above.
Compared with the prior art, the invention has at least one of the following beneficial effects:
(1) the method for automatically removing the background of the picture comprises the following steps: s1: acquiring a background picture to be removed, and wiping unnecessary internal details containing excessive noise points in the background picture to be removed; s2: extracting object lines in the background picture to be removed by adopting any one method including a thick line extraction method and a thin line extraction method; s3: acquiring the external contour of the object line, and drawing the external contour; s4: filling the outer contour inner color as white RGB (255, 255, 255), filling the outer contour outer color as black RGB (0, 0, 0); s5: when the background picture to be removed is a picture which does not comprise an alpha layer and comprises jpg and jpeg, a new alpha layer is created, when the background picture to be removed is a picture which comprises a png and comprises an alpha layer, the numerical value of the alpha layer is rewritten, the alpha layer is assigned to the numerical value of any other layer, wherein, because 255 in the alpha layer represents opaque and 0 represents transparent, the image inside the outer contour is displayed after setting, the image outside the outer contour is not displayed, namely the picture background is successfully removed. The technical scheme has the advantages of being capable of removing the picture background in a full-automatic mode, free of manual operation and good in background removing effect for the pictures with complex edges.
Drawings
FIG. 1 is a flowchart illustrating an overall method for automatically removing a background of a picture according to the present invention;
FIG. 2 is a schematic diagram of the method for extracting thick lines by using the edge detection algorithm canny according to the present invention;
FIG. 3 is a schematic diagram of the fine line extraction by the edge detection CNN model Dexined according to the present invention;
FIG. 4 is a schematic representation of an unprocessed image of an original image according to the present invention;
FIG. 5 is a schematic diagram of an object line after the object line is extracted according to the present invention;
fig. 6 is a schematic diagram of an image after the background is finally removed according to the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, and it is obvious that the described embodiments are some embodiments of the present application, but not all embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
As used herein, the singular forms "a", "an", "the" and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise. It will be further understood that the terms "comprises" and/or "comprising," when used in this specification, specify the presence of stated features, integers, steps, operations, elements, and/or components, but do not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and/or groups thereof.
First embodiment
As shown in fig. 1, the present embodiment provides a method for automatically removing a background of a picture, which specifically includes the following steps:
s1: and acquiring a background picture to be removed, and wiping unnecessary internal details containing excessive noise points in the background picture to be removed.
Specifically, in the present embodiment, the internal details are first erased by using gaussian blur, otherwise, excessive noise occurs when the lines are extracted next. For example, if a cat is a picture of grass, the gaussian blur will remove details such as cat hairs and grass, so that when the cat is extracted next, the grass and cat have clear contour lines. If this step is not done, the details of the grass are not removed, and when the outer contour is subsequently traced, the grass will be identified as the outermost contour, and the extraction of the background will fail. If the gaussian blur is first made, the grass and cat are easily separated by removing unnecessary details. In such a case, the success rate of background removal is high, and the removal is relatively clean.
Among them, gaussian blur, also called gaussian smoothing, is a processing effect widely used in image processing software such as Adobe Photoshop, GIMP, and paint. The visual effect of the image generated by the blurring technique is like observing the image through a frosted glass, which is obviously different from the out-of-focus imaging effect of a lens and the effect in a common lighting shadow.
S2: and extracting the object lines in the background picture to be removed by adopting any one method including a thick line extraction method and a thin line extraction method.
The thick line extraction method adopts an edge detection algorithm canny in Python, and is applied to a scene which is expected to remove background lines and is smooth (figure 2); the fine line extraction method adopts an edge detection CNN model Dexined in Python, and is applied to a scene which is expected to remove background lines and is fine (figure 3).
S3: and acquiring the external contour of the object line, and drawing the external contour.
Specifically, in the present embodiment, the lines extracted in step S2 are removed except for the contour. And acquiring the external contour of the object line by using a findContours function in Python, and drawing the external contour by using a drawContours function in Python.
S4: the outer contour inner color is filled as white RGB (255, 255, 255), and the outer contour outer color is filled as black RGB (0, 0, 0).
Specifically, it is next necessary to fill the outline internal color to white, RGB (255, 255, 255), and the outline external color to black, RGB (0, 0, 0). If the original picture is jpg or jpeg, the original picture only has three layers without an alpha layer; if the original picture is png, the original picture has four layers, and an alpha layer is provided, wherein the alpha layer is a layer for managing transparency.
S5: when the background picture to be removed is a picture which does not comprise an alpha layer and comprises jpg and jpeg, a new alpha layer is created, when the background picture to be removed is a picture which comprises a png and comprises an alpha layer, the numerical value of the alpha layer is rewritten, the alpha layer is assigned to the numerical value of any other layer, wherein, because 255 in the alpha layer represents opaque and 0 represents transparent, the image inside the outer contour is displayed after setting, the image outside the outer contour is not displayed, namely the picture background is successfully removed.
In this embodiment, a new picture covered with an alpha layer is created, the filling color of the image portion of the alpha layer is set to be opaque, and the other background portions are set to be transparent. The method specifically comprises the following steps:
the method comprises the steps of obtaining an original background picture to be removed without any operation, assuming that the original picture is x wide y, if the original picture has no alpha layer, creating a new alpha layer, namely a matrix of x y 1, arbitrarily taking any one of the images processed in the steps S1-S4, assigning the alpha layer to be the same value as the image processed in the steps S1-S4, setting the layers to be 255 and 0 before, wherein in the alpha layer, 255 represents opaque and 0 represents transparent, so that the effect is that the image inside a contour can be displayed, the image outside the contour is not displayed, and the image background is successfully removed because the layers are set to be transparent. If the original picture has an alpha layer, rewriting the numerical value of the alpha layer according to the same method, because the layer has been set to be 255 and 0 values before, in the alpha layer, 255 represents opaque, and 0 represents transparent, after the alpha layer is covered on the original picture, the effect is that the image inside the outline is displayed, the image outside is not displayed, and because the layer has been set to be transparent, the background of the picture is successfully removed.
After the steps of the invention are processed, the image can be separated from the background and can be separated, thus being convenient for later edition and being used in the garment design. Specific examples are shown in fig. 4-6. Fig. 4 is an original image, and fig. 5 is a schematic diagram of object lines after the object lines in the image are extracted in step S2. Fig. 6 is a schematic diagram of an image after final background removal.
Second embodiment
The embodiment provides a system for automatically removing a picture background, which comprises:
the internal detail removing module 1 is used for acquiring a background picture to be removed and wiping unnecessary internal details containing excessive noise points in the background picture to be removed;
the object line extraction module 2 is configured to extract an object line in the background picture to be removed by using any one of a thick line extraction method and a thin line extraction method;
the external contour drawing module 3 is used for acquiring the external contour of the object line and drawing the external contour;
a color filling module 4 for filling the outer contour inner color with white RGB (255, 255, 255) and the outer contour outer color with black RGB (0, 0, 0)
and the alpha layer assignment module 5 is configured to create a new alpha layer when the background picture to be removed is a picture including jpg and jpeg but not including an alpha layer, rewrite an alpha layer numerical value when the background picture to be removed is a picture including png but including an alpha layer, and assign the alpha layer to a numerical value of any other layer, where 255 in the alpha layer represents opaque and 0 represents transparent, and after the setting, an image inside the outer contour is displayed and an image outside the outer contour is not displayed, that is, the picture background is successfully removed.
Further, in the internal detail removal module 1, the method further includes: and wiping out the internal details in the background picture to be removed by adopting Gaussian blur.
Further, in the object line extraction module 2, the method further includes: the thick line extraction method adopts an edge detection algorithm canny and is applied to a scene which is expected to remove background lines and is smooth; the fine line extraction method adopts an edge detection CNN model Dex i Ned, and is applied to a scene which is expected to remove background lines and is fine.
Further, in the external profiling module 3, the method further includes: and acquiring the external contour of the object line by using a findContours function in Python, and drawing the external contour by using a drawContours function in Python.
The present invention also provides a computer readable storage medium having stored thereon computer code which, when executed, performs the method as described above. Those skilled in the art will appreciate that all or part of the steps in the methods of the above embodiments may be implemented by associated hardware instructed by a program, which may be stored in a computer-readable storage medium, and the storage medium may include: read Only Memory (ROM), Random Access Memory (RAM), magnetic or optical disks, and the like.
The above description is only a preferred embodiment of the present invention, and the protection scope of the present invention is not limited to the above embodiments, and all technical solutions belonging to the idea of the present invention belong to the protection scope of the present invention. It should be noted that modifications and embellishments within the scope of the invention may occur to those skilled in the art without departing from the principle of the invention, and are considered to be within the scope of the invention.
The technical features of the embodiments described above may be arbitrarily combined, and for the sake of brevity, all possible combinations of the technical features in the embodiments described above are not described, but should be considered as being within the scope of the present specification as long as there is no contradiction between the combinations of the technical features.
It should be noted that the above embodiments can be freely combined as necessary. The foregoing is only a preferred embodiment of the present invention, and it should be noted that, for those skilled in the art, various modifications and decorations can be made without departing from the principle of the present invention, and these modifications and decorations should also be regarded as the protection scope of the present invention.
The software program of the present invention can be executed by a processor to implement the steps or functions described above. Also, the software programs (including associated data structures) of the present invention can be stored in a computer readable recording medium, such as RAM memory, magnetic or optical drive or diskette and the like. Additionally, some of the steps or functionality of the present invention may be implemented in hardware, for example, as circuitry that cooperates with the processor to perform various functions or steps. The method disclosed by the embodiment shown in the embodiment of the present specification can be applied to or realized by a processor. The processor may be an integrated circuit chip having signal processing capabilities. In implementation, the steps of the above method may be performed by integrated logic circuits of hardware in a processor or instructions in the form of software. The Processor may be a general-purpose Processor, including a Central Processing Unit (CPU), a Network Processor (NP), and the like; but also Digital Signal Processors (DSPs), Application Specific Integrated Circuits (ASICs), Field Programmable Gate Arrays (FPGAs) or other Programmable logic devices, discrete Gate or transistor logic devices, discrete hardware components. The various methods, steps and logic blocks disclosed in the embodiments of the present specification may be implemented or performed. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like. The steps of a method disclosed in connection with the embodiments of the present specification may be embodied directly in a hardware decoding processor, or in a combination of hardware and software modules in the decoding processor. The software module may be located in ram, flash memory, rom, prom, or eprom, registers, etc. storage media as is well known in the art. The storage medium is located in a memory, and a processor reads information in the memory and completes the steps of the method in combination with hardware of the processor.
Embodiments also provide a computer readable storage medium storing one or more programs that, when executed by an electronic system including a plurality of application programs, cause the electronic system to perform the method of embodiment one. And will not be described in detail herein.
Computer-readable media, including both non-transitory and non-transitory, removable and non-removable media, may implement information storage by any method or technology. The information may be computer readable instructions, data structures, modules of a program, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), Static Random Access Memory (SRAM), Dynamic Random Access Memory (DRAM), other types of Random Access Memory (RAM), Read Only Memory (ROM), Electrically Erasable Programmable Read Only Memory (EEPROM), flash memory or other memory technology, compact disc read only memory (CD-RO M), Digital Versatile Discs (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices, or any other non-transmission medium that can be used to store information that can be accessed by a computing device. As defined herein, a computer readable medium does not include a transitory computer readable medium (tr ansitory medium), such as a modulated data signal and a carrier wave.
The systems, devices, modules or units illustrated in the above embodiments may be implemented by a computer chip or an entity, or by a product with certain functions. One typical implementation device is a computer. In particular, the computer may be, for example, a personal computer, a laptop computer, a cellular telephone, a camera phone, a smartphone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or a combination of any of these devices. Computer-readable media, including both non-transitory and non-transitory, removable and non-removable media, may implement information storage by any method or technology. The information may be computer readable instructions, data structures, modules of a program, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PR AM), Static Random Access Memory (SRAM), Dynamic Random Access Memory (DRAM), other types of Random Access Memory (RAM), Read Only Memory (ROM), Electrically Erasable Programmable Read Only Memory (EEPROM), flash memory or other memory technology, compact disc read only memory (CD-ROM), Digital Versatile Discs (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices, or any other non-transmission medium that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include transitory computer readable media (transducer y media) such as modulated data signals and carrier waves. It should also be noted that the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises the element.
In addition, some of the present invention can be applied as a computer program product, such as computer program instructions, which when executed by a computer, can invoke or provide the method and/or technical solution according to the present invention through the operation of the computer. Program instructions which invoke the methods of the present invention may be stored on a fixed or removable recording medium and/or transmitted via a data stream on a broadcast or other signal-bearing medium and/or stored within a working memory of a computer device operating in accordance with the program instructions. An embodiment according to the invention herein comprises an apparatus comprising a memory for storing computer program instructions and a processor for executing the program instructions, wherein the computer program instructions, when executed by the processor, trigger the apparatus to perform a method and/or solution according to embodiments of the invention as described above.