Disclosure of Invention
In order to solve the above technical problems, an object of the present invention is to provide a method for judging hair direction based on gradient direction, which can process images shot at any angle, and has high accuracy and high processing speed.
In order to achieve the technical purpose, the invention provides a hair direction judging method based on a gradient direction, which specifically comprises the following steps:
(1) detecting 68 characteristic points (marked as B1-B68 respectively) of the face in the face portrait picture by using a Dlib face characteristic point detection algorithm;
(2) rotating the image according to 68 characteristic points of the face to enable the face to be in the horizontal position of the image, and then intercepting an integral photo area of the face containing hair;
(3) cutting out the image of the hair region aiming at the transformed complete face region, and then calculating the gradient direction of each pixel point of the image of the region;
(4) and traversing the gradient direction of each pixel point of the whole hair area, and judging the direction of the hair according to the change condition of the gradient direction.
Specifically, the step (2) includes the steps of:
(2.1) calculating an included angle alpha between a straight line formed by points B1 and B17 of the human face contour and a horizontal line;
(2.2) rotating the image clockwise by alpha to obtain an image of a horizontal face, and correcting 68 coordinates of the characteristic point of the face;
(2.3) determining the coordinates (X) of the center point C of the facec,Yc);
(2.4) calculating X coordinates of upper left and lower right corners of the rectangular face region based on the C-point seat and the coordinates of points B1 and B17;
(2.5) calculating Y coordinates of upper left and lower right corners of the rectangular face region based on the C-point seat and the coordinates of points B1 and B17;
and (2.6) determining and intercepting face and hair area images based on the coordinates of the upper left corner and the lower right corner.
Specifically, the step (3) includes the steps of:
(3.1) determining a hair region in the face image;
(3.2) calculating the gradient angle theta of each pixel point in the hair region;
and (3.3) obtaining a gradient angle matrix of the pixel points in the hair area.
Specifically, the step (4) includes the steps of:
(4.1) normalizing the gradient angle matrix;
(4.2) dividing the hair region image into 2N small regions (N is a positive integer), calculating the average value of the gradient angle of each region, and normalizing the average value;
(4.3) calculating the average value of the gradient angles of the left N blocks of regions and the right N blocks of regions;
and (4.4) judging the hair direction according to the average value of the gradient angles of the N areas on the left side and the right side.
In particular, the angle
Wherein (X)
B1,Y
B1) Is B1 point coordinate, (X)
B17,Y
B17) Is B17 point coordinates.
Specifically, the calculation formula of the X coordinates of the upper left corner and the lower right corner of the rectangular face area is as follows: x coordinate X of upper left corner point
LCan be calculated by the following formula, X
L=X
C-1.3×(X
C-X
B1) If X is
LLess than 0, then X
L0; x coordinate X of lower right corner
RCan be calculated by the following formula, X
R=X
C+1.3×(X
B17-X
C) (ii) a If X is calculated above
RGreater than the Width Width of the image, then X
RWidth; wherein, the coordinates of the point C
Specifically, the calculation formula of the Y coordinates of the upper left corner and the lower right corner of the rectangular face area is as follows: y coordinate Y of upper left corner point
LCan be calculated by the following formula, Y
L=Y
C-1.5×(Y
B9-Y
C) Wherein B9 is the edge of the face contour that marks the chin boundary, if Y
LLess than 0, then Y
L0; y coordinate Y of lower right corner
RCan be calculated by the following formula, Y
R=Y
C+1.5×(Y
B9-Y
C) If Y is calculated as above
RGreater than Height of image, Y
RHeight; wherein, the coordinates of the point C
Specifically, the method for calculating the gradient angle of each pixel point in the hair region is as follows: point Pi(I, j) is the pixel value of I (I, j), firstly, the pixel point P is calculatediGradient D in X-directionxGradient D in (I, J) and Y directionsy(I, J) respectively calculating the formula: dx(I,J)=I(I+1,J)-I(I-1,J),Dy(I, J) ═ I (I, J +1) -I (I, J-1), pixel PiAngle of gradient (D)θ=atan2(Dy(I,J),Dx(I,J))。
Specifically, the gradient angle matrix normalization formula is: when in use
Considering the direction to the right, and setting theta to be 1; when in use
Considering the position to be deviated to the left, and setting theta as-1; when in use
When the deviation is not generated, θ is set to 0.
Specifically, the hair region image is divided into 12 small regions by the following method: taking the center points C of the characteristic points B1 and B17 as the circle centers, drawing a sector area from the point B1 at intervals of 15 degrees, and traversing to the point B17 to obtain exactly 12 sector areas which are respectively marked as S1 to S12; the process of calculating and normalizing the average value of the gradient angle of each block area is as follows: adding the gradient angles of each pixel point in the Si region and then dividing the sum by the number of the pixel points to obtain the average gradient value Ti of the Si region; when Ti is more than 0.3 and less than or equal to 1, the region is considered to be deviated to the right, and Ti is set to 1; when theta is more than or equal to-1 and less than or equal to-0.3, the area is considered to be deviated to the left, and Ti is set as-1; when-0.3 < theta.ltoreq.0.3, the region is considered to have no bias, and Ti is set to 0.
According to the technical scheme, the hair direction judgment method based on the gradient direction detects 68 characteristic points of the face in the face portrait picture by using a Dlib face characteristic point detection algorithm, rotates the image according to the characteristic points in the face portrait picture, enables the face portrait to be in a horizontal position, and can process images shot at any angle; in the processing process, the face image area containing the hair is intercepted from the whole image, and in the hair direction judging process, the gradient direction calculation and the traversing processing of gradient information are only carried out in the hair area, so that the whole calculation amount of the algorithm can be effectively reduced, and the processing speed is improved; the gradient direction of the hair area can well reflect the trend of the hair, so the change of the gradient direction can be naturally estimated.
Detailed Description
The invention is further described below with reference to the accompanying drawings:
referring to fig. 1-2, the method for determining a direction of hair based on a gradient direction provided in this embodiment includes the following steps:
(1) detecting 68 characteristic points of the face in the face portrait picture by using a Dlib face characteristic point detection algorithm; the detection of the human face characteristic points can be completed by adopting a human face characteristic point detection algorithm of an open source library Dlib, 68 characteristic points of a human face can be detected by a Dlib library function, the detection speed is high, the positions of the characteristic points are accurate, the method is a currently popular human face characteristic point detection method, 17 points are used for the face contour edge, 10 points are used for two eyebrows, 12 points are used for two eyes, 9 points are used for a nose, 20 points are used for a mouth, and the specific positions of various organs of the face can be conveniently determined through the 68 points;
(2) marking 68 characteristic points detected by the Dlib as B1-B68, wherein 17 points of the face contour edge are B1-B17, and a straight line consisting of B1 and B17, and calculating the slope
The angle α between the line and the horizontal line can be found, where (X)
B1,Y
B1) Is B1 point coordinate, (X)
B17,Y
B17) B17 point coordinates;
(3) clockwise rotating the whole picture by an angle alpha by taking B1 as a circle center to obtain a corrected face image, so that the face is in a horizontal position, and meanwhile, clockwise rotating 68 feature points by an angle alpha to correct to obtain feature points in the horizontal face;
(4) the center positions of the 1 st point B1 and the 17 th point B17 in the corrected 68 feature points are marked as a point C, the point C is the center point of the whole hair and face image, and the coordinate of the center point C can be calculated
(5) Calculating X coordinates of a left upper corner point and a right lower corner point of the rectangular frame of the intercepted face and hair area based on the coordinates of the point C and the coordinates of a face edge starting point B1 and an end point B17; wherein, the X coordinate X of the upper left corner pointLCan be calculated by the following formula, XL=XC-1.3×(XC-XB1) If X isLLess than 0, then XL0; x coordinate X of lower right cornerRCan be calculated by the following formula, XR=XC+1.3×(XB17-XC) (ii) a If X is calculated aboveRGreater than the Width Width of the image, then XR=Width;
(6) Based on the coordinates of the point C and the coordinates of the starting point B1 and the ending point B17 of the face edge, the Y coordinates of the upper left corner point and the lower right corner point of the rectangle frame of the intercepted face and hair area are calculated, and the Y coordinate of the upper left corner point is Y coordinateLCan be calculated by the following formula, YL=YC-1.5×(YB9-YC) Wherein B9 is the edge of the face contour that marks the chin boundary, if YLLess than 0, then YL0; y coordinate Y of lower right cornerRCan be calculated by the following formula, YR=YC+1.5×(YB9-YC) If Y is calculated as aboveRGreater than Height of image, YR=Height;
(7) Coordinates of upper left corner (X)L,YL) And coordinates of lower right corner (X)R,YR) The rectangular area marked by the method is the face and hair area image, and the image area is zoomed, so that the zoomed image width is 480 pixels;
(8) characteristic point B
iHas an original coordinate of (X)
Bi,Y
Bi) B in the new image obtained in step 2
iIs (X'
Bi,Y
Bi') can be calculated by the following formula, wherein the X coordinate is calculated by the formula:
the calculation formula of the Y coordinate is as follows:
(9) taking the area above the ears as the hair area, and intercepting the Y coordinate which is less than Y ═ max (Y)B1,YB17) As a hair region;
(10) calculating the gradient angle of each pixel point (I, j) in the hair region, wherein the pixel value of the pixel point (I, j) is I (I, j), and firstly calculating the gradient D of the pixel point in the X directionxGradient D in (I, J) and Y directionsy(I, J) respectively calculating the formula: dx(I,J)=I(I+1,J)-I(I-1,J),Dy(I, J) ═ I (I, J +1) -I (I, J-1), gradient angle θ of pixel point ═ atan2 (D)y(I,J),Dx(I,J));
(11) Obtaining a gradient angle matrix of a new image, wherein the Y coordinate is larger than Y ═ max (Y)B1,YB17) In other regions, the gradient angle θ of the pixel point is set to 0;
(12) normalizing the gradient angle matrix of the image by normalizing the gradient angle of the pixels by the following formula: when in use
Considering the direction to the right, and setting theta to be 1; when in use
Considering the position to be deviated to the left, and setting theta as-1; when in use
Considering no deviation, and setting theta as 0;
(13) taking the central points C of the characteristic points B1 and B17 as the circle centers, drawing a sector area from the point B1 at intervals of 15 degrees, traversing to the point B17 to obtain just 12 sector areas, respectively marking as S1 to S12, and adding the gradient angle of each pixel point in each sector area Si according to the normalized gradient angle matrix, and then dividing the gradient angle by the number of the pixel points to obtain the area average gradient value Ti of the Si;
(14) normalizing the average gradient value Ti of the sector area, and normalizing the average gradient Ti of the 12 areas calculated by the following formula: when Ti is more than 0.3 and less than or equal to 1, the region is considered to be deviated to the right, and Ti is set to 1; when theta is more than or equal to-1 and less than or equal to-0.3, the area is considered to be deviated to the left, and Ti is set as-1; when theta is more than-0.3 and less than or equal to 0.3, the area is considered to have no deviation, and Ti is set to be 0;
(15) summing the average gradient values of 6 fan-shaped areas in the left half area of the hair image to obtain a gradient value TL(ii) a Summing the average gradient values of 6 fan-shaped areas of the right half area to obtain a gradient value TR(ii) a For TLAnd TRWhen it is less than 0, it indicates that the hair is biased to the left, set to-1; when the hair deflection is larger than 0, the hair is deflected to the right and is set as 1;
(16) based on TLAnd TRJudging the hair direction according to the different values of the hair direction; there are a total of 4 states of hair distribution: dividing, left dividing, right dividing and undivided; when T isLIs-1 and TRWhen the number is 1, the number is judged to be the median; when T isL+TR<When 0, judging as left score; when T isL+TR>When 0, judging as right score; in addition to the above-described bisection case, TL+TRWhen 0, the hair has no direction.
The invention uses Dlib face feature point detection algorithm to detect 68 feature points of the face in the face head portrait picture, and can process images shot at any angle; in the processing process, the face image area containing the hair is intercepted from the whole image, and in the hair direction judging process, the gradient direction calculation and the traversing processing of gradient information are only carried out in the hair area, so that the whole calculation amount of the algorithm can be effectively reduced, and the processing speed is improved; because the gradient direction of the hair area can well reflect the trend of the hair, the change of the gradient direction can be naturally estimated, and the invention can accurately judge the direction of the hair based on the principle and has higher accuracy; the invention has good application value.
The above-mentioned embodiments are merely illustrative of the preferred embodiments of the present invention, and do not limit the scope of the present invention, and various modifications and improvements of the technical solution of the present invention by those skilled in the art should fall within the protection scope defined by the claims of the present invention without departing from the spirit of the present invention.