A kind of garbage mask border rendering method of tiled
Technical field
The invention belongs to the image/video treatment technology, be specifically related to a kind of garbage mask border rendering method of tiled.
Background technology
In the image/video process software, image/video is synthesized or carries out mask process during special effect processing is a kind of demand very widely.Common this mask can generate out or obtain out from the mask images of a special use by the key stunt, but so under many circumstances mask is not enough, also need to be used for to create out a mask by the method for manual drawing, this mask generally is referred to as garbage mask.
A garbage mask is made of some orderly end points (end point), per two continuous end points constitute a curve, along the working direction of end points order positive dirction, otherwise be contrary direction, each end points has two reference mark (control point), one is forward reference mark (forward control point), one is reverse reference mark (backward control point), the forward reference mark is used to control the shape of the forward curve of its place end points, and reverse reference mark is used to control the shape of the reverse curve of its place end points.Therefore, the shape of each bar curve is by two end points and two reference mark decisions, and they are actually three Beizer curves, and a garbage mask is described out by the closed curve that some three Beizer curves constitute.It is as follows to describe three Beizer curves with parametric equation:
x(t)=a
x·t
3+b
x·t
2+c
x·t+d
x
y(t)=a
y·t
3+b
y·t
2+c
y·t+d
y
T is the parameter of parametric equation in the following formula, and its scope is [0,1], a
x, b
xA series of coefficients are that the coordinate by the end points of this curve and reference mark determines out.
Except the fundamental element of describing a garbage mask is arranged: the Bezier extra curvature of end points and its defined, the element that control garbage mask boundary shape also will be arranged, this element is referred to as frontier point (edgepoint), frontier point has two attributes: position (position) and distance (distance), the parameter position of this frontier point of determining positions on garbage mask boundary curve, garbage mask boundary curve all is made up of the Bezier curve, and the parameter value (parameter value of parametric equation) of the position attribution of the frontier point Bezier curve that it was positioned at just adds the sequence number of this Bezier curve, such as a position is that 1.6 frontier point just means it between second and the 3rd curve that end points constituted of garbage mask (starting point is 0), and is on parametric t is that aspect of 0.6; Distance has determined the distance of the normal direction of the point of this frontier point above curve away from curve, and it is an outer boundary point that distance is represented this for positive number, is used for determining the outer boundary shape of garbage mask; Then representing this for negative is an inner boundary point, is used for determining the inner boundary shape of garbage mask.The border of whole garbage mask is decided by the shape that inner and outer boundary point institute envelope comes out.
So, the outside of a garbage mask and inner how the differentiation? here, still be that right side (counter clockwise direction or clockwise direction in other words conj.or perhaps) stipulates which side is the outside of garbage mask along the left side that positive dirction is seen generally, and it is called outside direction with Bezier curve among the garbage mask.Left side such as our regulation curve is an outside direction, that is to say that so the Bezier curve is the perimeter of garbage mask along the left side that positive dirction is seen, the right side is an interior zone.
Each frontier point has two coordinates, is respectively coordinate on coordinate and the border on the curve.Coordinate is meant the coordinate of that point of the correspondence of this frontier point on garbage mask on the curve, and this can directly obtain by its position attribution and the parametric equation of its place Bezier curve; Coordinate is meant that the normal direction of that point of the correspondence of this frontier point above garbage mask is away from the coordinate of its that point so far away apart from attribute of curve on the border, it can be by coordinate points on the curve tangent line side clockwise or be rotated counterclockwise 90 degree (for the outer boundary point, if the outside direction of garbage mask is the left side then is rotated counterclockwise, otherwise turn clockwise, so then just in time opposite to inner boundary point) be offset it again and so far calculate coordinate on its border apart from attribute.So coordinate is (x on the curve of a frontier point if calculated
0, y
0) and this unit tangent vector be (x
t, y
t), it is as follows to calculate on its border Coordinate Calculation method so:
x′=x
0+dist·(x
t·cos(θ)-y
t·sin(θ))
y′=y
0+dist·(x
t·sin(θ)+y
t·cos(θ))
In the following formula, dist represents the absolute value of this frontier point apart from attribute, and θ is-pi/2 or pi/2, and this depends on and turns clockwise or be rotated counterclockwise.Fig. 2 has shown a garbage mask and its frontier point.Garbage mask among Fig. 2 has 5 end points, three frontier points.Its outside direction is the left side, and frontier point A and frontier point C are outer boundary points and frontier point B is an inner boundary point as can be seen.
Creating this garbage mask generally is the curve of drawing a closure by the user, and filling the gray-scale map that this closed curve obtains then is exactly a garbage mask.It is too stiff to fill closed curve gray-scale map border afterwards, make it seem more level and smooth so need carry out certain modification to the border, general in addition also will control the length of border transition makes the border look certain thickness is arranged, in order to realize that these most of softwares tend to use morphologic contraction and expansion algorithm and Gaussian Blur Processing Algorithm, whole garbage mask seems too fuzzy when fog-level is bigger but such implementation method can cause, its border is also more fixing in addition, lacks ability of regulation and control flexibly.
Summary of the invention
The objective of the invention is to defective, a kind of garbage mask border rendering method of tiled is provided, make the garbage mask border after playing up more clear, control more flexible at prior art.
Technical scheme of the present invention is as follows: a kind of garbage mask border rendering method of tiled comprises the steps:
(1) all the Bezier curves among the garbage mask is divided into plurality of sub segments;
(2) determine the outside direction of garbage mask;
(3) go out the outer boundary distance and the inner boundary distance of all waypoints according to the information calculations of frontier point;
(4) be that outer/inner the border of its correspondence and outer/inner arm of angle circle of its initial end points correspondence are drawn by unit with every Bezier curve.
Further, the garbage mask border rendering method of aforesaid tiled, wherein, the method that in the step (1) the Bezier curve is divided into plurality of sub segments is as follows: for a Bezier curve, advance forward for step-length by 1/30, calculate the position tangent line vector of current this point, and carry out dot product with the unit tangent vector of this vector and previous fixed waypoint and calculate, if the value that draws is less than threshold value cos (2.5 π/180), then current point just is defined as a waypoint, not so this point is just ignored in the past, continues then by 1/30 step-length this process of repetition of advancing forward, up to the end of this Bezier curve.
Further, the garbage mask border rendering method of aforesaid tiled, wherein, the step of the outside direction of definite garbage mask is as follows in the step (2):
1. calculate the bounding box of garbage mask, obtain the maximum y value of bounding box and the intermediate value yc of minimum y value then;
2. find out this horizontal line of y=yc and the intersection point of all Bezier curves above the garbage mask, and find out leftmost that intersection point in these intersection points;
3. obtain the tangent line vector of leftmost that intersection point in its Bezier curve, carry out the cross product computing with the unit vector of x axle positive dirction then, if the z value of cross product operation result greater than 0 then outside direction is the right side, otherwise is the left side.
Further, the garbage mask border rendering method of aforesaid tiled, wherein, the fall into a trap outer boundary distance of calculating waypoint and the method for inner boundary distance of step (3) is: that outer/inner frontier point of position attribution numerical value minimum above garbage mask, find next outer/inner the frontier point adjacent with it, if the position attribution of these two frontier points is respectively p0 and p1, apart from attribute is respectively d 0 and d1, travel through all waypoints between these two frontier points, outer/inner frontier distance computing formula of each waypoint is d=d0+ (p-p0)/(p1-p0) (d1-d0), wherein, p is the position of the waypoint that calculated.
Further, the garbage mask border rendering method of aforesaid tiled, wherein, outer/inner arm of angle circle of the initial end points correspondence of Bezier curve described in the step (4) is meant the forward tangent line and the inconsistent fan-like pattern border of causing of reverse tangential direction of this end points, perpendicular to the forward tangent line and the reverse tangent line of this end points, the length of waist is outer/inner frontier distance of this end points to two waists on this fan-like pattern border respectively; The method of drawing outer/inner arm of angle circle is: with this fan-like pattern boundary demarcation is some tri patchs, calculate the gray-scale value on these tri patch summits then, gray-scale value for that summit of end points correspondence is 1, for outer/inner borderline summit gray-scale value is exactly 0, utilizes gouraud shading colorize method to play up these tri patchs then.
Further, the garbage mask border rendering method of aforesaid tiled, wherein, the method on outer/inner border of the drafting Bezier curve correspondence described in the step (4) is: at each sub-line segment, find position attribution all frontier points between the starting and ending waypoint of this sub-line segment; Obtain each frontier point outside/coordinate on the inner boundary; With frontier point on curve coordinate points and outer/inner borderline coordinate points as the summit of tri patch, calculate the gray-scale value on these tri patch summits; Utilize gouraud shading colorize method to play up these tri patchs then.
Further, the garbage mask border rendering method of aforesaid tiled, wherein, when drawing the outer boundary of Bezier curve correspondence in the step (4), for the outer boundary point between the starting and ending waypoint of each sub-line segment, always calculate coordinate on its outer boundary according to coordinate on its curve and its external parties apart from attribute and current garbage mask; For the inner boundary point between the starting and ending waypoint of each sub-line segment, outer boundary according to the broad sense outer boundary point of its front and the broad sense outer boundary point of back obtains its outer boundary distance apart from carrying out linear interpolation, and then calculating coordinate on its outer boundary, described broad sense outer boundary point comprises that outer boundary puts coordinate points on the outer boundary corresponding with waypoint of coordinate points on the pairing border.
Further, the garbage mask border rendering method of aforesaid tiled, wherein, when drawing the inner boundary of Bezier curve correspondence in the step (4), for the inner boundary point between the starting and ending waypoint of each sub-line segment, always calculate coordinate on its inner boundary according to coordinate on its curve and its internal side apart from attribute and current garbage mask; For the outer boundary point between the starting and ending waypoint of each sub-line segment, inner boundary according to the broad sense inner boundary point of its front and the broad sense inner boundary point of back obtains its inner boundary distance apart from carrying out linear interpolation, and then calculating coordinate on its inner boundary, described broad sense inner boundary point comprises that inner boundary puts coordinate points on the inner boundary corresponding with waypoint of coordinate points on the pairing border.
Further again, the garbage mask border rendering method of aforesaid tiled, wherein, in the step (4) in the process on outer/inner the border of drawing Bezier curve correspondence, each sub-line segment is divided into plurality of sections by all frontier points between its starting and ending waypoint, the outer boundary of each section constitutes a quadrilateral, each quadrilateral on average is divided into the experimental process quadrilateral along the trend of Bezier curve, the quantity computation method of dividing is as follows: establish the length that l0 represents this quadrilateral before waist, l1 represents the length of that waist of back, so sub-quadrilateral quantity
Calculate this a little tetragonal apex coordinate and gray-scale value by linear interpolation after drawing sub-quadrilateral quantity, and then utilize identical method that each sub-quadrilateral is divided into the plurality of sub quadrilateral again along Bezier normal to curve direction, just whole quadrilateral can be divided into the plurality of sub quadrilateral at last; Each sub-quadrilateral is divided into two adjacent tri patchs, utilizes gouraud shading colorize method to play up these tri patchs.
Beneficial effect of the present invention is as follows: the garbage mask border rendering method of tiled provided by the present invention, realized the self-adaptation division of the sub-line segment of Bezier curve among the garbage mask, outer/inner the border of every Bezier curve correspondence and outer/inner arm of angle circle of its initial end points correspondence are divided into some tri patchs, utilize gouraud shading colorize method to play up these tri patchs, make the border of playing up more level and smooth, clear, more flexible to the regulation and control of operation.
Description of drawings
Fig. 1 is a method flow diagram of the present invention;
Fig. 2 is end points and the frontier point synoptic diagram of garbage mask;
Fig. 3 is divided into the synoptic diagram of tri patch for outside arm of angle circle of the end points correspondence of Bezier curve;
Fig. 4 is that frontier point and waypoint coordinate on outer boundary and inner boundary concern synoptic diagram;
Fig. 5 is rendering effect figure of the present invention.
Embodiment
Below in conjunction with the drawings and specific embodiments the present invention is described in detail.
As shown in Figure 1, the garbage mask border rendering method of a kind of tiled provided by the present invention comprises the steps:
(1) all the Bezier curves among the garbage mask is divided into plurality of sub segments;
(2) determine the outside direction of garbage mask;
(3) go out the outer boundary distance and the inner boundary distance of all waypoints according to the information calculations of frontier point;
(4) be that outer/inner the border of its correspondence and outer/inner arm of angle circle of its initial end points correspondence are drawn by unit with every Bezier curve.
In said method, at first to carry out the segmentation of Beizer curve among the garbage mask.Because for playing up of the processing border of tiled, the Bezier curve must be divided into some continuous sub-line segments, come this Beizer curve of approximate representation with this a little line segment, the tie point of these line segments is referred to as waypoint (segment point), a Beizer curve among the garbage mask has two waypoints at least, just its initial end points and end caps.The simplest method that a Bezier curve is divided into plurality of sub segments is exactly evenly to divide by the parametric t of this Bezier parametric equation, if can cause the very low curve of a lot of curvature to divide a lot of waypoints in vain too much but like this get, reduced the speed of the processing of some algorithms of back, if but get and under the very high situation of curvature, can cause curve level and smooth inadequately very little again.The present invention adopts the method for dynamic division, specific algorithm is as follows: for a Bezier curve, regulation is divided into 30 sub-line segments at most, that is to say that step-length minimum between the waypoint is 1/30, advance forward for step-length by 1/30, calculate the position tangent line vector of current this point, and carry out dot product with the unit tangent vector of this vector and previous fixed waypoint (t=0 must be a waypoint) and calculate, if the value that draws is less than threshold value cos (2.5 π/180), then current point just is defined as a waypoint, not so this point is just ignored in the past, then no matter be which kind of situation all continues by 1/30 step-length this process of repetition of marching forward, up to the end of going to this Bezier curve (t=1 also must be a waypoint).The benefit of dividing waypoint like this is if the sub-line segment that curvature of curve very low (or even straight line) is divided so will seldom even have only one section, and the sub-line segment of the high more division of curvature of curve is just many more, so this method is adaptive.
Next will distinguish outside and the inside of garbage mask.The present invention still is that right side (counter clockwise direction or clockwise direction in other words conj.or perhaps) stipulates which side is the outside of garbage mask along the left side that positive dirction is seen with Bezier curve among the garbage mask, and this is called outside direction.Left side such as the regulation curve is an outside direction, that is to say that so the Bezier curve is the perimeter of garbagemask along the left side that positive dirction is seen, the right side is an interior zone.In order to determine the outside direction of a garbage mask, following algorithm is arranged:
1. calculate the bounding box (bounding box) of garbage mask, obtain the maximum y value of bounding box and the intermediate value yc of minimum y value then.
In order to ask the bounding box of a garbage mask, at first to obtain the bounding box of each bar Bezier curve among the garbage mask.Concrete solution procedure is as follows: for the maximum/minimum value of horizontal direction, at first calculate x (0) and x (1), then x (t) asked the single order derived function, x ' (t)=3a
xT
2+ 2b
xT+c
x, find the solution (t)=0 real root in t ∈ [0,1] of x ', and then the functional value of these real root correspondences and the value of x (0) and x (1) compared, just can obtain x (t) in t ∈ [0,1] interval maximal value and minimum value, the left margin and the right margin of Here it is this Bez i er curve bounding box.In like manner, calculate y (t), can obtain the coboundary and the lower boundary of bounding box in t ∈ [0,1] interval maximal value and minimum value.Solve so after the bounding box of left and right sides curve among the garbage mask, we just may obtain the bounding box boundary value of whole garbage mask according to the most left/the rightest/the go up most/boundary value under of these bounding boxs again.
2. find out this horizontal line of y=yc and the intersection point (in asking the process of friendship, being actually the intersection point of the sub-line segment of asking this horizontal line and this Bezier curve) of all Bezier curves above the garbage mask, and find out leftmost that intersection point in these intersection points.
3. obtain the tangent line vector of leftmost that intersection point in its Bezier curve, carry out the cross product computing with the unit vector of x axle positive dirction then, if the z value of cross product operation result greater than 0 then outside direction is the right side, otherwise is the left side.
Before carrying out specifically the playing up of garbage mask border, also need to calculate pairing outer boundary distance of waypoint and inner boundary distance on all Bezier curves, these two numerical value are used in order to constitute whole inside and outside boundary curve.Because frontier point has only determined the position of several discrete frontier points, but it can't determine the shape of inner boundary or the outer boundary of whole garbage mask, therefore also can't play up whole border.And the pairing outer boundary of waypoint distance and inner boundary distance are just for the inner boundary that can determine whole garbage mask or the needed data of shape of outer boundary, according to the pairing outer boundary of each waypoint distance and inner boundary distance just can with background technology on the computation bound point border introduced the similar method of coordinate calculate coordinate on the corresponding inner and outer boundary of each waypoint, thereby these coordinate points are coupled together the whole inside/outside of formation border.Below just introduce how to calculate pairing outer boundary distance of each waypoint and inner boundary distance, that outer/inner frontier point of position attribution numerical value minimum above garbage mask, find next outer/inner the frontier point adjacent with it, the position attribution of supposing these two frontier points is respectively p0 and p1, apart from attribute (frontier point is away from the distance on border) is respectively d0 and d1, travel through all waypoints between these two frontier points, outer/inner frontier distance of each segmentation can calculate like this: the position of supposing this waypoint is p, so its outer/inner frontier distance d=d0+ (p-p0)/(p1-p0) (d1-d0).Handle all frontier points successively by this method and to the last get back to outer/inner the frontier distance that first frontier point can calculate all waypoints.
Introduce the border of how playing up garbage mask with the method for tiled below, concrete method is: travel through in order that each end points E plays up its pairing border of pairing forward Bezier curve among the garbage mask.Be divided into following two steps:
1) plays up outer/inner arm of angle circle of this end points
Arm of angle circle is meant the forward tangent line and the inconsistent fan-like pattern border of causing of reverse tangential direction of this end points, two waists on this fan-like pattern border are respectively perpendicular to the forward tangent line and the reverse tangent line of this end points, the length of waist is exactly outer/inner frontier distance of this end points, and Fig. 3 is the synoptic diagram of outside arm of angle circle.With this fan-like pattern boundary demarcation is some tri patchs, in the present embodiment arm of angle circle is divided into 20 fixing tri patchs, calculate the gray-scale value on these tri patch summits then, gray-scale value for that summit of end points correspondence is 1, for the summit gray-scale value on the outer boundary is exactly 0, utilizes gouraud shading colorize method to play up these tri patchs then and gets final product.
2) play up outer/inner border of this end points forward curve
Play up outer/inner border of this end points forward curve and will carry out following processing: find position attribution all frontier points between the starting and ending waypoint of this sub-line segment each sub-line segment of this end points forward curve, these frontier points are the outer boundary point a bit, and some then is the inner boundary point.When playing up outer boundary, for the outer boundary point between the starting and ending waypoint of each sub-line segment, always calculate coordinate (referring to the description in the background technology) on its outer boundary according to coordinate on its curve and its external parties apart from attribute and current garbage mask, coordinate points in the back will be as the summit of tri patch on coordinate points and the border on its curve; For the inner boundary point between the starting and ending waypoint of each sub-line segment, can not directly use on its border coordinate points as the summit of tri patch, because its direction is anti-, need obtain its outer boundary distance apart from carrying out linear interpolation according to the outer boundary of the broad sense outer boundary point of its front (comprise outer boundary put coordinate points on the outer boundary corresponding of coordinate points on the pairing border) and the broad sense outer boundary point of back with waypoint, and then calculate coordinate on its outer boundary, position attribution such as certain inner boundary point is p, the position of that broad sense outer boundary point of its front is p0, distance is d0, the position of that broad sense outer boundary point of its back is p1, distance is d1, its outer boundary is apart from d=d0+ (p-p0)/(p1-p0) (d1-d0) so, just can calculate coordinate on its outer boundary so like this, thereby can use on its curve on the coordinate points and outer boundary coordinate points as the summit of tri patch.
When playing up the inner boundary of Bezier curve correspondence, for the inner boundary point between the starting and ending waypoint of each sub-line segment, always calculate coordinate (referring to the description in the background technology) on its inner boundary according to coordinate on its curve and its internal side apart from attribute and current garbagemask; For the outer boundary point between the starting and ending waypoint of each sub-line segment, inner boundary according to the broad sense inner boundary point of its front and the broad sense inner boundary point of back obtains its inner boundary distance apart from carrying out linear interpolation, and then calculating coordinate on its inner boundary, concrete grammar and above-mentioned situation when playing up outer boundary are similar.
Fig. 4 has shown an instantiation, as can be seen from Figure 4,3 frontier points is arranged between two continuous waypoints, and two outer boundary points are wherein arranged, and an inner boundary point is arranged.So just this line segment has been divided into 4 sections, the outer boundary of each section can constitute a quadrilateral, next will play up these quadrilaterals exactly.But before playing up, at first to determine the gray-scale value on these quadrilateral summits.For its gray-scale value of summit that is positioned on the outer boundary must be 0; Obtain for then need between 0 to 1, to carry out linear interpolation on the summit on the curve with the ratio of its outer boundary distance and inner boundary distance.Attention: the frontier distance that outer boundary point is put coordinate points on the border externally at the frontier distance of coordinate points on the inner boundary or inner boundary is that the frontier distance interpolation by its adjacent broad sense inside/outside frontier point obtains, and obtains apart from interpolation such as the outer boundary distance of the inner boundary point C outer boundary by outer boundary point A and outer boundary point B.
These quadrilaterals will have been played up exactly so, the present invention plays up it with the method that simply these quadrilaterals is divided into two tri patchs, because the situation of tangible ladder sense is arranged at the critical place that can occur two tri patchs in some cases, therefore these quadrilaterals further to be subdivided into the plurality of sub quadrilateral again, divided method is as follows: at first this quadrilateral on average is divided into the experimental process quadrilateral along the trend of Bezier curve, concrete quantity of dividing can be calculated like this: suppose that l0 represents the length of this quadrilateral before waist, l1 represents the length of that waist of back, so sub-quadrilateral quantity
Just can calculate this a little tetragonal apex coordinate and gray-scale value after drawing sub-quadrilateral quantity by linear interpolation.And then utilize similar method that each sub-quadrilateral is divided into the plurality of sub quadrilateral along Bezier normal to curve direction, just whole quadrilateral can be divided into the plurality of sub quadrilateral at last.Afterwards each sub-quadrilateral being divided into two adjacent tri patchs utilizes gouraud shading colorize method to play up these tri patchs to get final product then.
Method of the present invention is not limited to the embodiment described in the embodiment, and those skilled in the art's technical scheme according to the present invention draws other embodiment, belongs to innovation scope of the present invention equally.