Disclosure of Invention
The invention aims to disclose a matting method and a matting system based on edge processing, which solve the problems of how to improve the smoothness of the edge of an image obtained after matting and improve the matting efficiency.
In order to achieve the above purpose, the invention adopts the following technical scheme:
in one aspect, the invention provides a matting method based on edge processing, which comprises the following steps:
s1, acquiring coordinates of a position clicked by a user on an image to be scratched, and storing the coordinates into a contour coordinate set;
S2, acquiring image edges based on coordinates clicked by a user on an image to be scratched, and numbering each image edge;
s3, highlighting the edges of the images on the images to be scratched, and displaying the number of each image edge;
S4, acquiring a number input by a user, and storing coordinates of pixel points in the image edge corresponding to the number into a contour coordinate set;
S5, judging whether pixel points corresponding to elements in the contour coordinate set can form a closed area on the image to be scratched, if not, entering S6, and if so, entering S7;
S6, canceling highlighting and canceling display of numbers on the image to be scratched, highlighting pixel points in the contour coordinate set, and entering S1;
s7, generating a first mask layer based on pixel points in the closed area;
s8, performing smoothing treatment on the edge of the first mask layer to obtain a second mask layer;
And S9, fusing the second mask layer with the image to be scratched to obtain a contour scratched result layer.
Optionally, before S1, the method further includes:
And acquiring an image to be scratched, and establishing a rectangular coordinate system on the image to be scratched.
Optionally, establishing a rectangular coordinate system on the image to be scratched includes:
and establishing a rectangular coordinate system by taking the lower left corner of the image to be scratched as an origin of coordinates, taking the left side edge of the image to be scratched as an X axis, and taking the lower side edge of the image to be scratched as a Y axis.
Optionally, acquiring the image edge based on coordinates clicked by the user on the image to be scratched includes:
b is used for representing a pixel point corresponding to a coordinate clicked by a user on the image to be scratched;
The image edges are acquired as follows:
S21, obtaining pixel points in the 8 adjacent areas of b, wherein the absolute value of the difference value between the pixel points and the gray value of b is smaller than a set gray value threshold value, and storing the obtained pixel points into a set to be calculated;
S22, respectively acquiring image edges corresponding to each pixel point in the set to be calculated;
s23, taking all obtained image edges as image edges.
Optionally, the obtaining an image edge corresponding to each pixel point in the set to be calculated includes:
For a pixel point d in the set to be calculated, the corresponding image edge obtaining process includes:
s220, taking the pixel point d as a contrast pixel point;
s221, acquiring a set Ucmp of pixel points, of which the absolute value of the difference value between the gray value of the 8 adjacent areas of the contrast pixel points and the gray value of the contrast pixel points is smaller than a set gray value threshold value;
S221, judging whether Ucmp is an empty set, if so, storing the contrast pixel points into an image edge set, ending calculation, and if not, entering S222;
S222, taking the pixel point which does not belong to the image edge set and has the minimum difference absolute value of the gray value between the pixel point and the contrast pixel point in Ucmp as a new contrast pixel point, and entering S220.
Optionally, numbering each image edge includes:
starting from 1, all image edges are numbered consecutively, numbered as integer numbers.
Optionally, highlighting the image edges and displaying the number of each image edge includes:
Randomly selecting a display color for each image edge, wherein the display colors corresponding to different image edges are different;
respectively modifying the colors of all pixel points corresponding to the edges of the image into corresponding display colors on the image to be scratched;
The number of the image edge is displayed below the last pixel point of the image edge.
Optionally, highlighting the pixel point in the contour coordinate set includes:
And on the image to be scratched, modifying the color of the pixel point displaying the pixel point in the contour coordinate set into a preset color.
Optionally, generating the first mask layer based on the pixel points in the enclosed area includes:
S81, generating a blank image with the same resolution as the image to be scratched, wherein the gray values of all pixel points in the blank image are set to be 0;
s82, acquiring a set Upix of coordinates of pixel points in the closed area on an image to be scratched;
S83, in the blank image, setting the gray value of the pixel point corresponding to the coordinates in the Upix to be 1, and obtaining a first mask layer.
On the other hand, the invention provides a matting system based on edge processing, which comprises a coordinate acquisition module, an image edge acquisition module, a first display control module, a number acquisition module, a judgment module, a second display control module, a first generation module, a second generation module and a matting module;
The coordinate acquisition module is used for acquiring coordinates of positions clicked by a user on the image to be scratched and storing the coordinates into the contour coordinate set;
The image edge acquisition module is used for acquiring image edges based on coordinates clicked by a user on an image to be scratched, and numbering each image edge;
the first display control module is used for highlighting the edges of the images on the images to be scratched and displaying the numbers of each image edge;
The number acquisition module is used for acquiring a number input by a user and storing coordinates of pixel points in the image edge corresponding to the number into the contour coordinate set;
the judging module is used for judging whether the pixel points corresponding to the elements in the contour coordinate set can form a closed area on the image to be scratched;
The second display control module is used for canceling the highlighting and the display of the canceling numbers on the image to be scratched when the pixel points corresponding to the elements in the contour coordinate set cannot form a closed area on the image to be scratched, and highlighting the pixel points in the contour coordinate set;
The first generation module is used for generating a first mask layer based on pixel points in a closed area when the pixel points corresponding to elements in the contour coordinate set can enclose the closed area on the image to be scratched;
the second generation module is used for carrying out smoothing treatment on the edge of the first mask layer to obtain a second mask layer;
The matting module is used for fusing the second mask layer with the image to be matting to obtain a contour matting result layer.
The beneficial effects are that:
The selection of the contour matting edge area is more intelligent and reasonable, and the edge area of the image is subjected to smoothing treatment, so that the separated image edge area is smoother, and the display effect of the image is better.
In addition, in the process of selecting the edges of the image by the user, the invention intelligently provides various possible image edges for the user through the set algorithm, and then the user can intelligently realize the automatic acquisition of the image edges only by selecting the image edges which appear, without clicking the pixels belonging to the image edges by the user, thereby effectively improving the image matting efficiency.
Detailed Description
The following description of the embodiments of the present invention will be made clearly and completely with reference to the accompanying drawings, in which it is apparent that the embodiments described are only some embodiments of the present invention, but not all embodiments. The components of the embodiments of the present invention generally described and illustrated in the figures herein may be arranged and designed in a wide variety of different configurations. Thus, the following detailed description of the embodiments of the invention, as presented in the figures, is not intended to limit the scope of the invention, as claimed, but is merely representative of selected embodiments of the invention. All other embodiments, which can be made by a person skilled in the art without making any inventive effort, are intended to be within the scope of the present invention.
Contour matting is one of the most common operations in image processing, drawing a closed region on an image, separating the closed region on the image into separate layers, and preparing for later image synthesis. At present, the traditional contour matting is realized by using Xfermode, and the general implementation scheme is to draw a rectangular area and then make conversion of intersection result output with the original image, so that the rectangular area on the image is separated into separate image layers.
Embodiment one:
In one embodiment shown in fig. 1, the present invention provides a matting method based on edge processing, including:
s1, acquiring coordinates of a position clicked by a user on an image to be scratched, and storing the coordinates into a contour coordinate set;
S2, acquiring image edges based on coordinates clicked by a user on an image to be scratched, and numbering each image edge;
s3, highlighting the edges of the images on the images to be scratched, and displaying the number of each image edge;
S4, acquiring a number input by a user, and storing coordinates of pixel points in the image edge corresponding to the number into a contour coordinate set;
S5, judging whether pixel points corresponding to elements in the contour coordinate set can form a closed area on the image to be scratched, if not, entering S6, and if so, entering S7;
S6, canceling highlighting and canceling display of numbers on the image to be scratched, highlighting pixel points in the contour coordinate set, and entering S1;
s7, generating a first mask layer based on pixel points in the closed area;
s8, performing smoothing treatment on the edge of the first mask layer to obtain a second mask layer;
And S9, fusing the second mask layer with the image to be scratched to obtain a contour scratched result layer.
Compared with the prior art, in the image obtained by the matting, the edge area is selected more intelligently and reasonably, and the edge area of the image is subjected to smoothing treatment, so that the edge area of the separated image is smoother, and the display effect of the image is better.
In addition, in the process of selecting the edges of the image by the user, the invention intelligently provides various possible image edges for the user through the set algorithm, and then the user can intelligently realize the automatic acquisition of the image edges only by selecting the image edges which appear, without clicking the pixels belonging to the image edges by the user, thereby effectively improving the image matting efficiency.
Furthermore, the image edge which can be selected is generated for the user through the algorithm, so that the accuracy of the user in edge contour selection is improved.
Optionally, before S1, the method further includes:
And acquiring an image to be scratched, and establishing a rectangular coordinate system on the image to be scratched.
Specifically, in the present invention, all coordinates are coordinates in a rectangular coordinate system.
Optionally, establishing a rectangular coordinate system on the image to be scratched includes:
and establishing a rectangular coordinate system by taking the lower left corner of the image to be scratched as an origin of coordinates, taking the left side edge of the image to be scratched as an X axis, and taking the lower side edge of the image to be scratched as a Y axis.
Further, the left side here means the leftmost side, and the lower side means the lowest value side.
Optionally, acquiring the image edge based on coordinates clicked by the user on the image to be scratched includes:
b is used for representing a pixel point corresponding to a coordinate clicked by a user on the image to be scratched;
The image edges are acquired as follows:
S21, obtaining pixel points in the 8 adjacent areas of b, wherein the absolute value of the difference value between the pixel points and the gray value of b is smaller than a set gray value threshold value, and storing the obtained pixel points into a set to be calculated;
S22, respectively acquiring image edges corresponding to each pixel point in the set to be calculated;
s23, taking all obtained image edges as image edges.
Specifically, the set gray value threshold is 3.
By setting the gray value threshold, the pixel points with very high similarity with the pixel point b are all stored in the set to be calculated, and then all the image edges meeting the requirements can be obtained according to the set to be calculated.
Optionally, the obtaining an image edge corresponding to each pixel point in the set to be calculated includes:
For a pixel point d in the set to be calculated, the corresponding image edge obtaining process includes:
s220, taking the pixel point d as a contrast pixel point;
s221, acquiring a set Ucmp of pixel points, of which the absolute value of the difference value between the gray value of the 8 adjacent areas of the contrast pixel points and the gray value of the contrast pixel points is smaller than a set gray value threshold value;
S221, judging whether Ucmp is an empty set, if so, storing the contrast pixel points into an image edge set, ending calculation, and if not, entering S222;
S222, taking the pixel point which does not belong to the image edge set and has the minimum difference absolute value of the gray value between the pixel point and the contrast pixel point in Ucmp as a new contrast pixel point, and entering S220.
In the above-mentioned acquisition process, the present invention constantly changes the positions of the contrast pixel points, so that the image edge can extend along the possible edge direction, thereby obtaining the image edge.
Optionally, numbering each image edge includes:
starting from 1, all image edges are numbered consecutively, numbered as integer numbers.
Optionally, highlighting the image edges and displaying the number of each image edge includes:
Randomly selecting a display color for each image edge, wherein the display colors corresponding to different image edges are different;
respectively modifying the colors of all pixel points corresponding to the edges of the image into corresponding display colors on the image to be scratched;
The number of the image edge is displayed below the last pixel point of the image edge.
Specifically, for example, if there are a total of 5 image edges, the corresponding number is an integer of 1 to 5. One at a time from the image edges that are not numbered, then numbered until all image edges are numbered.
In particular, the display colors should be relatively different from each other, so that the user can recognize different image edges at a burst.
Optionally, highlighting the pixel point in the contour coordinate set includes:
And on the image to be scratched, modifying the color of the pixel point displaying the pixel point in the contour coordinate set into a preset color.
Specifically, the preset color may be a color different from all display colors.
Optionally, generating the first mask layer based on the pixel points in the enclosed area includes:
S81, generating a blank image with the same resolution as the image to be scratched, wherein the gray values of all pixel points in the blank image are set to be 0;
s82, acquiring a set Upix of coordinates of pixel points in the closed area on an image to be scratched;
S83, in the blank image, setting the gray value of the pixel point corresponding to the coordinates in the Upix to be 1, and obtaining a first mask layer.
In the blank image, a rectangular coordinate system is established in the same mode as the image to be scratched, so that the coordinates in the blank image and the coordinates in the image to be scratched can be used without coordinate conversion, and the mask layer generation efficiency is higher.
Specifically, after the blank image is generated, the pixel value of the pixel point of the area needing to be scratched is set to be 1, so that only the target area in the image to be scratched can be reserved in the subsequent image operation.
Optionally, smoothing the edge of the first mask layer to obtain a second mask layer, including:
Acquiring a set Uedg of pixel points, wherein the distance between the pixel points and the edge in the first mask layer is smaller than or equal to a preset distance;
And respectively carrying out smoothing treatment on each pixel point in the Uedg to obtain a second mask layer.
Specifically, the invention screens the pixel points at the edge of the first mask layer through the preset distance, so that the pixel points can be subjected to smoothing processing in the subsequent calculation process.
Optionally, the determining process of the pixel point corresponding to the edge of the first mask layer includes:
for a pixel u in the first mask layer, if the pixel u only includes one pixel with a gray value of 0, the pixel u is a pixel at the edge of the first mask layer.
Further, the preset distance is 3.
Optionally, smoothing is performed on each pixel point in Uedg to obtain a second mask layer, including:
for the pixel point v in Uedg, the gray value after the smoothing process is calculated by using the following function:
wherein, grayv,af and grayv,bf respectively represent the gray values of the pixel point v after the smoothing process and before the smoothing process, distv represents the distance between the pixel point v and the edge of the first mask layer, and distpreset represents the preset distance.
Specifically, in the invention, the closer the distance from the edge is, the smaller the gray value after smoothing is, so that when the second mask layer is used for carrying out operation with the image to be scratched, the edge of the obtained scratched area is smoother, and a better display effect is obtained.
Optionally, in the first mask layer, blurring and etching may be performed on the pixel points in the Uedg to obtain a second mask image.
Optionally, fusing the second mask layer with the image to be scratched to obtain a contour scratched result layer, including:
And multiplying the second mask layer with the image to be scratched to obtain a contour scratched result layer.
Specifically, in the invention, if the image to be scratched is stored in an RGB format, the gray values of the pixel points in the second mask layer are multiplied by the red component, the green component and the red component of the pixel points in the image to be scratched, and the pixel values of the components obtained after multiplication are recalculated into the RGB color space.
Embodiment two:
The invention provides an edge processing-based matting system, which comprises a coordinate acquisition module, an image edge acquisition module, a first display control module, a number acquisition module, a judgment module, a second display control module, a first generation module, a second generation module and a matting module, wherein the first display control module is used for acquiring a number of a target object;
The coordinate acquisition module is used for acquiring coordinates of positions clicked by a user on the image to be scratched and storing the coordinates into the contour coordinate set;
The image edge acquisition module is used for acquiring image edges based on coordinates clicked by a user on an image to be scratched, and numbering each image edge;
the first display control module is used for highlighting the edges of the images on the images to be scratched and displaying the numbers of each image edge;
The number acquisition module is used for acquiring a number input by a user and storing coordinates of pixel points in the image edge corresponding to the number into the contour coordinate set;
the judging module is used for judging whether the pixel points corresponding to the elements in the contour coordinate set can form a closed area on the image to be scratched;
The second display control module is used for canceling the highlighting and the display of the canceling numbers on the image to be scratched when the pixel points corresponding to the elements in the contour coordinate set cannot form a closed area on the image to be scratched, and highlighting the pixel points in the contour coordinate set;
The first generation module is used for generating a first mask layer based on pixel points in a closed area when the pixel points corresponding to elements in the contour coordinate set can enclose the closed area on the image to be scratched;
the second generation module is used for carrying out smoothing treatment on the edge of the first mask layer to obtain a second mask layer;
The matting module is used for fusing the second mask layer with the image to be matting to obtain a contour matting result layer.
In the several embodiments provided by the present application, it should be understood that the disclosed systems, devices, and methods may be implemented in other manners. The above-described apparatus embodiments are merely illustrative, for example, the division of the units is merely a logical function division, and there may be other manners of division in actual implementation, and for example, multiple units or components may be combined or integrated into another system, or some features may be omitted, or not performed. Alternatively, the coupling or direct coupling or communication connection shown or discussed with each other may be through some communication interface, device or unit indirect coupling or communication connection, which may be in electrical, mechanical or other form.
The units described as separate units may or may not be physically separate, and units shown as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
In addition, each functional unit in the embodiments of the present invention may be integrated in one processing unit, or each unit may exist alone physically, or two or more units may be integrated in one unit.
The functions, if implemented in the form of software functional units and sold or used as a stand-alone product, may be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention may be embodied essentially or in a part contributing to the prior art or in a part of the technical solution, in the form of a software product stored in a storage medium, comprising several instructions for causing a computer device (which may be a personal computer, a server, a network device, etc.) to perform all or part of the steps of the method according to the embodiments of the present invention. The storage medium includes a U disk, a removable hard disk, a Read-only memory (ROM), a random access memory (RAM, randomAccessMemory), a magnetic disk, an optical disk, or other various media capable of storing program codes.
The foregoing is merely illustrative of the present invention, and the present invention is not limited thereto, and any person skilled in the art will readily recognize that variations or substitutions are within the scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.